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