]> git.donarmstrong.com Git - lilypond.git/blob - flower/string.hh
release: 0.0.6
[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 *ptr() const;
53     const char *ptr() { return ((const String *)this)->ptr(); }
54     /// return the data. Don't use for writing the data.
55     operator const char *() const { return ptr(); }
56     
57     String operator =( const String & source ) { data = source.data; return *this; }
58
59     /// concatenate s
60     void operator += (const char *s) { data += s; }
61     void operator += (String s);
62
63     char operator []( int n ) const { return data[n]; }
64
65     /// return n leftmost chars
66     String left( int n ) const;
67
68     /// return n rightmost chars
69     String right( int n ) const;
70
71     /// convert this to upcase
72     String upper();
73
74     /// convert this to downcase
75     String lower(); // & ??
76
77     /// return the "esrever" of *this
78     String reversed() const;
79
80
81     /// return a piece starting at pos (first char = pos 1), length n
82     String mid(int pos,  int n ) const;
83
84     /// cut out a middle piece, return remainder
85     String nomid(int pos, int n ) const;
86
87     /// signed comparison,  analogous to strcmp;
88     int  compare( const char* s ) const;
89
90     /// index of rightmost c 
91     int lastPos( char c) const;
92     /// index of rightmost element of string 
93     int lastPos( const char* string ) const;
94
95     /// index of leftmost c
96     int pos(char c ) const;
97     /**
98     RETURN:
99     0 if not found, else index + 1
100     */
101     int pos(const char* string ) const;
102     int posAny(const char* string ) const;
103
104
105     /// provide Stream output
106     void printOn(ostream& os) const;
107
108     /// convert to an integer
109     long value() const;
110
111     /// convert to a double
112     double fvalue() const;
113     
114     /// the length of the string
115     int len() const;
116
117     /// DO NOT MAKE THIS INTO AN OPERATOR
118     bool to_bool() const;
119     /** perl -like string to bool conversion
120      */
121 };
122 /** 
123
124   Intuitive string class. provides 
125
126   ref counting thru #String_handle#
127
128   conversion from bool, int, double, char *, char.
129
130   conversion to int, upcase, downcase
131
132
133   printable. 
134
135   indexing (pos, posAny, lastPos)
136
137   cutting (left, right, mid)
138
139   concat (+=, +)
140
141   signed comparison (<, >, ==, etc)
142
143   No operator[] is provided, since this would be enormously  slow. If needed,
144   convert to const char *. 
145 */
146
147
148 // because const char* also has an operator ==, this is for safety:
149 inline bool operator==(String s1, String s2){    return !(s1.compare(s2));}
150 inline bool operator==(String s1, const char *s2){ return !(s1.compare(s2));}
151 inline bool operator==(const char *s1, String s2){ return !(s2.compare(s1));}
152 inline bool operator!=(String s1, const char *s2  ) {    return s1.compare(s2);}
153 inline bool operator!=(const char *s1,String s2) {    return s2.compare(s1);}
154 inline bool operator!=(String s1, String s2  ) {    return s1.compare(s2);}
155
156 inline String
157 operator  + (String s1, String  s2)
158 {
159     s1 += s2;
160     return s1;
161 }
162
163 inline ostream &
164 operator << ( ostream& os, String d )
165 {
166     d.printOn(os);
167     return os;
168 }
169
170
171 String quoteString(String message, String quote);
172
173 #endif