StringHelper.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
24 //----------------------------------------------------------------------
25 #ifndef ICL_CORE_STRING_HELPER_H_INCLUDED
26 #define ICL_CORE_STRING_HELPER_H_INCLUDED
27 
28 #include <algorithm>
29 #include <iomanip>
30 #include <limits>
31 #include <string>
32 #include <sstream>
33 #include <ctype.h>
34 #include <vector>
35 
36 #include "icl_core/BaseTypes.h"
37 #include "icl_core/ImportExport.h"
39 
40 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
41 # include "icl_core/Deprecate.h"
42 #endif
43 
44 namespace icl_core {
45 
46 // all int types
47 template <typename T> String stringify(typename ConvertToRef<T>::ToConstRef x)
48 {
49  std::ostringstream out;
50  out << x;
51  return out.str();
52 }
53 
54 // bool
55 template <> inline String stringify<bool>(const bool& x)
56 {
57  std::ostringstream out;
58  out << std::boolalpha << x;
59  return out.str();
60 }
61 
62 template <> inline String stringify<double>(const double& x)
63 {
64  const int sigdigits = std::numeric_limits<double>::digits10;
65  std::ostringstream out;
66  out << std::setprecision(sigdigits) << x;
67  return out.str();
68 }
69 
70 template <> inline String stringify<float>(const float& x)
71 {
72  const int sigdigits = std::numeric_limits<float>::digits10;
73  std::ostringstream out;
74  out << std::setprecision(sigdigits) << x;
75  return out.str();
76 }
77 
78 template <> inline String stringify<long double>(const long double& x)
79 {
80  const int sigdigits = std::numeric_limits<long double>::digits10;
81  std::ostringstream out;
82  out << std::setprecision(sigdigits) << x;
83  return out.str();
84 }
85 
87 {
88  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
89  return str;
90 }
91 
93 {
94  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
95  return str;
96 }
97 
98 
99 template <typename T>
100 String padLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
101 {
102  return padLeft<String>(stringify<T>(x), width, pad_chr);
103 }
104 
105 template <>
106 inline String padLeft<String>(const String& str, size_t width, char pad_chr)
107 {
108  size_t str_len = str.length();
109  if (str_len < width)
110  {
111  return String(width - str_len, pad_chr) + str;
112  }
113  else
114  {
115  return str;
116  }
117 }
118 
119 template <typename T>
120 String padRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
121 {
122  return padRight<String>(stringify<T>(x), width, pad_chr);
123 }
124 
125 template <>
126 inline String padRight<String>(const String& str, size_t width, char pad_chr)
127 {
128  size_t str_len = str.length();
129  if (str_len < width)
130  {
131  return str + String(width - str_len, pad_chr);
132  }
133  else
134  {
135  return str;
136  }
137 }
138 
147 ICL_CORE_IMPORT_EXPORT std::vector<String> split(const String& str, const String& delimiter);
148 
154 ICL_CORE_IMPORT_EXPORT String join(const std::vector<String>& substrings, const String& delimiter);
155 
157 
159 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
160 
161 // all int types
162 template <typename T> String Stringify(typename ConvertToRef<T>::ToConstRef x) ICL_CORE_GCC_DEPRECATE_STYLE;
163 template <typename T> String Stringify(typename ConvertToRef<T>::ToConstRef x)
164 {
165  return stringify<T>(x);
166 }
167 
168 // bool
169 template <> inline String Stringify<bool>(const bool& x) ICL_CORE_GCC_DEPRECATE_STYLE;
170 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<bool>(const bool& x)
171 {
172  return stringify<bool>(x);
173 }
174 
175 template <> inline String Stringify<double>(const double& x) ICL_CORE_GCC_DEPRECATE_STYLE;
176 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<double>(const double& x)
177 {
178  return stringify<double>(x);
179 }
180 
181 template <> inline String Stringify<float>(const float& x) ICL_CORE_GCC_DEPRECATE_STYLE;
182 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<float>(const float& x)
183 {
184  return stringify<float>(x);
185 }
186 
187 template <> inline String Stringify<long double>(const long double& x) ICL_CORE_GCC_DEPRECATE_STYLE;
188 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<long double>(const long double& x)
189 {
190  return stringify<long double>(x);
191 }
192 
195 {
196  return toLower(str);
197 }
198 
201 {
202  return toUpper(str);
203 }
204 
205 
206 template <typename T>
207 String PadLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
209 template <typename T>
211 String PadLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr)
212 {
213  return padLeft<T>(x, width, pad_chr);
214 }
215 
216 template <>
217 inline String PadLeft<String>(const String& str, size_t width, char pad_chr)
218  ICL_CORE_GCC_DEPRECATE_STYLE;
219 template <>
221 String PadLeft<String>(const String& str, size_t width, char pad_chr)
222 {
223  return padLeft<String>(str, width, pad_chr);
224 }
225 
226 template <typename T>
227 String PadRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
229 template <typename T>
231 String PadRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr)
232 {
233  return padRight<T>(x, width, pad_chr);
234 }
235 
236 template <>
237 inline String PadRight<String>(const String& str, size_t width, char pad_chr)
238  ICL_CORE_GCC_DEPRECATE_STYLE;
239 template <>
241 String PadRight<String>(const String& str, size_t width, char pad_chr)
242 {
243  return padRight<String>(str, width, pad_chr);
244 }
245 
247 
248 #endif
249 
251 }
252 
253 #endif
Helper definitions for template programming.
String padRight< String >(const String &str, size_t width, char pad_chr)
Definition: StringHelper.h:126
#define ICL_CORE_VC_DEPRECATE_STYLE
Definition: Deprecate.h:53
std::vector< String > split(const String &str, const String &delimiter)
String toUpper(icl_core::String str)
Definition: StringHelper.h:92
String toLower(icl_core::String str)
Definition: StringHelper.h:86
String padLeft(typename ConvertToRef< T >::ToConstRef x, size_t width, char pad_chr= ' ')
Definition: StringHelper.h:100
String trim(String const &str)
Contains macros to deprecate classes, types, functions and variables.
String stringify< float >(const float &x)
Definition: StringHelper.h:70
String join(const std::vector< String > &substrings, const String &delimiter)
#define ICL_CORE_IMPORT_EXPORT
Definition: ImportExport.h:42
String stringify< bool >(const bool &x)
Definition: StringHelper.h:55
String stringify(typename ConvertToRef< T >::ToConstRef x)
Definition: StringHelper.h:47
String stringify< long double >(const long double &x)
Definition: StringHelper.h:78
String padLeft< String >(const String &str, size_t width, char pad_chr)
Definition: StringHelper.h:106
Contains import/export definitions for the Win32 plattform.
String padRight(typename ConvertToRef< T >::ToConstRef x, size_t width, char pad_chr= ' ')
Definition: StringHelper.h:120
std::string String
Definition: BaseTypes.h:43
String stringify< double >(const double &x)
Definition: StringHelper.h:62
Contains Interface base classes and base types.
#define ICL_CORE_GCC_DEPRECATE_STYLE
Definition: Deprecate.h:54


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58