]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
release: 0.1.48
[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 /*
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
80   /// String s = "abc";
81   String (char const* source); 
82   String (Byte const* byte_C, int length_i); 
83     
84   /// "ccccc"
85   String (char c, int n = 1);
86
87   String (int i , char const *fmt=0);
88   String (double f , char const* fmt =0);
89   /// 'true' or 'false'
90   String (bool);
91
92   ///  return a "new"-ed copy of contents
93   Byte* copy_byte_p () const; //  return a "new"-ed copy of contents
94
95   char const* ch_C () const;
96   Byte const* byte_C () const;
97   char* ch_l ();
98   Byte* byte_l ();
99
100   String &operator =(String const & source);
101
102   /// concatenate s
103   void operator += (char const* s) { strh_ += s; }
104   void operator += (String s);
105
106   bool empty_b  () const;
107 #if 0
108   /** is the string empty?
109
110     Ugh-ugh-thank-you-cygnus.  W32 barfs on this
111    */
112   operator bool  () const;
113   {
114     return length_i  (); 
115   }
116 #endif
117   void append (String);
118   void prepend (String);
119
120   /**
121     Return a char.  UNSAFE because it may change strlen () result
122    */
123   char &operator [](int n);
124   char operator [](int n) const;
125
126   /// return n leftmost chars
127   String left_str (int n) const;
128
129   /// return n rightmost chars
130   String right_str (int n) const;
131
132   /// return uppercase of *this
133   String upper_str () const;
134
135   /// return lowercase of *this
136   String lower_str () const;
137
138   /// return the "esrever" of *this
139   String reversed_str () const;
140
141
142   /// return a piece starting at index_i (first char = index_i 0), length n
143   String cut (int index_i, int n) const;
144
145   /// cut out a middle piece, return remainder
146   String nomid_str (int index_i, int n) const;
147
148   /// signed comparison,  analogous to memcmp;
149   static int compare_i (String const & s1,const  String& s2);
150         
151   /// index of rightmost c 
152   int index_last_i (char c) const;
153
154   /// index of rightmost element of string  (???)
155   int index_last_i (char const* string) const;
156
157   /// index of leftmost #c#
158   int index_i (char c) const;
159
160
161   /// index of leftmost occurance of STRING
162   int index_i (String) const;
163   int index_any_i (String) const;
164
165   void to_upper ();
166   void to_lower ();
167   /// provide Stream output
168   void print_on (ostream& os) const;
169
170   /// the length of the string
171   int length_i () const;
172
173   /// convert to an integer
174   int value_i () const;
175
176   /// convert to a double
177   double value_f () const;
178 };
179
180 #include "compare.hh"
181 INSTANTIATE_COMPARE(String const &, String::compare_i);
182
183 #ifdef STRING_UTILS_INLINED
184 #ifndef INLINE
185 #define INLINE inline
186 #endif
187 #include "string.icc"
188 /* we should be resetting INLINE. oh well. */
189 #endif
190
191
192 // because char const* also has an operator ==, this is for safety:
193 inline bool operator==(String s1, char const* s2)
194 {
195   return s1 == String (s2);
196 }
197 inline bool operator==(char const* s1, String s2)
198 {
199   return String (s1)==s2;
200 }
201 inline bool operator!=(String s1, char const* s2 ) {
202   return s1!=String (s2);
203 }
204 inline bool operator!=(char const* s1,String s2) {
205   return String (s2) !=s1;
206 }
207
208 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
209
210 inline ostream &
211 operator << (ostream& os, String d)
212 {
213   d.print_on (os);
214   return os;
215 }
216
217
218
219 #endif