Update THANKS.
[lilypond.git] / flower / std-string.cc
blobec5ddffbd4bd05f013b666004938e7350a50ec15
1 /*
2 std-string.cc -- implement external interface for Std_String
4 source file of the GNU LilyPond music typesetter
6 (c) 2006--2008 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "std-string.hh"
10 #include "string-convert.hh"
12 string
13 to_string (string s)
15 return s;
18 string
19 to_string (char c, int n)
21 return string (max (n, 0), c);
24 string
25 to_string (double f, char const *format)
27 return String_convert::double_string (f, format);
30 string
31 to_string (int i, char const *format)
33 return String_convert::int_string (i, format);
36 string
37 to_string (bool b)
39 return String_convert::bool_string (b);
42 string
43 to_string (long b)
45 return String_convert::long_string (b);
48 string
49 to_string (long unsigned b)
51 return String_convert::unsigned_string (b);
54 string
55 to_string (unsigned u)
57 return String_convert::unsigned_string (u);
60 string
61 to_string (I64 b, char const *format)
63 return String_convert::i64_string (b, format);
66 string
67 to_string (char const *format, ...)
69 va_list args;
70 va_start (args, format);
71 string str = String_convert::vform_string (format, args);
72 va_end (args);
73 return str;
77 TODO: this O(n^2) in #occurences of find, due to repeated copying.
79 string &
80 replace_all (string *str, string const &find, string const &replace)
82 ssize len = find.length ();
83 for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + len))
84 *str = str->replace (i, len, replace);
85 return *str;
88 string &
89 replace_all (string *str, char find, char replace)
91 for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + 1))
92 (*str)[i] = replace;
93 return *str;
96 char *
97 string_copy (string s)
99 ssize len = s.length ();
100 char *dest = new char[len + 1];
101 copy (s.begin (), s.end (), dest);
102 dest[len] = 0;
104 return dest;
108 string_compare (string const &a, string const &b)
110 return a.compare (b);
113 #include "std-vector.hh"
115 vector<string>
116 string_split (string str, char c)
118 ssize i = str.find (c);
120 vector<string> a;
121 while (i != NPOS)
123 string s = str.substr (0, i);
124 a.push_back (s);
125 i ++;
126 str = str.substr (i);
127 i = str.find (c);
129 if (str.length ())
130 a.push_back (str);
131 return a;
134 string
135 string_join (vector<string> const &strs, string infix)
137 string result;
138 for (vsize i = 0; i < strs.size (); i ++)
140 if (i)
141 result += infix;
142 result += strs[i];
145 return result;