]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
252fdb48f2b3888d8c7553a41962c693f10af1fb
[lilypond.git] / flower / include / string.hh
1 /*
2
3   FILE   : string.hh -- declare String
4  
5   Rehacked by HWN 3/nov/95
6   removed String & 's
7   introduced Class String_handle
8  */
9
10 #ifndef STRING_HH
11 #define STRING_HH
12
13
14 // too darn slow with gcc3
15 #ifdef STREAM_SUPPORT
16 #if ( __GNUC__ > 2 )
17 #include <iostream>
18 #else
19 class ostream;
20 #endif
21 #endif
22
23 #include "arithmetic-operator.hh"
24 #include "string-handle.hh"
25
26 /** 
27  
28   Intuitive string class. provides 
29 \begin{itemize}
30 \item
31   ref counting through #String_handle#
32 \item
33   conversion from bool, int, double, char* , char.  
34 \item
35   to be moved to String_convert:
36   conversion to int, upcase, downcase 
37
38 \item
39   printable. 
40
41 \item
42   indexing (index_i, index_any_i, last_index_i)
43
44 \item
45   cutting (left_string, right_string, mid_string)
46
47 \item
48   concat (+=, +)
49
50 \item
51   signed comparison (<, >, ==, etc)
52
53 \item
54   No operator[] is provided, since this would be enormously  slow. If needed,
55   convert to char const* .
56 \end{itemize}
57
58 */
59 class String
60 {
61 protected:
62   String_handle strh_; 
63
64   bool null_terminated ();
65     
66 public:
67
68   /** init to empty string. This is needed because other
69     constructors are provided.*/
70   String ();
71
72   /// String s = "abc";
73   String (char const* source); 
74   String (Byte const* byte, int length_i); 
75     
76   ///  return "new"-ed copy of contents
77   Byte* get_copy_byte () const;
78   char* get_copy_str0 () const;
79
80   char const* to_str0 () const;
81   Byte const* to_bytes () const;
82   char *get_str0 ();
83   Byte *get_bytes ();
84
85   String &operator = (String const & source);
86
87   /// concatenate s
88   void operator += (char const* s) { strh_ += s; }
89   void operator += (String s);
90
91   bool is_empty () const;
92
93   void append (String);
94   void prepend (String);
95
96   /**
97     Return a char.  UNSAFE because it may change strlen () result
98    */
99   char &operator [] (int n);
100   char operator [] (int n) const;
101
102   /// return n leftmost chars
103   String left_string (int n) const;
104
105   /// return n rightmost chars
106   String right_string (int n) const;
107
108   /// return uppercase of *this
109   String upper_string () const;
110   /// return lowercase of *this
111   String lower_string () const;
112
113   /// return the "esrever" of *this
114   String reversed_string () const;
115
116   /// return a piece starting at index (first char = index_i 0), length n
117   String cut_string (int index_i, int n) const;
118
119   /// cut out a middle piece, return remainder
120   String nomid_string (int index_i, int n) const;
121
122   /// signed comparison,  analogous to memcmp;
123   static int compare (String const & s1,const  String& s2);
124         
125   /// index of rightmost c 
126   int index_last (char c) const;
127
128   /// index of rightmost element of string (???)
129   int index_last (char const* string) const;
130
131   int index (char c) const;
132
133   /// index of leftmost occurance of STRING
134   int index (String) const;
135
136
137   int index_any (String) const;
138
139   void to_upper ();
140   void to_lower ();
141
142 #ifdef STREAM_SUPPORT
143   /// provide Stream output
144   void print_on (ostream& os) const;
145 #endif
146   
147   /// the length of the string
148   int length () const;
149
150   /// convert to an integer
151   int to_int () const;
152
153   /// convert to a double
154   double to_double () const;
155
156   String substituted (char text, String sub) const; 
157 };
158
159 /*
160  better to clutter global namespace, than suffer *ugh, ugh, ugh*
161  implicit conversions.
162
163  it might be cool to have no type-checking at all in a language,
164  but once there is, having this silently circumvented is a nightmare.
165
166  whenever implicit conversions seem necessary (e.g. operator << ()),
167  use Scalar as the generic type iso String.
168  */
169
170 /// for completeness (=handy)
171 inline String to_string (String s) { return s; }
172 /// "cccc"
173 String to_string (char c, int n = 1);
174 String to_string (int i, char const* format = 0);
175 String to_string (double f, char const* format = 0);
176 String to_string (long  b);
177 String to_string (bool b);
178 String to_string (char const* format, ... );
179
180 /*
181   technically incorrect, but lets keep it here: this is a
182   catch all place for this stuff.
183   */
184   
185 #include "international.hh"
186
187
188 #include "compare.hh"
189 INSTANTIATE_COMPARE (String const &, String::compare);
190
191 #ifdef STRING_UTILS_INLINED
192 #ifndef INLINE
193 #define INLINE inline
194 #endif
195 #include "string.icc"
196 /* we should be resetting INLINE. oh well. */
197 #endif
198
199
200 // because char const* also has an operator ==, this is for safety:
201 inline bool operator== (String s1, char const* s2)
202 {
203   return s1 == String (s2);
204 }
205 inline bool operator== (char const* s1, String s2)
206 {
207   return String (s1)==s2;
208 }
209 inline bool operator!= (String s1, char const* s2 ) {
210   return s1!=String (s2);
211 }
212 inline bool operator!= (char const* s1,String s2) {
213   return String (s2) !=s1;
214 }
215
216 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
217 #ifdef STREAM_SUPPORT
218 ostream &operator << (ostream& os, String d);
219 #endif
220
221 #endif