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