StringHelper.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00024 //----------------------------------------------------------------------
00025 #ifndef ICL_CORE_STRING_HELPER_H_INCLUDED
00026 #define ICL_CORE_STRING_HELPER_H_INCLUDED
00027 
00028 #include <algorithm>
00029 #include <iomanip>
00030 #include <limits>
00031 #include <string>
00032 #include <sstream>
00033 #include <ctype.h>
00034 #include <vector>
00035 
00036 #include "icl_core/BaseTypes.h"
00037 #include "icl_core/ImportExport.h"
00038 #include "icl_core/TemplateHelper.h"
00039 
00040 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00041 # include "icl_core/Deprecate.h"
00042 #endif
00043 
00044 namespace icl_core {
00045 
00046 // all int types
00047 template <typename T> String stringify(typename ConvertToRef<T>::ToConstRef x)
00048 {
00049   std::ostringstream out;
00050   out << x;
00051   return out.str();
00052 }
00053 
00054 // bool
00055 template <> inline String stringify<bool>(const bool& x)
00056 {
00057   std::ostringstream out;
00058   out << std::boolalpha << x;
00059   return out.str();
00060 }
00061 
00062 template <> inline String stringify<double>(const double& x)
00063 {
00064   const int sigdigits = std::numeric_limits<double>::digits10;
00065   std::ostringstream out;
00066   out << std::setprecision(sigdigits) << x;
00067   return out.str();
00068 }
00069 
00070 template <> inline String stringify<float>(const float& x)
00071 {
00072   const int sigdigits = std::numeric_limits<float>::digits10;
00073   std::ostringstream out;
00074   out << std::setprecision(sigdigits) << x;
00075   return out.str();
00076 }
00077 
00078 template <> inline String stringify<long double>(const long double& x)
00079 {
00080   const int sigdigits = std::numeric_limits<long double>::digits10;
00081   std::ostringstream out;
00082   out << std::setprecision(sigdigits) << x;
00083   return out.str();
00084 }
00085 
00086 inline String toLower(icl_core::String str)
00087 {
00088   std::transform(str.begin(), str.end(), str.begin(), ::tolower);
00089   return str;
00090 }
00091 
00092 inline String toUpper(icl_core::String str)
00093 {
00094   std::transform(str.begin(), str.end(), str.begin(), ::toupper);
00095   return str;
00096 }
00097 
00098 
00099 template <typename T>
00100 String padLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
00101 {
00102   return padLeft<String>(stringify<T>(x), width, pad_chr);
00103 }
00104 
00105 template <>
00106 inline String padLeft<String>(const String& str, size_t width, char pad_chr)
00107 {
00108   size_t str_len = str.length();
00109   if (str_len < width)
00110   {
00111     return String(width - str_len, pad_chr) + str;
00112   }
00113   else
00114   {
00115     return str;
00116   }
00117 }
00118 
00119 template <typename T>
00120 String padRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
00121 {
00122   return padRight<String>(stringify<T>(x), width, pad_chr);
00123 }
00124 
00125 template <>
00126 inline String padRight<String>(const String& str, size_t width, char pad_chr)
00127 {
00128   size_t str_len = str.length();
00129   if (str_len < width)
00130   {
00131     return str + String(width - str_len, pad_chr);
00132   }
00133   else
00134   {
00135     return str;
00136   }
00137 }
00138 
00147 ICL_CORE_IMPORT_EXPORT std::vector<String> split(const String& str, const String& delimiter);
00148 
00154 ICL_CORE_IMPORT_EXPORT String join(const std::vector<String>& substrings, const String& delimiter);
00155 
00156 ICL_CORE_IMPORT_EXPORT String trim(String const & str);
00157 
00159 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00160 
00161 // all int types
00162 template <typename T> String Stringify(typename ConvertToRef<T>::ToConstRef x) ICL_CORE_GCC_DEPRECATE_STYLE;
00163 template <typename T> String Stringify(typename ConvertToRef<T>::ToConstRef x)
00164 {
00165   return stringify<T>(x);
00166 }
00167 
00168 // bool
00169 template <> inline String Stringify<bool>(const bool& x) ICL_CORE_GCC_DEPRECATE_STYLE;
00170 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<bool>(const bool& x)
00171 {
00172   return stringify<bool>(x);
00173 }
00174 
00175 template <> inline String Stringify<double>(const double& x) ICL_CORE_GCC_DEPRECATE_STYLE;
00176 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<double>(const double& x)
00177 {
00178   return stringify<double>(x);
00179 }
00180 
00181 template <> inline String Stringify<float>(const float& x) ICL_CORE_GCC_DEPRECATE_STYLE;
00182 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<float>(const float& x)
00183 {
00184   return stringify<float>(x);
00185 }
00186 
00187 template <> inline String Stringify<long double>(const long double& x) ICL_CORE_GCC_DEPRECATE_STYLE;
00188 template <> ICL_CORE_VC_DEPRECATE_STYLE inline String Stringify<long double>(const long double& x)
00189 {
00190   return stringify<long double>(x);
00191 }
00192 
00193 inline String Tolower(icl_core::String str) ICL_CORE_GCC_DEPRECATE_STYLE;
00194 inline ICL_CORE_VC_DEPRECATE_STYLE String Tolower(icl_core::String str)
00195 {
00196   return toLower(str);
00197 }
00198 
00199 inline String Toupper(icl_core::String str) ICL_CORE_GCC_DEPRECATE_STYLE;
00200 inline ICL_CORE_VC_DEPRECATE_STYLE String Toupper(icl_core::String str)
00201 {
00202   return toUpper(str);
00203 }
00204 
00205 
00206 template <typename T>
00207 String PadLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
00208   ICL_CORE_GCC_DEPRECATE_STYLE;
00209 template <typename T>
00210 ICL_CORE_VC_DEPRECATE_STYLE
00211 String PadLeft(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr)
00212 {
00213   return padLeft<T>(x, width, pad_chr);
00214 }
00215 
00216 template <>
00217 inline String PadLeft<String>(const String& str, size_t width, char pad_chr)
00218   ICL_CORE_GCC_DEPRECATE_STYLE;
00219 template <>
00220 inline ICL_CORE_VC_DEPRECATE_STYLE
00221 String PadLeft<String>(const String& str, size_t width, char pad_chr)
00222 {
00223   return padLeft<String>(str, width, pad_chr);
00224 }
00225 
00226 template <typename T>
00227 String PadRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr = ' ')
00228   ICL_CORE_GCC_DEPRECATE_STYLE;
00229 template <typename T>
00230 ICL_CORE_VC_DEPRECATE_STYLE
00231 String PadRight(typename ConvertToRef<T>::ToConstRef x, size_t width, char pad_chr)
00232 {
00233   return padRight<T>(x, width, pad_chr);
00234 }
00235 
00236 template <>
00237 inline String PadRight<String>(const String& str, size_t width, char pad_chr)
00238   ICL_CORE_GCC_DEPRECATE_STYLE;
00239 template <>
00240 inline ICL_CORE_VC_DEPRECATE_STYLE
00241 String PadRight<String>(const String& str, size_t width, char pad_chr)
00242 {
00243   return padRight<String>(str, width, pad_chr);
00244 }
00245 
00246 ICL_CORE_VC_DEPRECATE_STYLE String Trim(String const & str) ICL_CORE_GCC_DEPRECATE_STYLE;
00247 
00248 #endif
00249 
00250 
00251 }
00252 
00253 #endif


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04