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