OFFIS DCMTK  Version 3.6.0
strhelp.h
Go to the documentation of this file.
1 // Module: Log4CPLUS
2 // File: stringhelper.h
3 // Created: 3/2003
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright 2003-2010 Tad E. Smith
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 
23 #ifndef LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
24 #define LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
25 
26 #include "dcmtk/oflog/config.h"
27 #include "dcmtk/oflog/tstring.h"
28 #include "dcmtk/ofstd/oflist.h"
29 
30 //#include <algorithm>
31 //#include <limits>
32 //#include <iterator>
33 
34 
35 namespace log4cplus {
36  namespace helpers {
37 
41  LOG4CPLUS_EXPORT log4cplus::tstring toUpper(const log4cplus::tstring& s);
42 
43 
47  LOG4CPLUS_EXPORT log4cplus::tstring toLower(const log4cplus::tstring& s);
48 
49 
63  template <class StringType>
64  inline
65  void
66  tokenize(const StringType& s, typename StringType::value_type c,
67  OFList<StringType>& result, bool collapseTokens = true)
68  {
69  typedef typename StringType::size_type size_type;
70  size_type const slen = s.length();
71  size_type first = 0;
72  size_type i = 0;
73  for (i=0; i < slen; ++i)
74  {
75  if (s[i] == c)
76  {
77  result.push_back(StringType (s, first, i - first));
78  if (collapseTokens)
79  while (i+1 < slen && s[i+1] == c)
80  ++i;
81  first = i + 1;
82  }
83  }
84  if (first != i)
85  result.push_back(StringType (s, first, i - first));
86  }
87 
88 
89  template <typename intType>
91  {
92  static inline
93  void
94  step1 (tchar * & it, intType & value)
95  {
96  // This code assumes two-compliments'. The problem here is that
97  // integer overflow of an signed type is undefined behavior :(
98  // This code is based upon http://www.fefe.de/intof.html
99 
100  // True if intType is unsigned
101  const OFBool isUnsigned = (OFstatic_cast(intType, -1) < 0) ? OFFalse : OFTrue;
102 
103  // If intType is a signed type, halfMaxSigned is intType_MAX / 2
104  const intType halfMaxSigned = OFstatic_cast(intType, 1) << (sizeof(intType) * 8 - 2);
105 
106  // if intType is a signed type, MaxSigned is its intType_MAX
107  const intType maxSigned = halfMaxSigned - 1 + halfMaxSigned;
108 
109  // if intType is a signed type, MinSigned is its intType_MIN
110  const intType minSigned = OFstatic_cast(intType, -1) - maxSigned;
111 
112  // This is the minimum value that intType can represent;
113  const intType minVal = isUnsigned ? 0 : minSigned;
114 
115  //if (value == (STD_NAMESPACE numeric_limits<intType>::min) ())
116  if (value == minVal)
117  {
118  intType const r = value / 10;
119  intType const a = (0-r) * 10;
120  intType const mod = 0-(a + value);
121  value = 0-r;
122 
123  *(it - 1) = OFstatic_cast(tchar, LOG4CPLUS_TEXT('0') + mod);
124  --it;
125  }
126  else
127  value = 0-value;
128  }
129  };
130 
131 
132  template<class intType>
133  inline
134  void
135  convertIntegerToString (tstring & str, intType value)
136  {
137  if (value == 0)
138  str = LOG4CPLUS_TEXT("0");
139  // We can't use (value < 0) because that could cause a compiler
140  // warning for unsigned types.
141  bool const negative = !(value > 0 || value == 0);
142 
143  const size_t buffer_size = 30; // More than enough space, even for 64 bit integers
144  // = intTypeLimits::digits10 + 2;
145  tchar buffer[buffer_size];
146  tchar * it = &buffer[buffer_size];
147  tchar const * const buf_end = it;
148 
149  if (negative)
151 
152  for (; value != 0; --it)
153  {
154  intType mod = value % 10;
155  value = value / 10;
156  *(it - 1) = OFstatic_cast(tchar, LOG4CPLUS_TEXT('0') + mod);
157  }
158 
159  if (negative)
160  {
161  --it;
162  *it = LOG4CPLUS_TEXT('-');
163  }
164 
165  str.assign (OFstatic_cast(tchar const *, it), buf_end - it);
166  }
167 
168 
169  template<class intType>
170  inline
171  tstring
172  convertIntegerToString (intType value)
173  {
174  tstring result;
175  convertIntegerToString (result, value);
176  return result;
177  }
178 
179  } // namespace helpers
180 
181 } // namespace log4cplus
182 
183 #endif // LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2