lilypond-0.0.1
[lilypond.git] / flower / string.hh
blob145fea8da05164d735ddc2d8d313b3ffad5465a5
1 /*
3 FILE : string.hh -- implement String inline helpclasses,
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>
19 //#include "globals.hh"
20 #include "stringutil.hh"
22 // whugh
23 #ifdef CENTRAL_OBJECT
24 #include "sortable.hh"
25 #define String__mpp String : public Sortable
26 #else
27 #define String__mpp String
28 #endif
30 /// the smart string class.
31 class String__mpp
33 protected:
34 String_handle data; // should derive String from String_handle?
36 public:
38 #ifdef CENTRAL_OBJECT // everything derived from Sortable object
39 virtual int operator ==( const Sortable& test ) const;
40 virtual int operator &&( const Object& test ) const;
41 virtual int operator >( const Sortable& test ) const;
42 #endif
44 /// init to ""
45 String() { }
46 /** needed because other constructors are provided.*/
48 /// String s = "abc";
49 String( const char* source );
51 /// "ccccc"
52 String( char c, int n = 1 );
54 /// String s( 10 );
55 String( int i );
57 /// 'true' or 'false'
58 String(bool );
60 /// String s( 3.14, 6, '#' );
61 String ( double f , const char *fmt =0);
62 String( int i, int n, char c = ' ' );
64 /// return a "new"-ed copy of contents
65 char *copy_array() const; // return a "new"-ed copy of contents
67 /// return the data. Don't use for writing the data.
68 virtual operator const char *() const; // virtual???
70 String operator =( const String & source ) { data = source.data; return *this; }
72 /// concatenate s
73 void operator += (const char *s) { data += s; }
74 void operator += (String s);
76 char operator []( int n ) const { return data[n]; }
78 /// return n leftmost chars
79 String left( int n ) const;
81 /// return n rightmost chars
82 String right( int n ) const;
84 /// convert this to upcase
85 String upper();
87 /// convert this to downcase
88 String lower(); // & ??
90 /// return the "esrever" of *this
91 String reversed() const;
94 /// return a piece starting at pos (first char = pos 1), length n
95 String mid(int pos, int n ) const;
97 /// cut out a middle piece, return remainder
98 String nomid(int pos, int n ) const;
100 /// signed comparison, analogous to strcmp;
101 int compare( const char* s ) const;
103 /// index of rightmost c
104 int lastPos( char c) const;
105 /// index of rightmost element of string
106 int lastPos( const char* string ) const;
108 /// index of leftmost c
109 int pos(char c ) const;
111 RETURN:
112 0 if not found, else index + 1
114 int pos(const char* string ) const;
115 int posAny(const char* string ) const;
118 /// provide Stream output
119 void printOn(ostream& os) const;
121 /// convert to an integer
122 long value() const;
124 /// convert to a double
125 double fvalue() const;
127 /// the length of the string
128 int len() const;
130 /**
132 Intuitive string class. provides
134 ref counting thru #String_handle#
136 conversion from bool, int, double, char *, char.
138 conversion to int, upcase, downcase
141 printable.
143 indexing (pos, posAny, lastPos)
145 cutting (left, right, mid)
147 concat (+=, +)
149 signed comparison (<, >, ==, etc)
151 No operator[] is provided, since this would be enormously slow. If needed,
152 convert to const char *.
156 // because const char* also has an operator ==, this is for safety:
157 inline bool operator==(String s1, String s2){ return !(s1.compare(s2));}
158 inline bool operator==(String s1, const char *s2){ return !(s1.compare(s2));}
159 inline bool operator==(const char *s1, String s2){ return !(s2.compare(s1));}
160 inline bool operator!=(String s1, const char *s2 ) { return s1.compare(s2);}
161 inline bool operator!=(const char *s1,String s2) { return s2.compare(s1);}
162 inline bool operator!=(String s1, String s2 ) { return s1.compare(s2);}
164 inline String
165 operator + (String s1, String s2)
167 s1 += s2;
168 return s1;
171 inline ostream &
172 operator << ( ostream& os, String d )
174 d.printOn(os);
175 return os;
179 String quoteString(String message, String quote);
181 #endif