]> git.donarmstrong.com Git - lilypond.git/blob - flower/std-string.cc
5dc5a3dce8630f5e134fba02bcf7f0eccfe7826e
[lilypond.git] / flower / std-string.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2015  Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "std-string.hh"
21 #include "string-convert.hh"
22 #include "std-vector.hh"
23
24 using std::string;
25 using std::vector;
26
27 string
28 to_string (const string &s)
29 {
30   return s;
31 }
32
33 string
34 to_string (char c, int n)
35 {
36   return string (std::max (n, 0), c);
37 }
38
39 string
40 to_string (double f, char const *format)
41 {
42   return String_convert::double_string (f, format);
43 }
44
45 string
46 to_string (int i, char const *format)
47 {
48   return String_convert::int_string (i, format);
49 }
50
51 string
52 to_string (bool b)
53 {
54   return String_convert::bool_string (b);
55 }
56
57 string
58 to_string (long b)
59 {
60   return String_convert::long_string (b);
61 }
62
63 string
64 to_string (long unsigned b)
65 {
66   return String_convert::unsigned_long_string (b);
67 }
68
69 string
70 to_string (unsigned u)
71 {
72   return String_convert::unsigned_string (u);
73 }
74
75 string
76 to_string (I64 b, char const *format)
77 {
78   return String_convert::i64_string (b, format);
79 }
80
81 string
82 to_string (char const *format, ...)
83 {
84   va_list args;
85   va_start (args, format);
86   string str = String_convert::vform_string (format, args);
87   va_end (args);
88   return str;
89 }
90
91 /*
92   TODO: this O(n^2) in #occurences of find, due to repeated copying.
93  */
94 string &
95 replace_all (string *str, string const &find, string const &replace)
96 {
97   ssize len = find.length ();
98   ssize replen = replace.length ();
99   for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + replen))
100     * str = str->replace (i, len, replace);
101   return *str;
102 }
103
104 string &
105 replace_all (string *str, char find, char replace)
106 {
107   for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + 1))
108     (*str)[i] = replace;
109   return *str;
110 }
111
112 char *
113 string_copy (const string &s)
114 {
115   ssize len = s.length ();
116   char *dest = new char[len + 1];
117   copy (s.begin (), s.end (), dest);
118   dest[len] = 0;
119
120   return dest;
121 }
122
123 vector<string>
124 string_split (string str, char c)
125 {
126   ssize i = str.find (c);
127
128   vector<string> a;
129   while (i != NPOS)
130     {
131       string s = str.substr (0, i);
132       a.push_back (s);
133       i++;
134       str = str.substr (i);
135       i = str.find (c);
136     }
137   if (str.length ())
138     a.push_back (str);
139   return a;
140 }
141
142 string
143 string_join (vector<string> const &strs, const string &infix)
144 {
145   string result;
146   for (vsize i = 0; i < strs.size (); i++)
147     {
148       if (i)
149         result += infix;
150       result += strs[i];
151     }
152
153   return result;
154 }