]> git.donarmstrong.com Git - lilypond.git/blob - flower/string.hh
release: 0.0.34
[lilypond.git] / flower / 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 <string.h>
15 #include <iostream.h>
16 #include <Rational.h>
17
18 #include "stringhandle.hh"
19
20 /** 
21  
22   Intuitive string class. provides 
23 \begin{itemize}
24 \item
25   ref counting through #String_handle#
26 \item
27
28   conversion from bool, int, double, char *, char.
29 \item
30
31   conversion to int, upcase, downcase
32
33 \item
34
35   printable. 
36
37 \item
38   indexing (pos, posAny, lastPos)
39
40 \item
41   cutting (left, right, mid)
42
43 \item
44   concat (+=, +)
45
46 \item
47   signed comparison (<, >, ==, etc)
48
49 \item
50   No operator[] is provided, since this would be enormously  slow. If needed,
51   convert to const char *.
52 \end{itemize}
53 */
54 class String
55 {
56 protected:
57     String_handle strh_; // should derive String from String_handle?
58
59 public:
60
61     /**   init to "". needed because other constructors are provided.*/
62     String() {  }                  
63     String(Rational);
64     /// String s = "abc";
65     String( const char* source ); 
66
67     String( Byte const* l_byte_c, int length_i ); 
68     
69     /// "ccccc"
70     String( char c, int n = 1 );
71
72     /// String s( 10 );
73     String( int i );
74
75     /// 'true' or 'false'
76     String(bool );
77
78     /// String s( 3.14, 6, '#' );
79     String ( double f , const char *fmt =0);
80     String(  int i,  int n,  char c = ' ' );
81
82     ///  return a "new"-ed copy of contents
83     Byte* copy_byte_p() const; //  return a "new"-ed copy of contents
84
85     char const* ch_c_l() const;
86     Byte const* byte_c_l() const;
87     char* ch_l();
88     Byte* byte_l();
89
90     /// deprecated; use ch_c_l()
91     operator const char *() const { return ch_c_l(); }
92     
93     String operator =( const String & source ) { strh_ = source.strh_; return *this; }
94
95     /// concatenate s
96     void operator += (char const* s) { strh_ += s; }
97     void operator += (String s);
98
99     char operator []( int n ) const { return strh_[n]; }
100
101     /// return n leftmost chars
102     String left( int n ) const;
103
104     /// return n rightmost chars
105     String right( int n ) const;
106
107     /// convert this to upcase
108     String upper();
109
110     /// convert this to downcase
111     String lower(); // & ??
112
113     /// return the "esrever" of *this
114     String reversed() const;
115
116
117     /// return a piece starting at pos (first char = pos 1), ength n
118     String mid(int pos,  int n ) const;
119
120     /// cut out a middle piece, return remainder
121     String nomid(int pos, int n ) const;
122
123     /// signed comparison,  analogous to memcmp;
124     static int compare(const String& s1,const  String& s2);
125         
126     /// index of rightmost c 
127     int lastPos( char c) const;
128
129     /// index of rightmost element of string 
130     int lastPos( const char* string ) const;
131
132     /**
133       index of leftmost c.
134       
135     @return
136     0 if not found, else index + 1
137     */
138     int pos(char c ) const;
139     int pos(const char* string ) const;
140     int posAny(const char* string ) const;
141
142
143     /// provide Stream output
144     void printOn(ostream& os) const;
145
146     /// convert to an integer
147     long value() const;
148
149     /// convert to a double
150     double fvalue() const;
151     
152     /// the length of the string
153     int length_i() const;
154
155     // deprecated 
156     int len() const {
157         return length_i();
158     }
159
160 };
161
162 #include "compare.hh"
163
164 instantiate_compare(const String &, String::compare);
165
166 // because const char* also has an operator ==, this is for safety:
167 inline bool operator==(String s1, const char *s2){
168     return s1 == String(s2);
169 }
170 inline bool operator==(const char *s1, String s2)
171 {
172     return String(s1)==s2;
173 }
174 inline bool operator!=(String s1, const char *s2  ) {
175     return s1!=String(s2);
176 }
177 inline bool operator!=(const char *s1,String s2) {
178     return String(s2) !=s1;
179 }
180
181
182 inline String
183 operator  + (String s1, String  s2)
184 {
185     s1 += s2;
186     return s1;
187 }
188
189 inline ostream &
190 operator << ( ostream& os, String d )
191 {
192     d.printOn(os);
193     return os;
194 }
195
196
197 String quoteString(String message, String quote);
198
199 #include "stringconversion.hh"
200
201 #endif