]> git.donarmstrong.com Git - lilypond.git/blob - flower/string.cc
release: 0.1.9
[lilypond.git] / flower / string.cc
1 /*
2
3  string.cc - implement String
4  
5  (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen
6
7  */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <assert.h>
13 #include <string.h>
14
15 #include "string.hh"
16 #include "libc-extension.hh"
17 #include "string-convert.hh"
18
19 #ifdef STRING_DEBUG
20 void* mymemmove (void* dest, void const* src, size_t n);
21 #define memmove mymemmove
22 #endif
23
24 // return array, alloced with new.
25 Byte*
26 String::copy_byte_p() const
27 {
28   Byte const* src = strh_.byte_C();
29   Byte* dest = new Byte[strh_.length_i() + 1];
30   memcpy (dest, src, strh_.length_i() + 1);
31   return dest;    
32 }
33 void
34 String::print_on (ostream& os) const
35 {
36   if (!strh_.is_binary_bo())
37       os << ch_C();
38   else
39         for ( int i = 0; i < length_i(); i++)
40             os << (Byte)(*this)[ i ];
41 }
42 \f
43 /*
44   copying, constructing.
45  */
46 String&
47 String::operator = (String const&source)
48 {
49   strh_ = source.strh_;
50   return *this;
51 }
52
53
54 String::String (Rational r)
55 {
56   *this = String_convert::rational_str (r);
57 }
58
59 String::String (double f, char const* fmt)
60 {
61   *this= String_convert::double_str (f,fmt);
62 }
63
64 String::String (char c,  int n)
65 {
66   *this = String_convert::char_str (c,n);
67 }
68
69 /**
70   @see
71   String_convert::int_str
72  */
73 String::String (int i, char const * format)
74 {
75   *this = String_convert::int_str (i,format);
76 }
77
78 String::String (bool b)
79 {
80   *this = (char const*) (b ? "true" : "false");
81 }
82
83 String::String (char const* source)
84 {   
85   assert (source);    
86   strh_ = source;    
87 }
88
89 String::String (Byte const* byte_l, int length_i)
90 {   
91   strh_.set (byte_l, length_i);    
92 }
93 \f
94 void
95 String::append (String s)
96 {
97   strh_.append (s.byte_C(), s.length_i());
98 }
99 void
100 String::operator +=(String s)
101 {
102   append (s);
103 }
104
105 void
106 String::prepend (String s)
107 {
108   s += *this;
109   *this = s;
110 }
111
112 int
113 String::length_i() const
114 {
115   return strh_.length_i();
116 }
117
118 Byte const*
119 String::byte_C() const
120 {
121   return strh_.byte_C();
122 }
123
124 char const*
125 String::ch_C() const
126 {
127   return strh_.ch_C();
128 }
129
130 Byte*
131 String::byte_l()
132 {
133   return strh_.byte_l();
134 }
135
136 char*
137 String::ch_l()
138 {
139   return strh_.ch_l();
140 }
141
142 /**
143   Do a signed comparison,  analogous to memcmp;
144  */
145 int
146 String::compare_i (String const& s1, String const& s2) 
147 {
148   Byte const* p1 = s1.byte_C();
149   Byte const* p2 = s2.byte_C();
150   if ( p1 == p2)
151         return 0;
152
153   int i1 = s1.length_i();
154   int i2 = s2.length_i();
155
156   int result=  memcmp (p1, p2, i1 <? i2);
157   return result ? result : i1-i2;
158 }
159
160 \f
161 int
162 String::index_last_i (char const c) const
163 {
164   if ( !length_i()) 
165         return -1;
166
167   char const* me = strh_.ch_C();
168   char const* p = memrchr (me, length_i(), c);
169   if ( p)
170         return p - me;
171   return -1;
172 }
173
174 int
175 String::index_last_i (char const* string) const // UGK!
176 {
177   assert (false);               // broken
178   int length = strlen (string); // ugrh
179   if ( !length_i() || !length) 
180         return -1;
181   
182   int next_i = index_i (string);
183   if ( next_i == -1)
184         return -1;
185   
186   int index_i = 0;
187   while (next_i >= 0) 
188     {
189         index_i += next_i;
190         next_i = right_str (length_i() - index_i - length).index_i (string );
191     }
192   return index_i;
193 }
194
195 /** find  a character.
196
197   @return
198   the index of the leftmost character #c# (0 <= return < length_i()),
199   or   -1 if not found. 
200
201   ? should return length_i()?, as in string.left_str (index_i (delimiter))
202 */
203 int
204 String::index_i (char c) const
205 {
206   char const* me = strh_.ch_C();
207   char const* p = (char const *) memchr (me,c,  length_i());
208   if ( p)
209         return p - me;
210   return -1;
211 }
212
213 /**
214   find the substring.
215
216   @return
217   index of leftmost occurrence of #searchfor#
218  */
219 int
220 String::index_i (String searchfor) const
221 {
222   char const* me = strh_.ch_C();
223   char const* p = (char const *) memmem (
224         me, length_i(), searchfor.ch_C(), searchfor.length_i ());
225   
226   if ( p)
227         return p - me;
228   else
229         return -1;
230 }
231
232 /** find chars of a set.
233
234   @return
235   the index of the leftmost occurance of an element of #set#
236   */
237 int
238 String::index_any_i (String set) const
239 {
240   int n = length_i();
241   if ( !n)
242         return -1;
243
244   void const * me_l = (void const *) strh_.ch_C();
245   for (int i=0; i  < set.length_i(); i++) 
246     {
247         char * found=(char*) memchr (me_l, set[i], n );
248         if (found) 
249           {
250             return found - me_l;
251           }
252     }
253   return -1;
254 }
255 \f
256 String
257 String::left_str (int n) const
258 {
259   if (n >= length_i())
260         return *this;
261
262   String retval;        
263   if (n < 1)
264       return retval;
265   
266   retval = *this;
267   retval.strh_.trunc (n);
268   return retval;
269 }
270
271 String
272 String::right_str (int n) const
273 {
274   if (n > length_i())
275         return *this;
276   
277   if ( n < 1)
278       return "";
279   
280   return String (strh_.byte_C() + length_i() - n, n); 
281 }
282
283
284 String
285 String::nomid_str (int index_i, int n) const
286 {
287   if ( index_i < 0) 
288     {
289         n += index_i;
290         index_i = 0;
291     }
292   if ( n <= 0)
293         return *this;
294   
295   return
296         left_str (index_i)   +
297         right_str (length_i() - index_i - n) ;
298 }
299
300 /*
301   proposal: change to "cut()"
302  */
303 String
304 String::mid_str (int index_i, int n) const
305 {
306   if (index_i <0) 
307     {
308         n += index_i;
309         index_i=0;
310     }
311   
312   if ( !length_i() || ( index_i < 0) || ( index_i >= length_i () ) || ( n < 1 ) )
313         return String();
314
315   if ( ( n > length_i()) ||  ( index_i + n > length_i () ) )
316         n = length_i() - index_i;
317
318   return String (byte_C() + index_i, n);
319 }
320 \f
321 String
322 String::upper_str() const
323 {
324   String str = *this;
325   str.to_upper();
326   return str;
327 }
328 void
329 String::to_upper()
330 {
331   char *s = (char*)strh_.byte_l();
332   strnupr (s ,length_i());
333 }
334
335 void
336 String::to_lower()
337 {
338   char* s = strh_.ch_l();
339   strnlwr (s,length_i());    
340 }
341
342
343 String 
344 String::lower_str() const
345 {
346   String str = *this;
347   str.to_lower();
348   return str;
349 }
350 String 
351 String::reversed_str() const
352 {
353   String str = *this;
354   strrev (str.byte_l(), str.length_i ());
355   return str;    
356 }
357
358 int
359 String::value_i() const
360 {
361   return String_convert::dec2_i (*this);
362 }
363
364 double
365 String::value_f() const
366 {
367   return String_convert::dec2_f (*this);
368 }
369
370