Changeset 13168


Ignore:
Timestamp:
03/22/12 14:52:09 (13 years ago)
Author:
tbretz
Message:
Implemented a function for word wraping
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/tools.cc

    r12245 r13168  
    7373    return str.substr(start, end-start+1);
    7474}
     75
     76// --------------------------------------------------------------------------
     77//
     78//! This is a static helper to remove leading and trailing whitespaces.
     79//!
     80//! Usage example:
     81//!
     82//! \code
     83//!    string str = "     Dies ist ein test fuer einen ganz     langen Satz "
     84//!        "und ob er korrekt umgebrochen und formatiert wird. Alles "
     85//!        "nur ein simpler test aber trotzdem ganz wichtig.";
     86//!
     87//!    cout << setfill('-') << setw(40) << "+" << endl;
     88//!    while (1)
     89//!    {
     90//!        const string rc = Tools::Wrap(str, 40);
     91//!        if (rc.empty())
     92//!            break;
     93//!        cout << rc << endl;
     94//!    }
     95//! \endcode
     96//!
     97string Tools::Wrap(string &str, size_t width)
     98{
     99    const size_t pos = str.length()<width ? string::npos : str.find_last_of(' ', width);
     100    if (pos==string::npos)
     101    {
     102        const string rc = str;
     103        str = "";
     104        return rc;
     105    }
     106
     107    const size_t indent = str.find_first_not_of(' ');
     108
     109    const string rc = str.substr(0, pos);
     110    const size_t p2 = str.find_first_not_of(' ', pos+1);
     111
     112    str = str.substr(0, indent) + str.substr(p2==string::npos ? pos+1 : p2);
     113
     114    return rc;
     115}
  • trunk/FACT++/src/tools.h

    r12246 r13168  
     1#ifndef FACT_Tools
     2#define FACT_Tools
     3
    14#include <string>
    25
     
    69    std::string Form(const char *fmt, ...);
    710    std::string Trim(const std::string &str);
     11    std::string Wrap(std::string &str, size_t width=78);
    812}
     13
     14#endif
Note: See TracChangeset for help on using the changeset viewer.