lilypond-0.1.57
[lilypond.git] / flower / string.cc
blob65279ce0370549f4fef27267218f7bef051c067a
1 /*
3 string.cc - implement String
5 (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen
7 */
9 #ifndef _GNU_SOURCE // we want memmem
10 #define _GNU_SOURCE
11 #endif
13 #include <stdlib.h>
14 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
19 #include "string.hh"
20 #include "libc-extension.hh"
21 #include "string-convert.hh"
23 #ifdef STRING_DEBUG
24 void* mymemmove (void* dest, void const* src, size_t n);
25 #define memmove mymemmove
26 #endif
28 // return array, alloced with new.
29 Byte*
30 String::copy_byte_p() const
32 Byte const* src = strh_.byte_C();
33 Byte* dest = new Byte[strh_.length_i() + 1];
34 memcpy (dest, src, strh_.length_i() + 1);
35 return dest;
37 void
38 String::print_on (ostream& os) const
40 if (!strh_.is_binary_bo())
41 os << ch_C();
42 else
43 for (int i = 0; i < length_i(); i++)
44 os << (Byte)(*this)[ i ];
48 copying, constructing.
50 String&
51 String::operator = (String const&source)
53 strh_ = source.strh_;
54 return *this;
57 String::String (double f, char const* fmt)
59 *this= String_convert::double_str (f,fmt);
62 String::String (char c, int n)
64 *this = String_convert::char_str (c,n);
67 /**
68 @see
69 String_convert::int_str
71 String::String (int i, char const * format)
73 *this = String_convert::int_str (i,format);
76 String::String (bool b)
78 *this = (char const*) (b ? "true" : "false");
81 String::String (char const* source)
83 assert (source);
84 strh_ = source;
87 String::String (Byte const* byte_l, int length_i)
89 strh_.set (byte_l, length_i);
92 void
93 String::append (String s)
95 strh_.append (s.byte_C(), s.length_i());
97 void
98 String::operator +=(String s)
100 append (s);
103 void
104 String::prepend (String s)
106 s += *this;
107 *this = s;
111 String::length_i() const
113 return strh_.length_i();
116 Byte const*
117 String::byte_C() const
119 return strh_.byte_C();
122 char const*
123 String::ch_C() const
125 return strh_.ch_C();
128 Byte*
129 String::byte_l()
131 return strh_.byte_l();
134 char*
135 String::ch_l()
137 return strh_.ch_l();
140 bool
141 String::empty_b () const
143 return !length_i ();
146 Do a signed comparison, analogous to memcmp;
149 String::compare_i (String const& s1, String const& s2)
151 Byte const* p1 = s1.byte_C();
152 Byte const* p2 = s2.byte_C();
153 if (p1 == p2)
154 return 0;
157 don't forget the terminating '\0'
159 int f = (s1.length_i() <? s2.length_i());
160 int cmp_length = 1+ f;
161 return memcmp (p1, p2, cmp_length);
166 String::index_last_i (char const c) const
168 if (!length_i())
169 return -1;
171 char const* me = strh_.ch_C();
172 char const* p = memrchr ((Byte*)me, length_i(), c);
173 if (p)
174 return p - me;
175 return -1;
179 String::index_last_i (char const* string) const // UGK!
181 assert (false); // broken
182 int length = strlen (string); // ugrh
183 if (!length_i() || !length)
184 return -1;
186 int next_i = index_i (string);
187 if (next_i == -1)
188 return -1;
190 int index_i = 0;
191 while (next_i >= 0)
193 index_i += next_i;
194 next_i = right_str (length_i() - index_i - length).index_i (string );
196 return index_i;
199 /** find a character.
201 @return
202 the index of the leftmost character #c# (0 <= return < length_i()),
203 or -1 if not found.
205 ? should return length_i()?, as in string.left_str (index_i (delimiter))
208 String::index_i (char c) const
210 char const* me = strh_.ch_C();
211 char const* p = (char const *) memchr (me,c, length_i());
212 if (p)
213 return p - me;
214 return -1;
218 find a substring.
220 @return
221 1 index of leftmost occurrence of #searchfor#
224 String::index_i (String searchfor) const
226 char const* me = strh_.ch_C();
228 char const* p = (char const *)
229 memmem (me, length_i(), searchfor.ch_C(), searchfor.length_i ());
231 if (p)
232 return p - me;
233 else
234 return -1;
237 /** find chars of a set.
239 @return
240 the index of the leftmost occurance of an element of #set#
243 String::index_any_i (String set) const
245 int n = length_i();
246 if (!n)
247 return -1;
249 void const * me_l = (void const *) strh_.ch_C();
250 for (int i=0; i < set.length_i(); i++)
252 char * found=(char*) memchr (me_l, set[i], n );
253 if (found)
255 return found - me_l;
258 return -1;
261 String
262 String::left_str (int n) const
264 if (n >= length_i())
265 return *this;
267 String retval;
268 if (n < 1)
269 return retval;
271 retval = *this;
272 retval.strh_.trunc (n);
273 return retval;
276 String
277 String::right_str (int n) const
279 if (n > length_i())
280 return *this;
282 if (n < 1)
283 return "";
285 return String (strh_.byte_C() + length_i() - n, n);
289 String
290 String::nomid_str (int index_i, int n) const
292 if (index_i < 0)
294 n += index_i;
295 index_i = 0;
297 if (n <= 0)
298 return *this;
300 return
301 left_str (index_i) +
302 right_str (length_i() - index_i - n) ;
305 String
306 String::cut (int index_i, int n) const
308 if (index_i <0)
310 n += index_i;
311 index_i=0;
314 if (!length_i() || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
315 return String();
317 if ((n > length_i()) || (index_i + n > length_i () ) )
318 n = length_i() - index_i;
320 return String (byte_C() + index_i, n);
323 String
324 String::upper_str() const
326 String str = *this;
327 str.to_upper();
328 return str;
330 void
331 String::to_upper()
333 char *s = (char*)strh_.byte_l();
334 strnupr (s ,length_i());
337 void
338 String::to_lower()
340 char* s = strh_.ch_l();
341 strnlwr (s,length_i());
345 String
346 String::lower_str() const
348 String str = *this;
349 str.to_lower();
350 return str;
352 String
353 String::reversed_str() const
355 String str = *this;
356 strrev (str.byte_l(), str.length_i ());
357 return str;
361 String::value_i() const
363 return String_convert::dec2_i (*this);
366 double
367 String::value_f() const
369 return String_convert::dec2_f (*this);