3 FILE : string.hh -- implement String inline helper classes,
4 and declare stringclass.
7 Rehacked by HWN 3/nov/95
9 introduced Class String_handle
20 #include "stringutil.hh"
22 /// the smart string class.
26 String_handle data; // should derive String from String_handle?
31 /** needed because other constructors are provided.*/
34 String( const char* source );
37 String( char c, int n = 1 );
45 /// String s( 3.14, 6, '#' );
46 String ( double f , const char *fmt =0);
47 String( int i, int n, char c = ' ' );
49 /// return a "new"-ed copy of contents
50 char *copy_array() const; // return a "new"-ed copy of contents
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(); }
57 String operator =( const String & source ) { data = source.data; return *this; }
60 void operator += (const char *s) { data += s; }
61 void operator += (String s);
63 char operator []( int n ) const { return data[n]; }
65 /// return n leftmost chars
66 String left( int n ) const;
68 /// return n rightmost chars
69 String right( int n ) const;
71 /// convert this to upcase
74 /// convert this to downcase
75 String lower(); // & ??
77 /// return the "esrever" of *this
78 String reversed() const;
81 /// return a piece starting at pos (first char = pos 1), length n
82 String mid(int pos, int n ) const;
84 /// cut out a middle piece, return remainder
85 String nomid(int pos, int n ) const;
87 /// signed comparison, analogous to strcmp;
88 int compare( const char* s ) const;
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;
95 /// index of leftmost c
96 int pos(char c ) const;
99 0 if not found, else index + 1
101 int pos(const char* string ) const;
102 int posAny(const char* string ) const;
105 /// provide Stream output
106 void printOn(ostream& os) const;
108 /// convert to an integer
111 /// convert to a double
112 double fvalue() const;
114 /// the length of the string
117 /// DO NOT MAKE THIS INTO AN OPERATOR
118 bool to_bool() const;
119 /** perl -like string to bool conversion
124 Intuitive string class. provides
126 ref counting thru #String_handle#
128 conversion from bool, int, double, char *, char.
130 conversion to int, upcase, downcase
135 indexing (pos, posAny, lastPos)
137 cutting (left, right, mid)
141 signed comparison (<, >, ==, etc)
143 No operator[] is provided, since this would be enormously slow. If needed,
144 convert to const char *.
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);}
157 operator + (String s1, String s2)
164 operator << ( ostream& os, String d )
171 String quoteString(String message, String quote);