diff options
Diffstat (limited to 'cpp/src/IceUtil/StringUtil.cpp')
-rw-r--r-- | cpp/src/IceUtil/StringUtil.cpp | 66 |
1 files changed, 40 insertions, 26 deletions
diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp index 92acea288ca..c2aa45d3c95 100644 --- a/cpp/src/IceUtil/StringUtil.cpp +++ b/cpp/src/IceUtil/StringUtil.cpp @@ -311,42 +311,56 @@ IceUtil::unescapeString(const string& s, string::size_type start, string::size_t bool IceUtil::splitString(const string& str, const string& delim, vector<string>& result) { - string::size_type beg; - string::size_type end = 0; - while(true) + string::size_type pos = 0; + string::size_type length = str.length(); + string elt; + + while(pos < length) { - beg = str.find_first_not_of(delim, end); - if(beg == string::npos) + char quoteChar = '\0'; + if(str[pos] == '"' || str[pos] == '\'') { - break; + quoteChar = str[pos]; + ++pos; } - - // - // Check for quoted argument. - // - char ch = str[beg]; - if(ch == '"' || ch == '\'') + bool trim = true; + while(pos < length) { - beg++; - end = str.find(ch, beg); - if(end == string::npos) + if(quoteChar != '\0' && str[pos] == '\\' && pos + 1 < length && str[pos + 1] == quoteChar) { - return false; + ++pos; } - result.push_back(str.substr(beg, end - beg)); - end++; // Skip end quote. - } - else - { - end = str.find_first_of(delim + "'\"", beg); - if(end == string::npos) + else if(quoteChar != '\0' && str[pos] == quoteChar) { - end = str.length(); + trim = false; + ++pos; + quoteChar = '\0'; + break; } - result.push_back(str.substr(beg, end - beg)); + else if(delim.find(str[pos]) != string::npos) + { + if(quoteChar == '\0') + { + ++pos; + break; + } + } + + if(pos < length) + { + elt += str[pos++]; + } + } + if(quoteChar != '\0') + { + return false; // Unmatched quote. + } + if(elt.length() > 0) + { + result.push_back(elt); + elt = ""; } } - return true; } |