]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
Typo fix.
[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.h>
18 #else
19 class ostream;
20 #endif
21 #endif
22
23 #include "arithmetic-operator.hh"
24 #include "flower-proto.hh"
25 #include "string-handle.hh"
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
94   void append (String);
95   void prepend (String);
96
97   /**
98     Return a char.  UNSAFE because it may change strlen () result
99    */
100   char &operator [] (int n);
101   char operator [] (int n) const;
102
103   /// return n leftmost chars
104   String left_str (int n) const;
105
106   /// return n rightmost chars
107   String right_str (int n) const;
108
109   /// return uppercase of *this
110   String upper_str () const;
111
112   /// return lowercase of *this
113   String lower_str () const;
114
115   /// return the "esrever" of *this
116   String reversed_str () const;
117
118   /// return a piece starting at index_i (first char = index_i 0), length n
119   String cut_str (int index_i, int n) const;
120
121   /// cut out a middle piece, return remainder
122   String nomid_str (int index_i, int n) const;
123
124   /// signed comparison,  analogous to memcmp;
125   static int compare_i (String const & s1,const  String& s2);
126         
127   /// index of rightmost c 
128   int index_last_i (char c) const;
129
130   /// index of rightmost element of string (???)
131   int index_last_i (char const* string) const;
132
133   int index_i (char c) const;
134
135   /// index of leftmost occurance of STRING
136   int index_i (String) const;
137
138
139   int index_any_i (String) const;
140
141   void to_upper ();
142   void to_lower ();
143
144 #ifdef STREAM_SUPPORT
145   /// provide Stream output
146   void print_on (ostream& os) const;
147 #endif
148   
149   /// the length of the string
150   int length_i () const;
151
152   /// convert to an integer
153   int value_i () const;
154
155   /// convert to a double
156   double value_f () 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_str (String s) { return s; }
172 /// "cccc"
173 String to_str (char c, int n = 1);
174 String to_str (int i, char const* format = 0);
175 String to_str (double f , char const* format = 0);
176 String to_str (long  b);
177 String to_str (bool b);
178 String to_str (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_i);
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