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