lilypond-0.0.35
[lilypond.git] / flower / string.hh
bloba253b7ca613a6859f0d9e67531f0a920464bf1f0
1 /*
3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
6 removed String & 's
7 introduced Class String_handle
8 */
10 #ifndef STRING_HH
11 #define STRING_HH
14 #include <string.h>
15 #include <iostream.h>
16 #include <Rational.h>
18 #include "stringhandle.hh"
20 /**
22 Intuitive string class. provides
23 \begin{itemize}
24 \item
25 ref counting through #String_handle#
26 \item
28 conversion from bool, int, double, char *, char.
29 \item
31 conversion to int, upcase, downcase
33 \item
35 printable.
37 \item
38 indexing (pos, posAny, lastPos)
40 \item
41 cutting (left, right, mid)
43 \item
44 concat (+=, +)
46 \item
47 signed comparison (<, >, ==, etc)
49 \item
50 No operator[] is provided, since this would be enormously slow. If needed,
51 convert to const char *.
52 \end{itemize}
54 class String
56 protected:
57 String_handle strh_; // should derive String from String_handle?
59 public:
61 /** init to "". needed because other constructors are provided.*/
62 String() { }
63 String(Rational);
64 /// String s = "abc";
65 String( const char* source );
67 String( Byte const* l_byte_c, int length_i );
69 /// "ccccc"
70 String( char c, int n = 1 );
72 /// String s( 10 );
73 String( int i );
75 /// 'true' or 'false'
76 String(bool );
78 /// String s( 3.14, 6, '#' );
79 String ( double f , const char *fmt =0);
80 String( int i, int n, char c = ' ' );
82 /// return a "new"-ed copy of contents
83 Byte* copy_byte_p() const; // return a "new"-ed copy of contents
85 char const* ch_c_l() const;
86 Byte const* byte_c_l() const;
87 char* ch_l();
88 Byte* byte_l();
90 /// deprecated; use ch_c_l()
91 operator const char *() const { return ch_c_l(); }
93 String operator =( const String & source ) { strh_ = source.strh_; return *this; }
95 /// concatenate s
96 void operator += (char const* s) { strh_ += s; }
97 void operator += (String s);
99 char operator []( int n ) const { return strh_[n]; }
101 /// return n leftmost chars
102 String left( int n ) const;
104 /// return n rightmost chars
105 String right( int n ) const;
107 /// convert this to upcase
108 String upper();
110 /// convert this to downcase
111 String lower(); // & ??
113 /// return the "esrever" of *this
114 String reversed() const;
117 /// return a piece starting at pos (first char = pos 1), ength n
118 String mid(int pos, int n ) const;
120 /// cut out a middle piece, return remainder
121 String nomid(int pos, int n ) const;
123 /// signed comparison, analogous to memcmp;
124 static int compare(const String& s1,const String& s2);
126 /// index of rightmost c
127 int lastPos( char c) const;
129 /// index of rightmost element of string
130 int lastPos( const char* string ) const;
133 index of leftmost c.
135 @return
136 0 if not found, else index + 1
138 int pos(char c ) const;
139 int pos(const char* string ) const;
140 int posAny(const char* string ) const;
143 /// provide Stream output
144 void printOn(ostream& os) const;
146 /// convert to an integer
147 long value() const;
149 /// convert to a double
150 double fvalue() const;
152 /// the length of the string
153 int length_i() const;
155 // deprecated
156 int len() const {
157 return length_i();
162 #include "compare.hh"
164 instantiate_compare(const String &, String::compare);
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);
170 inline bool operator==(const char *s1, String s2)
172 return String(s1)==s2;
174 inline bool operator!=(String s1, const char *s2 ) {
175 return s1!=String(s2);
177 inline bool operator!=(const char *s1,String s2) {
178 return String(s2) !=s1;
182 inline String
183 operator + (String s1, String s2)
185 s1 += s2;
186 return s1;
189 inline ostream &
190 operator << ( ostream& os, String d )
192 d.printOn(os);
193 return os;
197 String quoteString(String message, String quote);
199 #include "stringconversion.hh"
201 #endif