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