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