1 // Module: Log4CPLUS
2 // File: stringhelper.h
3 // Created: 3/2003
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
17 #define LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/tstring.h>
21
22 #include <algorithm>
23
24
25 namespace log4cplus {
26 namespace helpers {
27
28 /**
29 * Returns <code>s</code> in upper case.
30 */
31 LOG4CPLUS_EXPORT log4cplus::tstring toUpper(const log4cplus::tstring& s);
32
33
34 /**
35 * Returns <code>s</code> in lower case.
36 */
37 LOG4CPLUS_EXPORT log4cplus::tstring toLower(const log4cplus::tstring& s);
38
39
40 /**
41 * Tokenize <code>s</code> using <code>c</code> as the delimiter and
42 * put the resulting tokens in <code>_result</code>. If
43 * <code>collapseTokens</code> is false, multiple adjacent delimiters
44 * will result in zero length tokens.
45 * <p>
46 * <b>Example:</b>
47 * <pre>
48 * string s = // Set string with '.' as delimiters
49 * list<log4cplus::tstring> tokens;
50 * tokenize(s, '.', back_insert_iterator<list<string> >(tokens));
51 * </pre>
52 */
53 template <class _StringType, class _OutputIter>
54 void tokenize(const _StringType& s, typename _StringType::value_type c,
55 _OutputIter _result, bool collapseTokens = true)
56 {
57 _StringType tmp;
58 for(typename _StringType::size_type i=0; i<s.length(); ++i) {
59 if(s[i] == c) {
60 *_result = tmp;
61 ++_result;
62 tmp.erase(tmp.begin(), tmp.end());
63 if(collapseTokens)
64 while(s[i+1] == c) ++i;
65 }
66 else
67 tmp += s[i];
68 }
69 if(tmp.length() > 0) *_result = tmp;
70 }
71
72
73
74 template<class intType>
75 inline tstring convertIntegerToString(intType value)
76 {
77 if(value == 0) {
78 return LOG4CPLUS_TEXT("0");
79 }
80
81 char buffer[21];
82 char ret[21];
83 unsigned int bufferPos = 0;
84 unsigned int retPos = 0;
85
86 if(value < 0) {
87 ret[retPos++] = '-';
88 }
89
90 // convert to string in reverse order
91 while(value != 0) {
92 intType mod = value % 10;
93 value = value / 10;
94 buffer[bufferPos++] = '0' + static_cast<char>(mod);
95 }
96
97 // now reverse the string to get it in proper order
98 while(bufferPos > 0) {
99 ret[retPos++] = buffer[--bufferPos];
100 }
101 ret[retPos] = 0;
102
103 return LOG4CPLUS_C_STR_TO_TSTRING(ret);
104 }
105
106
107 /**
108 * This iterator can be used in place of the back_insert_iterator
109 * for compilers that don't have a std::basic_string class that
110 * has the <code>push_back</code> method.
111 */
112 template <class _Container>
113 class string_append_iterator {
114 protected:
115 _Container* container;
116 public:
117 typedef _Container container_type;
118 typedef void value_type;
119 typedef void difference_type;
120 typedef void pointer;
121 typedef void reference;
122 typedef std::forward_iterator_tag iterator_category;
123
124 explicit string_append_iterator(_Container& __x) : container(&__x) {}
125 string_append_iterator<_Container>&
126 operator=(const typename _Container::value_type& _cvalue) {
127 *container += _cvalue;
128 return *this;
129 }
130 string_append_iterator<_Container>& operator*() { return *this; }
131 string_append_iterator<_Container>& operator++() { return *this; }
132 string_append_iterator<_Container>& operator++(int) { return *this; }
133 };
134
135 }
136 }
137
138 #endif // LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1