lilypond-0.0.32
[lilypond.git] / flower / string.hh
blob420d5f959d85491096ad798696d3e034cd4bd5e8
1 /*
3 FILE : string.hh -- implement String inline helper classes,
4 and declare stringclass.
7 Rehacked by HWN 3/nov/95
8 removed String & 's
9 introduced Class String_handle
12 #ifndef STRING_HH
13 #define STRING_HH
16 #include <string.h>
17 #include <iostream.h>
18 #include <Rational.h>
20 #include "stringutil.hh"
22 /** the smart string class.
24 Intuitive string class. provides
26 ref counting through #String_handle#
28 conversion from bool, int, double, char *, char.
30 conversion to int, upcase, downcase
33 printable.
35 indexing (pos, posAny, lastPos)
37 cutting (left, right, mid)
39 concat (+=, +)
41 signed comparison (<, >, ==, etc)
43 No operator[] is provided, since this would be enormously slow. If needed,
44 convert to const char *.
46 class String
48 protected:
49 String_handle data; // should derive String from String_handle?
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 );
58 /// "ccccc"
59 String( char c, int n = 1 );
61 /// String s( 10 );
62 String( int i );
64 /// 'true' or 'false'
65 String(bool );
67 /// String s( 3.14, 6, '#' );
68 String ( double f , const char *fmt =0);
69 String( int i, int n, char c = ' ' );
71 /// return a "new"-ed copy of contents
72 char *copy_array() const; // return a "new"-ed copy of contents
74 const char *cptr() const;
75 const char *ptr() { return ((const String *)this)->cptr(); }
77 /// return the data. Don't use for writing the data.
78 operator const char *() const { return cptr(); }
80 String operator =( const String & source ) { data = source.data; return *this; }
82 /// concatenate s
83 void operator += (const char *s) { data += s; }
84 void operator += (String s);
86 char operator []( int n ) const { return data[n]; }
88 /// return n leftmost chars
89 String left( int n ) const;
91 /// return n rightmost chars
92 String right( int n ) const;
94 /// convert this to upcase
95 String upper();
97 /// convert this to downcase
98 String lower(); // & ??
100 /// return the "esrever" of *this
101 String reversed() const;
104 /// return a piece starting at pos (first char = pos 1), length n
105 String mid(int pos, int n ) const;
107 /// cut out a middle piece, return remainder
108 String nomid(int pos, int n ) const;
110 /// signed comparison, analogous to strcmp;
111 static int compare(const String& s1,const String& s2);
113 /// index of rightmost c
114 int lastPos( char c) const;
116 /// index of rightmost element of string
117 int lastPos( const char* string ) const;
119 /** index of leftmost c.
120 RETURN:
121 0 if not found, else index + 1
123 int pos(char c ) const;
124 int pos(const char* string ) const;
125 int posAny(const char* string ) const;
128 /// provide Stream output
129 void printOn(ostream& os) const;
131 /// convert to an integer
132 long value() const;
134 /// convert to a double
135 double fvalue() const;
137 /// the length of the string
138 int len() const;
142 #include "compare.hh"
144 instantiate_compare(const String &, String::compare);
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);
150 inline bool operator==(const char *s1, String s2)
152 return String(s1)==s2;
154 inline bool operator!=(String s1, const char *s2 ) {
155 return s1!=String(s2);
157 inline bool operator!=(const char *s1,String s2) {
158 return String(s2) !=s1;
162 inline String
163 operator + (String s1, String s2)
165 s1 += s2;
166 return s1;
169 inline ostream &
170 operator << ( ostream& os, String d )
172 d.printOn(os);
173 return os;
177 String quoteString(String message, String quote);
179 #endif