X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Fstd-string.cc;h=285c51883c5d1fc0a7727816a3e99ab0e5da2e03;hb=c8c3f3cb3714cd487435f35aaf86615716020141;hp=855193ecb07b0a8849e66dec01f5a3e21224b0f0;hpb=51805662a1a967b79469efe05a5a73ffa55fc5e8;p=lilypond.git diff --git a/flower/std-string.cc b/flower/std-string.cc index 855193ecb0..285c51883c 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--2009 Jan Nieuwenhuizen */ #include "std-string.hh" @@ -57,6 +57,12 @@ to_string (unsigned u) return String_convert::unsigned_string (u); } +string +to_string (I64 b, char const *format) +{ + return String_convert::i64_string (b, format); +} + string to_string (char const *format, ...) { @@ -67,21 +73,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 * @@ -89,8 +98,9 @@ string_copy (string s) { ssize len = s.length (); char *dest = new char[len + 1]; - //s.copy (dest, len + 1); - memcpy (dest, s.c_str (), len + 1); + copy (s.begin (), s.end (), dest); + dest[len] = 0; + return dest; } @@ -101,17 +111,18 @@ string_compare (string const &a, string const &b) } #include "std-vector.hh" + 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); } @@ -119,3 +130,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; +}