util/StringTrim.h

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      StringTrim.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Helper to emulate the java String.trim() method
00006 Author:    <Michele Laghi> laghi@swissinfo.org
00007 -----------------------------------------------------------------------------*/
00008 
00009 #ifndef _UTIL_STRINGTRIM_H
00010 #define _UTIL_STRINGTRIM_H
00011 
00012 #include <util/XmlBCfg.h>
00013 #include <ctype.h>  // ::tolower
00014 #include <string>
00015 #include <util/lexical_cast.h>
00016 
00017 #define  EMPTY_STRING std::string("")
00018 
00019 
00020 
00021 namespace org { namespace xmlBlaster {
00022 namespace util {
00023    
00031    class Dll_Export StringTrim
00032    {
00033    public:
00034       
00038       StringTrim()
00039       {
00040       }
00041 
00046       static std::string& toLowerCase(std::string& ref)
00047       {
00048          std::string::iterator iter = ref.begin();
00049          while (iter != ref.end()) {
00050             *iter = ::tolower(*iter);
00051             iter++;
00052          }
00053          return ref;
00054       }
00055 
00059       static std::string replaceAll(const std::string &str, const std::string &from, const std::string &to) {
00060          if (str.empty() || from.empty() || to.empty())
00061             return str;
00062          if (str.find(from) == std::string::npos)
00063             return str;
00064 
00065          std::string buf;
00066          std::string tail = str;
00067          while (true) {
00068             std::string::size_type index = tail.find(from);
00069             if (index != std::string::npos) {
00070                if (index > 0)
00071                   buf += tail.substr(0, index);
00072                buf += to;
00073                tail = tail.substr(index + from.size());
00074             }
00075             else
00076                break;
00077          }
00078          buf += tail;
00079          return buf;
00080       }
00081 
00088       static bool isTrue(const std::string& str) {
00089          std::string tmp = trim(str.c_str());
00090          return lexical_cast<bool>(tmp);
00091       }
00092 
00100       static bool isTrue(const std::string& str, bool def) {
00101          std::string value = trim(str);
00102          if (value.length() == 0) return def;
00103          if (isTrue(value) == true) return true;
00104          if ((value=="0")||(value=="false")||(value=="FALSE")) return false;
00105          return def;
00106       }
00107 
00116       static bool isTrueTrim(std::string& str) {
00117          trim(str);
00118          if (str.length() == 0) return true;
00119          return lexical_cast<bool>(str);
00120       }
00121 
00126        /*
00127       static int std::stringLength(const char *str)
00128       {
00129          int count = 0;
00130          while (str[count] != (char)0) count++;
00131          return count;
00132       }
00133         */
00134 
00138       static std::string trimStart(const char *str)
00139       {
00140          if (str == static_cast<const char *>(0)) return EMPTY_STRING;
00141 
00142          int start = 0;
00143          while (str[start] != 0)
00144             if (isspace(str[start++]))
00145                continue;
00146             else
00147                return std::string(str + (start-1));
00148 
00149          return EMPTY_STRING;
00150       }
00151       
00152 
00158       static std::string& trimStart(std::string &str)
00159       {
00160          if (str.capacity() < 1) return str;
00161 
00162          if (str.length() < 1)
00163             return str;
00164          if (!isspace(str[0]))
00165             return str;
00166 
00167          for (std::string::size_type ii=1; ii<str.length(); ii++) {
00168             if (!isspace(str[ii])) {
00169                str = str.substr(ii);
00170                return str;
00171             }
00172          }
00173 
00174          return str;
00175       }
00176 
00180       static std::string trimEnd(const char *str)
00181       {
00182          if (str == static_cast<const char *>(0) || *str == 0) return EMPTY_STRING;
00183          std::string strip(str);
00184          trimEnd(strip);
00185          return strip;
00186       }      
00187 
00193       static std::string& trimEnd(std::string &str)
00194       {
00195          if (str.capacity() < 1) return str;
00196 
00197          int i;
00198          for (i=(int)str.length()-1; i >= 0; i--) {
00199              if (!isspace(str[i])) {
00200                  str = str.substr(0, i+1);
00201                  return str;
00202              }
00203          }
00204          if (i<0) str = EMPTY_STRING;
00205          return str;
00206       }
00207       
00208 
00217       static std::string trim(const char *str)
00218       {
00219          std::string buffer = trimStart(str);
00220          if (buffer.empty()) return EMPTY_STRING;
00221          return trimEnd(buffer.c_str());
00222       }
00223 
00229       static void trim(std::string &str)
00230       {
00231          if (str.capacity() < 1 || str.size() < 1) return;
00232 
00233          int jj=0;
00234          if (isspace(str[str.size()-1])) {
00235             for (jj=(int)str.size()-2; jj >= 0; jj--) {
00236                 if (!isspace(str[jj])) {
00237                    str.resize(jj+1);
00238                    break;
00239                 }
00240             }
00241          }
00242          if (jj<0) {
00243             str = EMPTY_STRING;
00244             return;
00245          }
00246 
00247          if (!isspace(str[0]))
00248             return;
00249          for (std::string::size_type ii=1; ii<str.size(); ii++) {
00250             if (!isspace(str[ii])) {
00251                str = str.substr(ii);
00252                return;
00253             }
00254          }
00255       }
00256 
00257       static std::string trim(const std::string &str) {
00258          std::string tmp = str;
00259          trim(tmp);
00260          return tmp;
00261       }
00262 
00263    };
00264 
00265 }}} // namespace
00266 
00267 #endif
00268 
00269