]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/std-string.cc
Refactor fret diagrams for simpler calls
[lilypond.git] / flower / std-string.cc
index 0fdc96f5f6d78b5fbf6c890f79be1bb886cb2d70..285c51883c5d1fc0a7727816a3e99ab0e5da2e03 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2006  Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 2006--2009  Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #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;
 }
 
@@ -99,3 +109,38 @@ string_compare (string const &a, string const &b)
 {
   return a.compare (b);
 }
+
+#include "std-vector.hh"
+
+vector<string>
+string_split (string str, char c)
+{
+  ssize i = str.find (c);
+
+  vector<string> a;
+  while (i != NPOS)
+    {
+      string s = str.substr (0, i);
+      a.push_back (s);
+      i ++;
+      str = str.substr (i);
+      i = str.find (c);
+    }
+  if (str.length ())
+    a.push_back (str);
+  return a;
+}
+
+string
+string_join (vector<string> const &strs, string infix)
+{
+  string result;
+  for (vsize i = 0; i < strs.size (); i ++)
+    {
+      if (i)
+       result += infix;
+      result += strs[i];
+    }
+
+  return result;
+}