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