]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
release: 1.3.0
[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
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   /// provide Stream output
145   void print_on (ostream& os) const;
146
147   /// the length of the string
148   int length_i () const;
149
150   /// convert to an integer
151   int value_i () const;
152
153   /// convert to a double
154   double value_f () const;
155 };
156
157 /*
158  better to clutter global namespace, than suffer *ugh, ugh, ugh*
159  implicit conversions.
160
161  it might be cool to have no type-checking at all in a language,
162  but once there is, having this silently circumvented is a nightmare.
163
164  whenever implicit conversions seem necessary (e.g. operator << ()),
165  use Scalar as the generic type iso String.
166  */
167
168 /// for completeness (=handy)
169 inline String to_str (String s) { return s; }
170 /// "cccc"
171 String to_str (char c, int n = 1);
172 String to_str (int i, char const* format = 0);
173 String to_str (double f , char const* format = 0);
174 String to_str (long  b);
175 String to_str (bool b);
176 String to_str (char const* format, ... );
177
178 /*
179   technically incorrect, but lets keep it here: this is a
180   catch all place for this stuff.
181   */
182   
183 #include "international.hh"
184
185
186 #include "compare.hh"
187 INSTANTIATE_COMPARE(String const &, String::compare_i);
188
189 #ifdef STRING_UTILS_INLINED
190 #ifndef INLINE
191 #define INLINE inline
192 #endif
193 #include "string.icc"
194 /* we should be resetting INLINE. oh well. */
195 #endif
196
197
198 // because char const* also has an operator ==, this is for safety:
199 inline bool operator==(String s1, char const* s2)
200 {
201   return s1 == String (s2);
202 }
203 inline bool operator==(char const* s1, String s2)
204 {
205   return String (s1)==s2;
206 }
207 inline bool operator!=(String s1, char const* s2 ) {
208   return s1!=String (s2);
209 }
210 inline bool operator!=(char const* s1,String s2) {
211   return String (s2) !=s1;
212 }
213
214 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
215
216 inline ostream &
217 operator << (ostream& os, String d)
218 {
219   d.print_on (os);
220   return os;
221 }
222
223
224
225 #endif