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