3 FILE : string.hh -- declare String
5 Rehacked by HWN 3/nov/95
7 introduced Class String_handle
13 #include <iostream.h> /* gcc 3.0 */
14 #include "arithmetic-operator.hh"
15 #include "flower-proto.hh"
16 #include "string-handle.hh"
20 Intuitive string class. provides
23 ref counting through #String_handle#
25 conversion from bool, int, double, char* , char.
27 to be moved to String_convert:
28 conversion to int, upcase, downcase
34 indexing (index_i, index_any_i, last_index_i)
37 cutting (left_str, right_str, mid_str)
43 signed comparison (<, >, ==, etc)
46 No operator[] is provided, since this would be enormously slow. If needed,
47 convert to char const* .
56 bool null_terminated ();
60 /** init to empty string. This is needed because other
61 constructors are provided.*/
65 String (char const* source);
66 String (Byte const* byte_C, int length_i);
68 /// return "new"-ed copy of contents
69 Byte* copy_byte_p () const;
70 char* copy_ch_p () const;
72 char const* ch_C () const;
73 Byte const* byte_C () const;
77 String &operator = (String const & source);
80 void operator += (char const* s) { strh_ += s; }
81 void operator += (String s);
83 bool empty_b () const;
86 void prepend (String);
89 Return a char. UNSAFE because it may change strlen () result
91 char &operator [] (int n);
92 char operator [] (int n) const;
94 /// return n leftmost chars
95 String left_str (int n) const;
97 /// return n rightmost chars
98 String right_str (int n) const;
100 /// return uppercase of *this
101 String upper_str () const;
103 /// return lowercase of *this
104 String lower_str () const;
106 /// return the "esrever" of *this
107 String reversed_str () const;
109 /// return a piece starting at index_i (first char = index_i 0), length n
110 String cut_str (int index_i, int n) const;
112 /// cut out a middle piece, return remainder
113 String nomid_str (int index_i, int n) const;
115 /// signed comparison, analogous to memcmp;
116 static int compare_i (String const & s1,const String& s2);
118 /// index of rightmost c
119 int index_last_i (char c) const;
121 /// index of rightmost element of string (???)
122 int index_last_i (char const* string) const;
124 int index_i (char c) const;
126 /// index of leftmost occurance of STRING
127 int index_i (String) const;
130 int index_any_i (String) const;
135 /// provide Stream output
136 void print_on (ostream& os) const;
138 /// the length of the string
139 int length_i () const;
141 /// convert to an integer
142 int value_i () const;
144 /// convert to a double
145 double value_f () const;
149 better to clutter global namespace, than suffer *ugh, ugh, ugh*
150 implicit conversions.
152 it might be cool to have no type-checking at all in a language,
153 but once there is, having this silently circumvented is a nightmare.
155 whenever implicit conversions seem necessary (e.g. operator << ()),
156 use Scalar as the generic type iso String.
159 /// for completeness (=handy)
160 inline String to_str (String s) { return s; }
162 String to_str (char c, int n = 1);
163 String to_str (int i, char const* format = 0);
164 String to_str (double f , char const* format = 0);
165 String to_str (long b);
166 String to_str (bool b);
167 String to_str (char const* format, ... );
170 technically incorrect, but lets keep it here: this is a
171 catch all place for this stuff.
174 #include "international.hh"
177 #include "compare.hh"
178 INSTANTIATE_COMPARE (String const &, String::compare_i);
180 #ifdef STRING_UTILS_INLINED
182 #define INLINE inline
184 #include "string.icc"
185 /* we should be resetting INLINE. oh well. */
189 // because char const* also has an operator ==, this is for safety:
190 inline bool operator== (String s1, char const* s2)
192 return s1 == String (s2);
194 inline bool operator== (char const* s1, String s2)
196 return String (s1)==s2;
198 inline bool operator!= (String s1, char const* s2 ) {
199 return s1!=String (s2);
201 inline bool operator!= (char const* s1,String s2) {
202 return String (s2) !=s1;
205 IMPLEMENT_ARITHMETIC_OPERATOR (String, +);
207 ostream &operator << (ostream& os, String d);