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