]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
release: 1.3.148
[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 "arithmetic-operator.hh"
15 #include "flower-proto.hh"
16 #include "string-handle.hh"
17
18 class ostream;
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
66   /// String s = "abc";
67   String (char const* source); 
68   String (Byte const* byte_C, int length_i); 
69     
70   ///  return "new"-ed copy of contents
71   Byte* copy_byte_p () const;
72   char* copy_ch_p () const;
73
74   char const* ch_C () const;
75   Byte const* byte_C () const;
76   char* ch_l ();
77   Byte* byte_l ();
78
79   String &operator = (String const & source);
80
81   /// concatenate s
82   void operator += (char const* s) { strh_ += s; }
83   void operator += (String s);
84
85   bool empty_b () const;
86
87   void append (String);
88   void prepend (String);
89
90   /**
91     Return a char.  UNSAFE because it may change strlen () result
92    */
93   char &operator [] (int n);
94   char operator [] (int n) const;
95
96   /// return n leftmost chars
97   String left_str (int n) const;
98
99   /// return n rightmost chars
100   String right_str (int n) const;
101
102   /// return uppercase of *this
103   String upper_str () const;
104
105   /// return lowercase of *this
106   String lower_str () const;
107
108   /// return the "esrever" of *this
109   String reversed_str () const;
110
111   /// return a piece starting at index_i (first char = index_i 0), length n
112   String cut_str (int index_i, int n) const;
113
114   /// cut out a middle piece, return remainder
115   String nomid_str (int index_i, int n) const;
116
117   /// signed comparison,  analogous to memcmp;
118   static int compare_i (String const & s1,const  String& s2);
119         
120   /// index of rightmost c 
121   int index_last_i (char c) const;
122
123   /// index of rightmost element of string (???)
124   int index_last_i (char const* string) const;
125
126   int index_i (char c) const;
127
128   /// index of leftmost occurance of STRING
129   int index_i (String) const;
130
131
132   int index_any_i (String) const;
133
134   void to_upper ();
135   void to_lower ();
136
137   /// provide Stream output
138   void print_on (ostream& os) const;
139
140   /// the length of the string
141   int length_i () const;
142
143   /// convert to an integer
144   int value_i () const;
145
146   /// convert to a double
147   double value_f () const;
148 };
149
150 /*
151  better to clutter global namespace, than suffer *ugh, ugh, ugh*
152  implicit conversions.
153
154  it might be cool to have no type-checking at all in a language,
155  but once there is, having this silently circumvented is a nightmare.
156
157  whenever implicit conversions seem necessary (e.g. operator << ()),
158  use Scalar as the generic type iso String.
159  */
160
161 /// for completeness (=handy)
162 inline String to_str (String s) { return s; }
163 /// "cccc"
164 String to_str (char c, int n = 1);
165 String to_str (int i, char const* format = 0);
166 String to_str (double f , char const* format = 0);
167 String to_str (long  b);
168 String to_str (bool b);
169 String to_str (char const* format, ... );
170
171 /*
172   technically incorrect, but lets keep it here: this is a
173   catch all place for this stuff.
174   */
175   
176 #include "international.hh"
177
178
179 #include "compare.hh"
180 INSTANTIATE_COMPARE (String const &, String::compare_i);
181
182 #ifdef STRING_UTILS_INLINED
183 #ifndef INLINE
184 #define INLINE inline
185 #endif
186 #include "string.icc"
187 /* we should be resetting INLINE. oh well. */
188 #endif
189
190
191 // because char const* also has an operator ==, this is for safety:
192 inline bool operator== (String s1, char const* s2)
193 {
194   return s1 == String (s2);
195 }
196 inline bool operator== (char const* s1, String s2)
197 {
198   return String (s1)==s2;
199 }
200 inline bool operator!= (String s1, char const* s2 ) {
201   return s1!=String (s2);
202 }
203 inline bool operator!= (char const* s1,String s2) {
204   return String (s2) !=s1;
205 }
206
207 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
208
209 ostream &operator << (ostream& os, String d);
210
211 #endif