StringUtils.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Some static methods for string processing
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <string>
00031 #include <cstdio>
00032 #include <utils/common/UtilExceptions.h>
00033 #include <utils/common/TplConvert.h>
00034 #include <utils/common/ToString.h>
00035 #include "StringUtils.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // static member definitions
00044 // ===========================================================================
00045 std::string StringUtils::emptyString;
00046 
00047 
00048 // ===========================================================================
00049 // method definitions
00050 // ===========================================================================
00051 std::string
00052 StringUtils::prune(std::string str) {
00053     int idx = 0;
00054     while (idx<str.length()&&str[idx]<=32) ++idx;
00055     if (idx<str.length()&&idx!=0) {
00056         str = str.substr(idx);
00057     }
00058     idx = str.length() - 1;
00059     while (idx>=0&&str[idx]<=32) --idx;
00060     if (idx!=str.length() - 1) {
00061         str = str.substr(0, idx+1);
00062     }
00063     return str;
00064 }
00065 
00066 
00067 std::string
00068 StringUtils::to_lower_case(std::string str) {
00069     for (size_t i=0; i<str.length(); i++) {
00070         if (str[i]>='A'&&str[i]<='Z') {
00071             str[i] = str[i] + 'a' - 'A';
00072         }
00073     }
00074     return str;
00075 }
00076 
00077 
00078 std::string
00079 StringUtils::convertUmlaute(std::string str) {
00080     str = replace(str, "ä", "ae");
00081     str = replace(str, "Ä", "Ae");
00082     str = replace(str, "ö", "oe");
00083     str = replace(str, "Ö", "Oe");
00084     str = replace(str, "ü", "ue");
00085     str = replace(str, "Ü", "Ue");
00086     str = replace(str, "ß", "ss");
00087     str = replace(str, "É", "E");
00088     str = replace(str, "é", "e");
00089     str = replace(str, "È", "E");
00090     str = replace(str, "è", "e");
00091     return str;
00092 }
00093 
00094 
00095 
00096 std::string
00097 StringUtils::replace(std::string str, const char *what,
00098                      const char *by) {
00099     std::string what_tmp(what);
00100     std::string by_tmp(by);
00101     size_t idx = str.find(what);
00102     size_t what_len = what_tmp.length();
00103     size_t by_len = by_tmp.length();
00104     while (idx!=std::string::npos) {
00105         str = str.replace(idx, what_len, by);
00106         idx = str.find(what, idx+by_len-what_len);
00107     }
00108     return str;
00109 }
00110 
00111 
00112 void
00113 StringUtils::upper(std::string &str) {
00114     for (size_t i=0; i<str.length(); i++) {
00115         if (str[i]>='a'&&str[i]<='z') {
00116             str[i] = str[i] - 'a' + 'A';
00117         }
00118     }
00119 }
00120 
00121 
00122 std::string
00123 StringUtils::toTimeString(int time) {
00124     std::ostringstream oss;
00125     char buffer[4];
00126     sprintf(buffer, "%02i:",(time/3600));
00127     oss << buffer;
00128     time=time%3600;
00129     sprintf(buffer, "%02i:",(time/60));
00130     oss << buffer;
00131     time=time%60;
00132     sprintf(buffer, "%02i", time);
00133     oss << buffer;
00134     return oss.str();
00135 }
00136 
00137 
00138 std::string
00139 StringUtils::escapeXML(const std::string &orig) throw() {
00140     std::string result = replace(orig, "&", "&amp;");
00141     result = replace(result, ">", "&gt;");
00142     result = replace(result, "<", "&lt;");
00143     result = replace(result, "\"", "&quot;");
00144     return replace(result, "'", "&apos;");
00145 }
00146 
00147 
00148 
00149 /****************************************************************************/
00150 

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6