]> git.donarmstrong.com Git - lilypond.git/blob - flower/std-string.cc
Run grand-replace (issue 3765)
[lilypond.git] / flower / std-string.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2014  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
23 string
24 to_string (const string &s)
25 {
26   return s;
27 }
28
29 string
30 to_string (char c, int n)
31 {
32   return string (max (n, 0), c);
33 }
34
35 string
36 to_string (double f, char const *format)
37 {
38   return String_convert::double_string (f, format);
39 }
40
41 string
42 to_string (int i, char const *format)
43 {
44   return String_convert::int_string (i, format);
45 }
46
47 string
48 to_string (bool b)
49 {
50   return String_convert::bool_string (b);
51 }
52
53 string
54 to_string (long b)
55 {
56   return String_convert::long_string (b);
57 }
58
59 string
60 to_string (long unsigned b)
61 {
62   return String_convert::unsigned_long_string (b);
63 }
64
65 string
66 to_string (unsigned u)
67 {
68   return String_convert::unsigned_string (u);
69 }
70
71 string
72 to_string (I64 b, char const *format)
73 {
74   return String_convert::i64_string (b, format);
75 }
76
77 string
78 to_string (char const *format, ...)
79 {
80   va_list args;
81   va_start (args, format);
82   string str = String_convert::vform_string (format, args);
83   va_end (args);
84   return str;
85 }
86
87 /*
88   TODO: this O(n^2) in #occurences of find, due to repeated copying.
89  */
90 string &
91 replace_all (string *str, string const &find, string const &replace)
92 {
93   ssize len = find.length ();
94   ssize replen = replace.length ();
95   for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + replen))
96     * str = str->replace (i, len, replace);
97   return *str;
98 }
99
100 string &
101 replace_all (string *str, char find, char replace)
102 {
103   for (ssize i = str->find (find); i != NPOS; i = str->find (find, i + 1))
104     (*str)[i] = replace;
105   return *str;
106 }
107
108 char *
109 string_copy (const string &s)
110 {
111   ssize len = s.length ();
112   char *dest = new char[len + 1];
113   copy (s.begin (), s.end (), dest);
114   dest[len] = 0;
115
116   return dest;
117 }
118
119 int
120 string_compare (string const &a, string const &b)
121 {
122   return a.compare (b);
123 }
124
125 #include "std-vector.hh"
126
127 vector<string>
128 string_split (string str, char c)
129 {
130   ssize i = str.find (c);
131
132   vector<string> a;
133   while (i != NPOS)
134     {
135       string s = str.substr (0, i);
136       a.push_back (s);
137       i++;
138       str = str.substr (i);
139       i = str.find (c);
140     }
141   if (str.length ())
142     a.push_back (str);
143   return a;
144 }
145
146 string
147 string_join (vector<string> const &strs, const string &infix)
148 {
149   string result;
150   for (vsize i = 0; i < strs.size (); i++)
151     {
152       if (i)
153         result += infix;
154       result += strs[i];
155     }
156
157   return result;
158 }