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