X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=flower%2Fstd-string.cc;h=5232574366c79c950c4e99101b09e5c88535764d;hb=def21b306e2b8fa2d5630fab0878e9922e197f0c;hp=4069278cb20d35fc2cd5b15cc0e5b0ec25c86df8;hpb=ad7402e8833de98674ebf26c6c1c94ceef266d42;p=lilypond.git diff --git a/flower/std-string.cc b/flower/std-string.cc index 4069278cb2..5232574366 100644 --- a/flower/std-string.cc +++ b/flower/std-string.cc @@ -3,7 +3,7 @@ source file of the GNU LilyPond music typesetter - (c) 2006 Jan Nieuwenhuizen + (c) 2006--2007 Jan Nieuwenhuizen */ #include "std-string.hh" @@ -67,21 +67,24 @@ to_string (char const *format, ...) return str; } +/* + TODO: this O(n^2) in #occurences of find, due to repeated copying. + */ string & -replace_all (string &str, string find, string replace) +replace_all (string *str, string const &find, string const &replace) { ssize len = find.length (); - for (ssize i = str.find (find); i != NPOS; i = str.find (find, i + len)) - str = str.replace (i, len, replace); - return str; + for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + len)) + *str = str->replace (i, len, replace); + return *str; } string & -replace_all (string &str, char find, char replace) +replace_all (string *str, char find, char replace) { - for (ssize i = str.find (find); i != NPOS; i = str.find (find, i + 1)) - str[i] = replace; - return str; + for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + 1)) + (*str)[i] = replace; + return *str; } char * @@ -106,14 +109,14 @@ string_compare (string const &a, string const &b) vector string_split (string str, char c) { - vector a; ssize i = str.find (c); + + vector a; while (i != NPOS) { string s = str.substr (0, i); a.push_back (s); - while (str[++i] == c) - ; + i ++; str = str.substr (i); i = str.find (c); } @@ -121,3 +124,17 @@ string_split (string str, char c) a.push_back (str); return a; } + +string +string_join (vector const &strs, string infix) +{ + string result; + for (vsize i = 0; i < strs.size (); i ++) + { + if (i) + result += infix; + result += strs[i]; + } + + return result; +}