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 *cptr() const;
53 const char *ptr() { return ((const String *)this)->cptr(); }
55 /// return the data. Don't use for writing the data.
56 operator const char *() const { return cptr(); }
58 String operator =( const String & source ) { data = source.data; return *this; }
61 void operator += (const char *s) { data += s; }
62 void operator += (String s);
64 char operator []( int n ) const { return data[n]; }
66 /// return n leftmost chars
67 String left( int n ) const;
69 /// return n rightmost chars
70 String right( int n ) const;
72 /// convert this to upcase
75 /// convert this to downcase
76 String lower(); // & ??
78 /// return the "esrever" of *this
79 String reversed() const;
82 /// return a piece starting at pos (first char = pos 1), length n
83 String mid(int pos, int n ) const;
85 /// cut out a middle piece, return remainder
86 String nomid(int pos, int n ) const;
88 /// signed comparison, analogous to strcmp;
89 int compare( const char* s ) const;
91 /// index of rightmost c
92 int lastPos( char c) const;
93 /// index of rightmost element of string
94 int lastPos( const char* string ) const;
96 /// index of leftmost c
97 int pos(char c ) const;
100 0 if not found, else index + 1
102 int pos(const char* string ) const;
103 int posAny(const char* string ) const;
106 /// provide Stream output
107 void printOn(ostream& os) const;
109 /// convert to an integer
112 /// convert to a double
113 double fvalue() const;
115 /// the length of the string
121 Intuitive string class. provides
123 ref counting thru #String_handle#
125 conversion from bool, int, double, char *, char.
127 conversion to int, upcase, downcase
132 indexing (pos, posAny, lastPos)
134 cutting (left, right, mid)
138 signed comparison (<, >, ==, etc)
140 No operator[] is provided, since this would be enormously slow. If needed,
141 convert to const char *.
145 // because const char* also has an operator ==, this is for safety:
146 inline bool operator==(String s1, String s2){ return !(s1.compare(s2));}
147 inline bool operator==(String s1, const char *s2){ return !(s1.compare(s2));}
148 inline bool operator==(const char *s1, String s2){ return !(s2.compare(s1));}
149 inline bool operator!=(String s1, const char *s2 ) { return s1.compare(s2);}
150 inline bool operator!=(const char *s1,String s2) { return s2.compare(s1);}
151 inline bool operator!=(String s1, String s2 ) { return s1.compare(s2);}
154 operator + (String s1, String s2)
161 operator << ( ostream& os, String d )
168 String quoteString(String message, String quote);