]> git.donarmstrong.com Git - lilypond.git/blob - flower/string.cc
release: 0.1.44
[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 bool 
143 String::empty_b () const
144 {
145   return !length_i ();
146 }
147 /**
148   Do a signed comparison,  analogous to memcmp;
149  */
150 int
151 String::compare_i (String const& s1, String const& s2) 
152 {
153   Byte const* p1 = s1.byte_C();
154   Byte const* p2 = s2.byte_C();
155   if (p1 == p2)
156     return 0;
157
158   /*
159     don't forget the terminating '\0'
160    */
161   int f = (s1.length_i() <? s2.length_i());
162   int cmp_length = 1+ f;
163   return memcmp (p1, p2, cmp_length);
164 }
165
166 \f
167 int
168 String::index_last_i (char const c) const
169 {
170   if (!length_i()) 
171     return -1;
172
173   char const* me = strh_.ch_C();
174   char const* p = memrchr ((Byte*)me, length_i(), c);
175   if (p)
176     return p - me;
177   return -1;
178 }
179
180 int
181 String::index_last_i (char const* string) const // UGK!
182 {
183   assert (false);               // broken
184   int length = strlen (string); // ugrh
185   if (!length_i() || !length) 
186     return -1;
187   
188   int next_i = index_i (string);
189   if (next_i == -1)
190     return -1;
191   
192   int index_i = 0;
193   while (next_i >= 0) 
194     {
195       index_i += next_i;
196       next_i = right_str (length_i() - index_i - length).index_i (string );
197     }
198   return index_i;
199 }
200
201 /** find  a character.
202
203   @return
204   the index of the leftmost character #c# (0 <= return < length_i()),
205   or   -1 if not found. 
206
207   ? should return length_i()?, as in string.left_str (index_i (delimiter))
208 */
209 int
210 String::index_i (char c) const
211 {
212   char const* me = strh_.ch_C();
213   char const* p = (char const *) memchr (me,c,  length_i());
214   if (p)
215     return p - me;
216   return -1;
217 }
218
219 /**
220   find a substring.
221
222   @return
223 1  index of leftmost occurrence of #searchfor#
224  */
225 int
226 String::index_i (String searchfor) const
227 {
228   char const* me = strh_.ch_C();
229
230   char const* p =     (char const *) 
231     memmem (me, length_i(), searchfor.ch_C(), searchfor.length_i ());
232   
233   if (p)
234     return p - me;
235   else
236     return -1;
237 }
238
239 /** find chars of a set.
240
241   @return
242   the index of the leftmost occurance of an element of #set#
243   */
244 int
245 String::index_any_i (String set) const
246 {
247   int n = length_i();
248   if (!n)
249     return -1;
250
251   void const * me_l = (void const *) strh_.ch_C();
252   for (int i=0; i  < set.length_i(); i++) 
253     {
254       char * found=(char*) memchr (me_l, set[i], n );
255       if (found) 
256         {
257           return found - me_l;
258         }
259     }
260   return -1;
261 }
262 \f
263 String
264 String::left_str (int n) const
265 {
266   if (n >= length_i())
267     return *this;
268
269   String retval;        
270   if (n < 1)
271     return retval;
272   
273   retval = *this;
274   retval.strh_.trunc (n);
275   return retval;
276 }
277
278 String
279 String::right_str (int n) const
280 {
281   if (n > length_i())
282     return *this;
283   
284   if (n < 1)
285     return "";
286   
287   return String (strh_.byte_C() + length_i() - n, n); 
288 }
289
290
291 String
292 String::nomid_str (int index_i, int n) const
293 {
294   if (index_i < 0) 
295     {
296       n += index_i;
297       index_i = 0;
298     }
299   if (n <= 0)
300     return *this;
301   
302   return
303     left_str (index_i)   +
304     right_str (length_i() - index_i - n) ;
305 }
306
307 String
308 String::cut (int index_i, int n) const
309 {
310   if (index_i <0) 
311     {
312       n += index_i;
313       index_i=0;
314     }
315   
316   if (!length_i() || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
317     return String();
318
319   if ((n > length_i()) ||  (index_i + n > length_i () ) )
320     n = length_i() - index_i;
321
322   return String (byte_C() + index_i, n);
323 }
324 \f
325 String
326 String::upper_str() const
327 {
328   String str = *this;
329   str.to_upper();
330   return str;
331 }
332 void
333 String::to_upper()
334 {
335   char *s = (char*)strh_.byte_l();
336   strnupr (s ,length_i());
337 }
338
339 void
340 String::to_lower()
341 {
342   char* s = strh_.ch_l();
343   strnlwr (s,length_i());    
344 }
345
346
347 String 
348 String::lower_str() const
349 {
350   String str = *this;
351   str.to_lower();
352   return str;
353 }
354 String 
355 String::reversed_str() const
356 {
357   String str = *this;
358   strrev (str.byte_l(), str.length_i ());
359   return str;    
360 }
361
362 int
363 String::value_i() const
364 {
365   return String_convert::dec2_i (*this);
366 }
367
368 double
369 String::value_f() const
370 {
371   return String_convert::dec2_f (*this);
372 }
373