]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
* buildscripts/analyse-cxx-log.py: new file. Read compile log to
[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   Byte const *to_bytes () const;
99   char *get_str0 ();
100   Byte *get_bytes ();
101
102   String &operator = (String const &source);
103
104   /// concatenate s
105   void operator += (char const *s) { strh_ += s; }
106   void operator += (String s);
107   
108   void append (String);
109   void prepend (String);
110
111   /**
112      Return a char.  UNSAFE because it may change strlen () result
113   */
114   char &operator [] (int n);
115   char operator [] (int n) const;
116
117   /// return n leftmost chars
118   String left_string (int n) const;
119
120   /// return n rightmost chars
121   String right_string (int n) const;
122
123   /// return the "esrever" of *this
124   void reverse ();
125
126   /// return a piece starting at index (first char = index_i 0), length n
127   String cut_string (int index_i, int n) const;
128
129   /// cut out a middle piece, return remainder
130   String nomid_string (int index_i, int n) const;
131
132   /// signed comparison,  analogous to memcmp;
133   static int compare (String const &s1, const String &s2);
134
135   /// index of rightmost element of string (???)
136   int index_last (char const *string) const;
137
138   int index_last (char c) const;
139   
140   int index (char c) const;
141
142   /// index of leftmost occurance of STRING
143   int index (String) const;
144
145   int index_any (String) const;
146
147   void to_upper ();
148   void to_lower ();
149
150 #ifdef STREAM_SUPPORT
151   /// provide Stream output
152   void print_on (ostream &os) const;
153 #endif
154
155   /// the length of the string
156   int length () const;
157
158   /// convert to an integer
159   int to_int () const;
160
161   /// convert to a double
162   double to_double () const;
163
164   String substitute (String find, String replace);
165   String substitute (char find, char replace);
166 };
167
168 /*
169   better to clutter global namespace, than suffer *ugh, ugh, ugh*
170   implicit conversions.
171
172   it might be cool to have no type-checking at all in a language,
173   but once there is, having this silently circumvented is a nightmare.
174
175   whenever implicit conversions seem necessary (e.g. operator << ()),
176   use Scalar as the generic type iso String.
177 */
178
179 /// for completeness (=handy)
180 String to_string (String s);
181 String to_string (char c, int n = 1);
182 String to_string (int i, char const *format = 0);
183 String to_string (double f, char const *format = 0);
184 String to_string (long b);
185 String to_string (bool b);
186 String to_string (char const *format, ...);
187
188 /*
189   technically incorrect, but lets keep it here: this is a
190   catch all place for this stuff.
191 */
192
193 #include "international.hh"
194 #include "compare.hh"
195
196 INSTANTIATE_COMPARE (String const &, String::compare);
197
198 #ifdef STRING_UTILS_INLINED
199 #ifndef INLINE
200 #define INLINE inline
201 #endif
202 #include "string.icc"
203 /* we should be resetting INLINE. oh well. */
204 #endif
205
206 // because char const* also has an operator ==, this is for safety:
207 bool operator == (String s1, char const *s2);
208 bool operator == (char const *s1, String s2);
209 bool operator != (String s1, char const *s2);
210 bool operator != (char const *s1, String s2);
211
212 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
213 #ifdef STREAM_SUPPORT
214 ostream &operator << (ostream &os, String d);
215 #endif
216
217 #endif