[Jsmooth-cvs] jsmooth/skeletons/util-core StringUtils.cpp, 1.2, 1.3 StringUtils.h, 1.2, 1.3
Status: Beta
Brought to you by:
reyes
From: Rodrigo R. <re...@us...> - 2007-04-28 08:46:21
|
Update of /cvsroot/jsmooth/jsmooth/skeletons/util-core In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv16649 Modified Files: StringUtils.cpp StringUtils.h Log Message: added (un)escaping and command-line quoting methods Index: StringUtils.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-core/StringUtils.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** StringUtils.cpp 4 Apr 2007 19:47:10 -0000 1.2 --- StringUtils.cpp 28 Apr 2007 08:46:17 -0000 1.3 *************** *** 21,25 **** #include "StringUtils.h" ! vector<string> StringUtils::split(const string& str, const string& separators, const string& quotechars, bool handleEscape) { vector<string> result; --- 21,26 ---- #include "StringUtils.h" ! vector<string> StringUtils::split(const string& str, const string& separators, const string& quotechars, ! bool handleEscape, bool returnSeparators) { vector<string> result; *************** *** 28,32 **** for (int i=0; i<str.length(); i++) { ! if ( handleEscape && (str[i] == '\\')) { i++; --- 29,33 ---- for (int i=0; i<str.length(); i++) { ! if ( handleEscape && (str[i] == '\\')) // Found an escaped char { i++; *************** *** 52,56 **** } } ! else if (separators.find(str[i], 0) != separators.npos) { if (buf.length() > 0) --- 53,57 ---- } } ! else if (separators.find(str[i], 0) != separators.npos) // found a separator... { if (buf.length() > 0) *************** *** 59,64 **** buf = ""; } } ! else if (quotechars.find(str[i], 0) != separators.npos) { if (buf.length() > 0) --- 60,73 ---- buf = ""; } + + if (returnSeparators) + { + buf += str[i]; + result.push_back(buf); + buf = ""; + } + } ! else if (quotechars.find(str[i], 0) != quotechars.npos) // Found a quote char... { if (buf.length() > 0) *************** *** 127,130 **** --- 136,146 ---- } + string StringUtils::toHexString(int val) + { + char buf[32]; + sprintf(buf, "%x", val); + return string(buf); + } + string StringUtils::toString(const vector<string>& seq) { *************** *** 241,244 **** --- 257,270 ---- } + string StringUtils::requoteForCommandLine(const string& str) + { + std::string res = StringUtils::replace(str, "\"", ""); + if (res[res.size()-1] == '\\') + res += "\\"; + // res = res.substr(0, res.size()-1); + return StringUtils::fixQuotes(res); + } + + string StringUtils::replace(const string& str, const string& pattern, const string& replacement) { *************** *** 265,268 **** --- 291,328 ---- } + std::string StringUtils::escape(const string& str) + { + std::string result; + for (int i=0; i<str.size(); i++) + { + if (str[i] == '\\') + result += "\\\\"; + else + result += str[i]; + } + return result; + } + + std::string StringUtils::unescape(const string& str) + { + std::string result; + for (int i=0; i<str.size(); i++) + { + if (str[i] == '\\') + { + if ((i+1<str.size()) && (str[i+1]=='\\')) + { + i++; + result += "\\"; + } + else + result += "\\"; + } + else + result += str[i]; + } + return result; + } + string StringUtils::toLowerCase(const string& str) { *************** *** 301,302 **** --- 361,386 ---- return res; } + + + std::string StringUtils::sizeToJavaString(int size) + { + if (size > (1024*1024)) + return StringUtils::toString(size / (1024*1024)) + "m"; + else if (size > 1024) + return StringUtils::toString(size / 1024) + "k"; + else + return StringUtils::toString(size); + } + + std::string StringUtils::sizeToJavaString(std::string size) + { + if ( (size.find('m') != string::npos) + || (size.find('M') != string::npos) + || (size.find('k') != string::npos) + || (size.find('K') != string::npos) ) + { + return size; + } + else + return sizeToJavaString(StringUtils::parseInt(size)); + } Index: StringUtils.h =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-core/StringUtils.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** StringUtils.h 4 Apr 2007 19:47:10 -0000 1.2 --- StringUtils.h 28 Apr 2007 08:46:17 -0000 1.3 *************** *** 71,75 **** * @param quotechars a string that contains all the characters that are used as quote chars. */ ! static vector<string> split(const string& str, const string& separators, const string& quotechars, bool handleEscape = true); /** --- 71,75 ---- * @param quotechars a string that contains all the characters that are used as quote chars. */ ! static vector<string> split(const string& str, const string& separators, const string& quotechars, bool handleEscape = true, bool returnSeparators = false); /** *************** *** 91,94 **** --- 91,95 ---- */ static string toString(int val); + static string toHexString(int val); /** *************** *** 136,141 **** * @return a fixed copy of the string */ ! static string fixQuotes(const string& str); ! /** * Ensures a string is correctly quoted, the quotes are enclosing --- 137,144 ---- * @return a fixed copy of the string */ ! static std::string fixQuotes(const string& str); ! ! static std::string escape(const string& str); ! static std::string unescape(const string& str); /** * Ensures a string is correctly quoted, the quotes are enclosing *************** *** 146,154 **** * @return a fixed copy of the string */ ! static string requote(const string& str); ! ! static string toLowerCase(const string& str); static std::string fixArgumentString(const std::string& arg); }; --- 149,161 ---- * @return a fixed copy of the string */ ! static std::string requote(const string& str); ! static std::string StringUtils::requoteForCommandLine(const string& str); ! static std::string toLowerCase(const string& str); static std::string fixArgumentString(const std::string& arg); + + static std::string sizeToJavaString(int size); + static std::string sizeToJavaString(std::string size); + }; |