]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
release: 1.3.149
[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 #include <iostream.h> /* gcc 3.0 */
14 #include "arithmetic-operator.hh"
15 #include "flower-proto.hh"
16 #include "string-handle.hh"
17
18 /** 
19  
20   Intuitive string class. provides 
21 \begin{itemize}
22 \item
23   ref counting through #String_handle#
24 \item
25   conversion from bool, int, double, char* , char.  
26 \item
27   to be moved to String_convert:
28   conversion to int, upcase, downcase 
29
30 \item
31   printable. 
32
33 \item
34   indexing (index_i, index_any_i, last_index_i)
35
36 \item
37   cutting (left_str, right_str, mid_str)
38
39 \item
40   concat (+=, +)
41
42 \item
43   signed comparison (<, >, ==, etc)
44
45 \item
46   No operator[] is provided, since this would be enormously  slow. If needed,
47   convert to char const* .
48 \end{itemize}
49
50 */
51 class String
52 {
53 protected:
54   String_handle strh_; 
55
56   bool null_terminated ();
57     
58 public:
59
60   /** init to empty string. This is needed because other
61     constructors are provided.*/
62   String ();
63
64   /// String s = "abc";
65   String (char const* source); 
66   String (Byte const* byte_C, int length_i); 
67     
68   ///  return "new"-ed copy of contents
69   Byte* copy_byte_p () const;
70   char* copy_ch_p () const;
71
72   char const* ch_C () const;
73   Byte const* byte_C () const;
74   char* ch_l ();
75   Byte* byte_l ();
76
77   String &operator = (String const & source);
78
79   /// concatenate s
80   void operator += (char const* s) { strh_ += s; }
81   void operator += (String s);
82
83   bool empty_b () const;
84
85   void append (String);
86   void prepend (String);
87
88   /**
89     Return a char.  UNSAFE because it may change strlen () result
90    */
91   char &operator [] (int n);
92   char operator [] (int n) const;
93
94   /// return n leftmost chars
95   String left_str (int n) const;
96
97   /// return n rightmost chars
98   String right_str (int n) const;
99
100   /// return uppercase of *this
101   String upper_str () const;
102
103   /// return lowercase of *this
104   String lower_str () const;
105
106   /// return the "esrever" of *this
107   String reversed_str () const;
108
109   /// return a piece starting at index_i (first char = index_i 0), length n
110   String cut_str (int index_i, int n) const;
111
112   /// cut out a middle piece, return remainder
113   String nomid_str (int index_i, int n) const;
114
115   /// signed comparison,  analogous to memcmp;
116   static int compare_i (String const & s1,const  String& s2);
117         
118   /// index of rightmost c 
119   int index_last_i (char c) const;
120
121   /// index of rightmost element of string (???)
122   int index_last_i (char const* string) const;
123
124   int index_i (char c) const;
125
126   /// index of leftmost occurance of STRING
127   int index_i (String) const;
128
129
130   int index_any_i (String) const;
131
132   void to_upper ();
133   void to_lower ();
134
135   /// provide Stream output
136   void print_on (ostream& os) const;
137
138   /// the length of the string
139   int length_i () const;
140
141   /// convert to an integer
142   int value_i () const;
143
144   /// convert to a double
145   double value_f () const;
146 };
147
148 /*
149  better to clutter global namespace, than suffer *ugh, ugh, ugh*
150  implicit conversions.
151
152  it might be cool to have no type-checking at all in a language,
153  but once there is, having this silently circumvented is a nightmare.
154
155  whenever implicit conversions seem necessary (e.g. operator << ()),
156  use Scalar as the generic type iso String.
157  */
158
159 /// for completeness (=handy)
160 inline String to_str (String s) { return s; }
161 /// "cccc"
162 String to_str (char c, int n = 1);
163 String to_str (int i, char const* format = 0);
164 String to_str (double f , char const* format = 0);
165 String to_str (long  b);
166 String to_str (bool b);
167 String to_str (char const* format, ... );
168
169 /*
170   technically incorrect, but lets keep it here: this is a
171   catch all place for this stuff.
172   */
173   
174 #include "international.hh"
175
176
177 #include "compare.hh"
178 INSTANTIATE_COMPARE (String const &, String::compare_i);
179
180 #ifdef STRING_UTILS_INLINED
181 #ifndef INLINE
182 #define INLINE inline
183 #endif
184 #include "string.icc"
185 /* we should be resetting INLINE. oh well. */
186 #endif
187
188
189 // because char const* also has an operator ==, this is for safety:
190 inline bool operator== (String s1, char const* s2)
191 {
192   return s1 == String (s2);
193 }
194 inline bool operator== (char const* s1, String s2)
195 {
196   return String (s1)==s2;
197 }
198 inline bool operator!= (String s1, char const* s2 ) {
199   return s1!=String (s2);
200 }
201 inline bool operator!= (char const* s1,String s2) {
202   return String (s2) !=s1;
203 }
204
205 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
206
207 ostream &operator << (ostream& os, String d);
208
209 #endif