# stringhelper.h

Go to the documentation of this file.

    00001 // Module:  Log4CPLUS
    00002 // File:    stringhelper.h
    00003 // Created: 3/2003
    00004 // Author:  Tad E. Smith
    00005 //
    00006 //
    00007 // Copyright (C) Tad E. Smith  All rights reserved.
    00008 //
    00009 // This software is published under the terms of the Apache Software
    00010 // License version 1.1, a copy of which has been included with this
    00011 // distribution in the LICENSE.APL file.
    00012 //
    00013
    00016 #ifndef LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
    00017 #define LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
    00018
    00019 #include <log4cplus/config.h>
    00020 #include <log4cplus/tstring.h>
    00021
    00022 #include <algorithm>
    00023
    00024
    00025 namespace log4cplus {
    00026     namespace helpers {
    00027
    00031         LOG4CPLUS_EXPORT log4cplus::tstring toUpper(const log4cplus::tstring& s);
    00032
    00033
    00037         LOG4CPLUS_EXPORT log4cplus::tstring toLower(const log4cplus::tstring& s);
    00038
    00039
    00053         template <class _StringType, class _OutputIter>
    00054         void tokenize(const _StringType& s, typename _StringType::value_type c,
    00055                       _OutputIter _result, bool collapseTokens = true)
    00056         {
    00057             _StringType tmp;
    00058             for(typename _StringType::size_type i=0; i<s.length(); ++i) {
    00059                 if(s[i] == c) {
    00060                     *_result = tmp;
    00061                     ++_result;
    00062                     tmp.erase(tmp.begin(), tmp.end());
    00063                     if(collapseTokens)
    00064                         while(s[i+1] == c) ++i;
    00065                 }
    00066                 else
    00067                     tmp += s[i];
    00068             }
    00069             if(tmp.length() > 0) *_result = tmp;
    00070         }
    00071
    00072
    00073
    00074         template<class intType>
    00075         inline tstring convertIntegerToString(intType value)
    00076         {
    00077             if(value == 0) {
    00078                 return LOG4CPLUS_TEXT("0");
    00079             }
    00080
    00081             char buffer[21];
    00082             char ret[21];
    00083             unsigned int bufferPos = 0;
    00084             unsigned int retPos = 0;
    00085
    00086             if(value < 0) {
    00087                 ret[retPos++] = '-';
    00088             }
    00089
    00090             // convert to string in reverse order
    00091             while(value != 0) {
    00092                 intType mod = value % 10;
    00093                 value = value / 10;
    00094                 buffer[bufferPos++] = '0' + static_cast<char>(mod);
    00095             }
    00096
    00097             // now reverse the string to get it in proper order
    00098             while(bufferPos > 0) {
    00099                 ret[retPos++] = buffer[--bufferPos];
    00100             }
    00101             ret[retPos] = 0;
    00102
    00103             return LOG4CPLUS_C_STR_TO_TSTRING(ret);
    00104         }
    00105
    00106
    00112         template <class _Container>
    00113         class string_append_iterator {
    00114         protected:
    00115             _Container* container;
    00116         public:
    00117             typedef _Container          container_type;
    00118             typedef void                value_type;
    00119             typedef void                difference_type;
    00120             typedef void                pointer;
    00121             typedef void                reference;
    00122
    00123             explicit string_append_iterator(_Container& __x) : container(&__x) {}
    00124             string_append_iterator<_Container>&
    00125             operator=(const typename _Container::value_type& __value) {
    00126                 *container += __value;
    00127                 return *this;
    00128             }
    00129             string_append_iterator<_Container>& operator*() { return *this; }
    00130             string_append_iterator<_Container>& operator++() { return *this; }
    00131             string_append_iterator<_Container>& operator++(int) { return *this; }
    00132         };
    00133
    00134     }
    00135 }
    00136
    00137 #endif // LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
    00138