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