*** empty log message ***
[lilypond.git] / flower / string.cc
blobc0e2e99c724202940ef18d001129184e3b388e21
1 /*
3 string.cc - implement String
5 (c) 1997--2000 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>
15 #include <assert.h>
16 #include <string.h>
17 #include <stdarg.h>
19 #include <iostream>
21 #include "string.hh"
22 #include "libc-extension.hh"
23 #include "string-convert.hh"
25 #ifdef STRING_DEBUG
26 void* mymemmove (void* dest, void const* src, size_t n);
27 #define memmove mymemmove
28 #endif
30 // return array, alloced with new.
31 Byte*
32 String::get_copy_byte () const
34 Byte const* src = strh_.to_bytes ();
35 Byte* dest = new Byte[strh_.length () + 1];
36 memcpy (dest, src, strh_.length () + 1);
37 return dest;
40 char*
41 String::get_copy_str0 () const
43 return (char*)get_copy_byte ();
48 copying, constructing.
50 String&
51 String::operator = (String const&source)
53 strh_ = source.strh_;
54 return *this;
58 String::String (Byte const* byte, int len_i)
60 strh_.set (byte, len_i);
63 /**
64 @see
65 String_convert::
67 String
68 to_string (char c, int n)
70 return String_convert::char_string (c, n);
73 String
74 to_string (double f, char const* format)
76 return String_convert::double_string (f, format);
79 String
80 to_string (int i, char const * format)
82 return String_convert::int_string (i, format);
85 String
86 to_string (bool b)
88 return String_convert::bool_string (b);
90 String
91 to_string (long b)
93 return String_convert::long_string (b);
96 String
97 to_string (char const* format, ... )
99 va_list args;
100 va_start (args, format);
101 String str = String_convert::vform_string (format, args);
102 va_end (args);
103 return str;
107 void
108 String::append (String s)
110 strh_.append (s.to_bytes (), s.length ());
112 void
113 String::operator += (String s)
115 append (s);
118 void
119 String::prepend (String s)
121 s += *this;
122 *this = s;
126 String::length () const
128 return strh_.length ();
131 Byte const*
132 String::to_bytes () const
134 return strh_.to_bytes ();
137 char const*
138 String::to_str0 () const
140 return strh_.to_str0 ();
143 Byte*
144 String::get_bytes ()
146 return strh_.get_bytes ();
149 char*
150 String::get_str0 ()
152 return strh_.get_str0 ();
155 bool
156 String::empty_b () const
158 return !length ();
161 Do a signed comparison, analogous to memcmp;
164 String::compare (String const& s1, String const& s2)
166 Byte const* p1 = s1.to_bytes ();
167 Byte const* p2 = s2.to_bytes ();
168 if (p1 == p2)
169 return 0;
172 don't forget the terminating '\0'
174 int f = (s1.length () <? s2.length ());
175 int cmp_length = 1+ f;
176 int i = memcmp (p1, p2, cmp_length);
177 return i;
182 String::index_last (char const c) const
184 if (!length ())
185 return -1;
187 char const* me = strh_.to_str0 ();
188 char const* p = (char const*)memrchr ((Byte*)me, length (), c);
189 if (p)
190 return p - me;
191 return -1;
195 String::index_last (char const* string) const // UGK!
197 assert (false); // broken
198 int len = strlen (string); // ugrh
199 if (!length () || !len)
200 return -1;
202 int next_i = index (string);
203 if (next_i == -1)
204 return -1;
206 int index_i = 0;
207 while (next_i >= 0)
209 index_i += next_i;
210 next_i = right_string (length () - index_i - len).index (string );
212 return index_i;
215 /** find a character.
217 @return
218 the index of the leftmost character #c# (0 <= return < length ()),
219 or -1 if not found.
221 ? should return length ()?, as in string.left_string (index (delimiter))
224 String::index (char c) const
226 char const* me = strh_.to_str0 ();
227 char const* p = (char const *) memchr (me,c, length ());
228 if (p)
229 return p - me;
230 return -1;
234 find a substring.
236 @return
237 1 index of leftmost occurrence of #searchfor#
240 String::index (String searchfor) const
242 char const* me = strh_.to_str0 ();
244 char const* p = (char const *)
245 memmem (me, length (), searchfor.to_str0 (), searchfor.length ());
247 if (p)
248 return p - me;
249 else
250 return -1;
253 /** find chars of a set.
255 @return
257 the index of the leftmost occurance of an element of #set#. -1 if
258 nothing is found.
263 String::index_any (String set) const
265 int n = length ();
266 if (!n)
267 return -1;
269 void const * me = (void const *) strh_.to_str0 ();
270 for (int i=0; i < set.length (); i++)
272 char * found= (char*) memchr (me, set[i], n );
273 if (found)
275 return found - (char const*)me;
278 return -1;
281 String
282 String::left_string (int n) const
284 if (n >= length ())
285 return *this;
287 String retval;
288 if (n < 1)
289 return retval;
291 retval = *this;
292 retval.strh_.trunc (n);
293 return retval;
296 String
297 String::right_string (int n) const
299 if (n > length ())
300 return *this;
302 if (n < 1)
303 return "";
305 return String (strh_.to_bytes () + length () - n, n);
309 String
310 String::nomid_string (int index_i, int n) const
312 if (index_i < 0)
314 n += index_i;
315 index_i = 0;
317 if (n <= 0)
318 return *this;
320 return
321 left_string (index_i) +
322 right_string (length () - index_i - n) ;
325 String
326 String::cut_string (int index_i, int n) const
328 if (index_i <0)
330 n += index_i;
331 index_i=0;
334 if (!length () || (index_i < 0) || (index_i >= length () ) || (n < 1 ) )
335 return String ();
337 if ((n > length ()) || (index_i + n > length () ) )
338 n = length () - index_i;
340 return String (to_bytes () + index_i, n);
343 String
344 String::upper_string () const
346 String str = *this;
347 str.to_upper ();
348 return str;
350 void
351 String::to_upper ()
353 char *s = (char*)strh_.get_bytes ();
354 strnupr (s ,length ());
357 void
358 String::to_lower ()
360 char* s = strh_.get_str0 ();
361 strnlwr (s,length ());
365 String
366 String::lower_string () const
368 String str = *this;
369 str.to_lower ();
370 return str;
372 String
373 String::reversed_string () const
375 String str = *this;
376 strrev (str.get_bytes (), str.length ());
377 return str;
381 String::to_int () const
383 return String_convert::dec2int (*this);
386 double
387 String::to_double () const
389 return String_convert::dec2double (*this);
392 #ifdef STREAM_SUPPORT
393 ostream &
394 operator << (ostream& os, String d)
396 d.print_on (os);
397 return os;
401 void
402 String::print_on (ostream& os) const
404 if (!strh_.is_binary_bo ())
405 os << to_str0 ();
406 else
407 for (int i = 0; i < length (); i++)
408 os << (Byte) (*this)[ i ];
410 #endif