]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
* flower
[lilypond.git] / flower / include / string.hh
1 /*
2   FILE   : string.hh -- declare String
3
4   Rehacked by HWN 3/nov/95
5   removed String & 's
6   introduced Class String_handle
7 */
8
9 #ifndef STRING_HH
10 #define STRING_HH
11
12 // too darn slow with gcc3
13 #ifdef STREAM_SUPPORT
14 #if (__GNUC__ > 2)
15 #include <iostream>
16 #else
17 class ostream;
18 #endif
19 #endif
20
21 #include "arithmetic-operator.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_string, right_string, mid_string)
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 class String
57 {
58 protected:
59   String_handle strh_;
60
61   bool null_terminated ();
62
63 public:
64
65   /** init to empty string. This is needed because other
66       constructors are provided.*/
67   String ();
68
69   /// String s = "abc";
70   String (char const *source);
71   String (Byte const *byte, int length_i);
72
73   ///  return "new"-ed copy of contents
74   Byte *get_copy_byte () const;
75   char *get_copy_str0 () const;
76
77   char const *to_str0 () const;
78   Byte const *to_bytes () const;
79   char *get_str0 ();
80   Byte *get_bytes ();
81
82   String &operator= (String const &source);
83
84   /// concatenate s
85   void operator+= (char const *s) { strh_ += s; }
86   void operator+= (String s);
87
88   bool is_empty () const;
89
90   void append (String);
91   void prepend (String);
92
93   /**
94      Return a char.  UNSAFE because it may change strlen () result
95   */
96   char &operator[] (int n);
97   char operator[] (int n) const;
98
99   /// return n leftmost chars
100   String left_string (int n) const;
101
102   /// return n rightmost chars
103   String right_string (int n) const;
104
105   /// return the "esrever" of *this
106   void reverse ();
107
108   /// return a piece starting at index (first char = index_i 0), length n
109   String cut_string (int index_i, int n) const;
110
111   /// cut out a middle piece, return remainder
112   String nomid_string (int index_i, int n) const;
113
114   /// signed comparison,  analogous to memcmp;
115   static int compare (String const &s1, const String &s2);
116
117   /// index of rightmost c 
118   int index_last (char c) const;
119
120   /// index of rightmost element of string (???)
121   int index_last (char const *string) const;
122
123   int index (char c) const;
124
125   /// index of leftmost occurance of STRING
126   int index (String) const;
127
128   int index_any (String) const;
129
130   void to_upper ();
131   void to_lower ();
132
133 #ifdef STREAM_SUPPORT
134   /// provide Stream output
135   void print_on (ostream& os) const;
136 #endif
137
138   /// the length of the string
139   int length () const;
140
141   /// convert to an integer
142   int to_int () const;
143
144   /// convert to a double
145   double to_double () const;
146
147   void substitute_char (char text, String sub);
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_string (String s) { return s; }
163 /// "cccc"
164 String to_string (char c, int n = 1);
165 String to_string (int i, char const *format = 0);
166 String to_string (double f, char const *format = 0);
167 String to_string (long b);
168 String to_string (bool b);
169 String to_string (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 #include "compare.hh"
179 INSTANTIATE_COMPARE (String const &, String::compare);
180
181 #ifdef STRING_UTILS_INLINED
182 #ifndef INLINE
183 #define INLINE inline
184 #endif
185 #include "string.icc"
186 /* we should be resetting INLINE. oh well. */
187 #endif
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 {
200   return s1 != String (s2);
201 }
202 inline bool operator!= (char const *s1, String s2)
203 {
204   return String (s2) !=s1;
205 }
206
207 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
208 #ifdef STREAM_SUPPORT
209 ostream &operator<< (ostream& os, String d);
210 #endif
211
212 #endif