]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
24b975949a48e54b950f55cd485238807608a7db
[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 #include <string.h>
15 #include <iostream.h>
16 #include "arithmetic-operator.hh"
17 #include "fproto.hh"
18 #include "string-handle.hh"
19
20 /* Libg++ also has a String class.  Programming errors can lead to
21   confusion about which String is in use.  Uncomment the following if you have 
22   unexplained crashes after mucking with String
23   */
24
25 //  #define String FlowerString
26
27 /** 
28  
29   Intuitive string class. provides 
30 \begin{itemize}
31 \item
32   ref counting through #String_handle#
33 \item
34   conversion from bool, int, double, char* , char.  
35 \item
36   to be moved to String_convert:
37   conversion to int, upcase, downcase 
38
39 \item
40   printable. 
41
42 \item
43   indexing (index_i, index_any_i, last_index_i)
44
45 \item
46   cutting (left_str, right_str, mid_str)
47
48 \item
49   concat (+=, +)
50
51 \item
52   signed comparison (<, >, ==, etc)
53
54 \item
55   No operator[] is provided, since this would be enormously  slow. If needed,
56   convert to char const* .
57 \end{itemize}
58
59 */
60 class String
61 {
62 protected:
63   String_handle strh_; 
64
65   bool null_terminated ();
66     
67 public:
68
69   /** init to empty string. This is needed because other
70     constructors are provided.*/
71   String  ();
72
73   /// String s = "abc";
74   String (char const* source); 
75   String (Byte const* byte_C, int length_i); 
76     
77   ///  return "new"-ed copy of contents
78   Byte* copy_byte_p () const;
79   char* copy_ch_p() const;
80
81   char const* ch_C () const;
82   Byte const* byte_C () const;
83   char* ch_l ();
84   Byte* byte_l ();
85
86   String &operator =(String const & source);
87
88   /// concatenate s
89   void operator += (char const* s) { strh_ += s; }
90   void operator += (String s);
91
92   bool empty_b  () const;
93 #if 0
94   /** is the string empty?
95
96     Ugh-ugh-thank-you-cygnus.  W32 barfs on this
97    */
98   operator bool  () const;
99   {
100     return length_i  (); 
101   }
102 #endif
103   void append (String);
104   void prepend (String);
105
106   /**
107     Return a char.  UNSAFE because it may change strlen () result
108    */
109   char &operator [](int n);
110   char operator [](int n) const;
111
112   /// return n leftmost chars
113   String left_str (int n) const;
114
115   /// return n rightmost chars
116   String right_str (int n) const;
117
118   /// return uppercase of *this
119   String upper_str () const;
120
121   /// return lowercase of *this
122   String lower_str () const;
123
124   /// return the "esrever" of *this
125   String reversed_str () const;
126
127   /// return a piece starting at index_i (first char = index_i 0), length n
128   String cut_str (int index_i, int n) const;
129
130   /// cut out a middle piece, return remainder
131   String nomid_str (int index_i, int n) const;
132
133   /// signed comparison,  analogous to memcmp;
134   static int compare_i (String const & s1,const  String& s2);
135         
136   /// index of rightmost c 
137   int index_last_i (char c) const;
138
139   /// index of rightmost element of string  (???)
140   int index_last_i (char const* string) const;
141
142   int index_i (char c) const;
143
144   /// index of leftmost occurance of STRING
145   int index_i (String) const;
146
147
148   int index_any_i (String) const;
149
150   void to_upper ();
151   void to_lower ();
152
153   /// provide Stream output
154   void print_on (ostream& os) const;
155
156   /// the length of the string
157   int length_i () const;
158
159   /// convert to an integer
160   int value_i () const;
161
162   /// convert to a double
163   double value_f () const;
164 };
165
166 /*
167  better to clutter global namespace, than suffer *ugh, ugh, ugh*
168  implicit conversions.
169
170  it might be cool to have no type-checking at all in a language,
171  but once there is, having this silently circumvented is a nightmare.
172
173  whenever implicit conversions seem necessary (e.g. operator << ()),
174  use Scalar as the generic type iso String.
175  */
176
177 /// for completeness (=handy)
178 inline String to_str (String s) { return s; }
179 /// "cccc"
180 String to_str (char c, int n = 1);
181 String to_str (int i, char const* format = 0);
182 String to_str (double f , char const* format = 0);
183 String to_str (long  b);
184 String to_str (bool b);
185 String to_str (char const* format, ... );
186
187 /*
188   technically incorrect, but lets keep it here: this is a
189   catch all place for this stuff.
190   */
191   
192 #include "international.hh"
193
194
195 #include "compare.hh"
196 INSTANTIATE_COMPARE(String const &, String::compare_i);
197
198 #ifdef STRING_UTILS_INLINED
199 #ifndef INLINE
200 #define INLINE inline
201 #endif
202 #include "string.icc"
203 /* we should be resetting INLINE. oh well. */
204 #endif
205
206
207 // because char const* also has an operator ==, this is for safety:
208 inline bool operator==(String s1, char const* s2)
209 {
210   return s1 == String (s2);
211 }
212 inline bool operator==(char const* s1, String s2)
213 {
214   return String (s1)==s2;
215 }
216 inline bool operator!=(String s1, char const* s2 ) {
217   return s1!=String (s2);
218 }
219 inline bool operator!=(char const* s1,String s2) {
220   return String (s2) !=s1;
221 }
222
223 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
224
225 inline ostream &
226 operator << (ostream& os, String d)
227 {
228   d.print_on (os);
229   return os;
230 }
231
232
233
234 #endif