1
//------------------------------------------------------------------------------
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
5
// Author: Frank Dehne (frank@cs.vu.nl).
7
// TCM is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
12
// TCM is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
17
// You should have received a copy of the GNU General Public License
18
// along with TCM; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21
//-----------------------------------------------------------------------------
27
/// infinite length string class.
29
/*@Doc: {\large {\bf scope:} global} */
32
static const string EMPTY;
35
/// create empty string.
38
/// copy constructor. usage: string x = string
39
string(const string &s);
41
/// copy constructor. usage: string x = "abc"
42
string(const char* s);
44
/// copy constructor. usage: string x = 'a'
47
/// copy constructor (decimals). usage: string x = 12;
50
/// copy constructor (decimals). usage: string x = 12;
53
/// copy constructor (doubles). usage: string x = 1.2;
61
/// copy operator a la strcpy.
62
string &operator=(const string &s);
64
/// copy operator a la strcpy.
65
string &operator=(const char* s);
67
/// copy operator a la strcpy.
68
string &operator=(char c);
70
/// copy operator a la strcpy. s = n in decimal notation.
71
string &operator=(int n);
73
/// copy operator a la strcpy. s = n in decimal notation.
74
string &operator=(unsigned n);
76
/// copy operator a la strcpy. s = d in floating point notation.
77
string &operator=(double d);
80
/// concat operator a la strcat.
81
string &operator+=(const string &s);
83
/// concat operator a la strcat.
84
string &operator+=(const char* s);
86
/// concat. n in decimal notation is added.
87
string &operator+=(int n);
89
/// concat. n in decimal notation is added.
90
string &operator+=(unsigned n);
92
/// concat. d in floating point notation is added.
93
string &operator+=(double d);
95
/// concat operator a la strcat.
96
string &operator+=(char c);
98
/// constructive concat operator.
99
friend string operator+(const char* s1, const string &s2);
101
/// constructive concat operator.
102
friend string operator+(const string &s1, const char* s2);
104
/// constructive concat operator.
105
string operator+(const string &s);
107
/// constructive concat operator.
108
string operator+(const char* s);
110
/// add c at end of string.
113
/// add n characters of s at end of string (0 = without limit).
114
void add(const string &s, unsigned n = 0);
116
/// add n characters of s at end of string (0 = without limit).
117
void add(const char *s, unsigned n = 0);
119
/// add character at n-th position of string.
120
void insert(char c, int pos);
122
// searching and matching.
123
/// return 1 if target is in string. 0 for failure.
124
int contains(const string &s) const;
126
/// return 1 if target is in string. 0 for failure.
127
int contains(const char* s) const;
129
/// return 1 if target is in string. 0 for failure.
130
int contains(char c) const;
132
/// return 1 if string starts with target, 0 for failure.
133
int startsWith(const string &s) const;
135
/// return 1 if string starts with target, 0 for failure.
136
int startsWith(const char* s) const;
138
/// return 1 if string ends with target, 0 for failure.
139
int endsWith(const string &s) const;
141
/// return 1 if string ends with target, 0 for failure.
142
int endsWith(const char* s) const;
144
/// adds suffix s. return 1 when suffix is changed, 0 when only added.
145
int setSuffix(const string &s, char delim = '.');
147
/// puts suffix in s. when there is no suffix returns 0, 1 otherwise.
148
int getSuffix(string &s, char delim = '.') const;
150
// element extraction.
151
/// selection of char by index a la array.
152
char &operator[](int i);
154
/// selection of char by index a la array.
155
const char &operator[](int i) const;
158
/// remove last character of string.
161
/// remove pos-th character of string.
162
void remove(int pos);
165
/// replace each occurence of old char by nw char .
166
int replace(char old, char nw);
168
/// global substitution.
169
int replace(const string &pat, const string &repl, int casesens=1);
171
/// first occurrence substitution.
172
int replaceFirst(const string &pat, const string &repl,
175
/// add to word an english plural ending, like horse->horses, sky->skies.
179
/// move all chars to upper case
180
friend string upcase(const string &s);
182
/// move all chars to lower case
183
friend string downcase(const string &s);
185
/// strip all spurious blank chars.
186
friend string stripblanks(const string &s);
188
/// in-place version of the above.
196
unsigned long toulong() const;
200
double todouble() const;
204
/// number of characters in string.
205
unsigned length() const {return len;}
207
/// the number of lines in string when printed: newlines+1.
208
unsigned lines() const;
210
/// return nr. of chars of line with most chars in string.
211
unsigned width() const;
213
/// return which line has i-th char. pos becomes position in line.
214
int whichline(int i, int &pos) const;
216
/// return position of n-th char in line-th line.
217
int nthInline(int line, int n) const;
219
/// return number of letters (a-z,A-Z) in string.
220
unsigned letters() const;
222
/// set precision when string is interpreted as double.
223
void setPrecision(unsigned p) {precision=p;}
226
/// write string to output stream.
227
friend std::ostream &operator<<(std::ostream &o, const string &s);
229
/// read string from input stream.
230
friend std::istream &operator>>(std::istream &i, string &s);
233
/// comparison operator returns 1 if equal
234
friend int operator==(const string &x, const char* s);
236
/// comparison operator returns 1 if equal.
237
friend int operator==(const string &x, const string &y);
239
/// comparison operator that ignores case
240
friend int operator%=(const string &x, const char* s);
242
/// comparison operator that ignores case
243
friend int operator%=(const string &x, const string &y);
245
/// comparison operator a la !equal.
246
friend int operator!=(const string &x, const char* s);
248
/// comparison operator a la !equal.
249
friend int operator!=(const string &x, const string &y);
251
/// comparison operator a la strcmp
252
friend int compare(const string &x, const char* s);
254
/// comparison operator a la strcmp
255
friend int compare(const string &x, const string &y);
258
/// return a const pointer to the data to do dirty stuff.
259
const char* getstr() const {return str;}
262
int doReplace(const string &pat, const string &repl,
263
int global, int casesens=1);
268
/// the length of the data.
271
/// the precision used for floats and doubles.
274
/// length in which data is allocated.
275
static const int CHUNK;