]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string.hh
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[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 using namespace std;
21
22 #include "arithmetic-operator.hh"
23 #include "string-handle.hh"
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_string, right_string, mid_string)
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 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, int length_i);
73
74   ///  return "new"-ed copy of contents
75   Byte *get_copy_byte () const;
76   char *get_copy_str0 () const;
77
78   char const *to_str0 () const;
79   Byte const *to_bytes () const;
80   char *get_str0 ();
81   Byte *get_bytes ();
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 is_empty () 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_string (int n) const;
102
103   /// return n rightmost chars
104   String right_string (int n) const;
105
106   /// return the "esrever" of *this
107   void reverse ();
108
109   /// return a piece starting at index (first char = index_i 0), length n
110   String cut_string (int index_i, int n) const;
111
112   /// cut out a middle piece, return remainder
113   String nomid_string (int index_i, int n) const;
114
115   /// signed comparison,  analogous to memcmp;
116   static int compare (String const &s1, const String &s2);
117
118   /// index of rightmost c 
119   int index_last (char c) const;
120
121   /// index of rightmost element of string (???)
122   int index_last (char const *string) const;
123
124   int index (char c) const;
125
126   /// index of leftmost occurance of STRING
127   int index (String) const;
128
129   int index_any (String) const;
130
131   void to_upper ();
132   void to_lower ();
133
134 #ifdef STREAM_SUPPORT
135   /// provide Stream output
136   void print_on (ostream &os) const;
137 #endif
138
139   /// the length of the string
140   int length () const;
141
142   /// convert to an integer
143   int to_int () const;
144
145   /// convert to a double
146   double to_double () const;
147
148   String substitute (String find, String replace);
149   String substitute (char find, char replace);
150 };
151
152 /*
153   better to clutter global namespace, than suffer *ugh, ugh, ugh*
154   implicit conversions.
155
156   it might be cool to have no type-checking at all in a language,
157   but once there is, having this silently circumvented is a nightmare.
158
159   whenever implicit conversions seem necessary (e.g. operator << ()),
160   use Scalar as the generic type iso String.
161 */
162
163 /// for completeness (=handy)
164 String to_string (String s);
165 String to_string (char c, int n = 1);
166 String to_string (int i, char const *format = 0);
167 String to_string (double f, char const *format = 0);
168 String to_string (long b);
169 String to_string (bool b);
170 String to_string (char const *format, ...);
171
172 /*
173   technically incorrect, but lets keep it here: this is a
174   catch all place for this stuff.
175 */
176
177 #include "international.hh"
178 #include "compare.hh"
179
180 INSTANTIATE_COMPARE (String const &, String::compare);
181
182 #ifdef STRING_UTILS_INLINED
183 #ifndef INLINE
184 #define INLINE inline
185 #endif
186 #include "string.icc"
187 /* we should be resetting INLINE. oh well. */
188 #endif
189
190 // because char const* also has an operator ==, this is for safety:
191 bool operator == (String s1, char const *s2);
192 bool operator == (char const *s1, String s2);
193 bool operator != (String s1, char const *s2);
194 bool operator != (char const *s1, String s2);
195
196 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
197 #ifdef STREAM_SUPPORT
198 ostream &operator << (ostream &os, String d);
199 #endif
200
201 #endif