This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.h
More file actions
250 lines (193 loc) · 5.81 KB
/
str.h
File metadata and controls
250 lines (193 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* str.h
* String, StringTokens, and StringMask classes
*
* Copyright (c) 1996-2001 Alien Internet Services
*/
#ifndef __STR_H_
# define __STR_H_
# include <iostream.h>
#include <stdlib.h>
# ifdef STL_HAS_HASH
# include <hash_map>
# endif
class String {
public:
typedef signed int length_t; // Our length type
private:
struct strptr { // To stop reference errors
char *str; // Pointer to the data
int ref; // Reference counter
strptr(void):ref(1) {}; // Initialiser
};
strptr *ptr; // Pointer to above
length_t len; // Length (quick reference)
public:
// Constructor
String(void)
: ptr(new strptr), len(0)
{};
String(char const *); // Migration constructor
String(String const &); // Copy constructor
String(unsigned char); // Migration constructor
String(int); // Migration constructor
String(unsigned int); // Migration constructor
String(long); // Migration constructor
String(unsigned long); // Migration constructor
String(double); // Migration constructor
String(char); // MIgration constructor
~String(void); // Destructor
String & operator=(char const *); // Set operator (migration)
String & operator=(String const &); // Set operator
char & operator[](length_t); // Get char operator
char const & operator[](length_t) const; // Get char operator
// Boolean equals operator
bool operator==(char const *s) const
{
return (strcmp(ptr->str, s) == 0);
};
// Boolean equals operator
bool operator==(String const &s) const
{
return ((ptr->str == s.ptr->str) ||
(strcmp(ptr->str, s.ptr->str) == 0));
};
// Boolean not-equals operator
bool operator!=(char const *s) const
{
return (strcmp(ptr->str, s) != 0);
};
// Boolean not-equals operator
bool operator!=(String const &s) const
{
return (strcmp(ptr->str, s.ptr->str) != 0);
};
// Boolean less-than operator
bool operator<(String const &s) const
{
return (strcmp(ptr->str, s.ptr->str) < 0);
};
// Boolean greater-than operator
bool operator>(String const &s) const
{
return (strcmp(ptr->str, s.ptr->str) > 0);
};
String operator+(char const *) const; // Addition operator
String operator+(String const &) const; // Addition operator
// Migration operator
operator char const *(void) const
{
return ptr->str;
};
// Migration operator
operator char *(void) const
{
return ptr->str;
};
// IO-stream output operator
friend ostream & operator<<(ostream &s, String const &line)
{
return (s << line.ptr->str);
};
// IO-stream input operator
friend istream & operator>>(istream &, String &);
// Get the length of a string
length_t length(void) const
{
return len;
};
length_t find(char) const; // Find the location of a char
String subString(length_t, length_t) const; // Sub-string from x to y
// Sub-string from x to end
String subString(length_t start) const
{
return subString(start, len -1);
};
String toLower(void) const; // Lower-case the string
String toUpper(void) const; // Upper-case the string
String IRCtoLower(void) const; // toLower() with IRC chars
int toInt(void) const // Convert to an integer
{
return atoi(ptr->str);
};
long toLong(void) const // Convert to a long int
{
return atol(ptr->str);
};
double toDouble(void) const // Convert to a double
{
return atof(ptr->str);
};
String trim(void) const; // Trim whitespace (pre/suf)fix
String trimQuotes(void) const; // Trim quote mark (pre/suf)fix
String pad(length_t, char = ' ') const; // Pad a string with chars
String prepad(length_t, char = ' ') const; // Prepad a string with chars
static String printf(char *, ...); // printf clone
};
// StringTokens for String class
class StringTokens {
private:
String str; // The string
String::length_t pos; // Our position
public:
// Constructor
StringTokens(String s, String::length_t p = 0)
: str(s), pos(p)
{};
// Copy constructor
StringTokens(StringTokens &st)
: str(st.str), pos(st.pos)
{};
bool hasMoreTokens(void) const; // Any more tokens left?
bool hasMoreTokens(char) const;
unsigned int countTokens(void); // Count the number of tokens
unsigned int countTokens(char);
String nextToken(void); // Get the next token
String nextToken(char, bool = false);
String nextColonToken(void); // Get next IRC style token
String rest(void); // Get the rest of the line
};
// String masking routines for String class
class StringMask {
private:
String mask; // The mask itself
static bool match(char const *,
char const *); // Match checker
public:
StringMask(void) // Constructor
: mask("")
{};
StringMask(String s) // Constructor
: mask(s)
{};
StringMask(StringMask const &sm) // Copy constructor
: mask(sm.mask)
{};
bool operator==(StringMask const &sm) const // Boolean equals operator
{
return mask == sm.mask;
};
bool operator!=(StringMask const &sm) const // Boolean not-equals operator
{
return mask != sm.mask;
};
String getMask(void) const // Return the mask
{
return mask;
};
bool matches(String const &s) const // Check string match
{
return match((char const *)mask, (char const *)s);
};
bool matches(char const *s) const
{
return match((char const *)mask, s);
};
};
# ifdef STL_HAS_HASH
// STL hash templates for String class
template<> struct hash<String>
{
size_t operator()(String const &) const; // STL Hash operator
};
# endif
#endif