GM6000 Digital Heater Controller Branch: main
SDX-1330
String.h
Go to the documentation of this file.
1#ifndef Cpl_Text_String_h_
2#define Cpl_Text_String_h_
3/*-----------------------------------------------------------------------------
4* This file is part of the Colony.Core Project. The Colony.Core Project is an
5* open source project with a BSD type of licensing agreement. See the license
6* agreement (license.txt) in the top/ directory or on the Internet at
7* http://integerfox.com/colony.core/license.txt
8*
9* Copyright (c) 2014-2022 John T. Taylor
10*
11* Redistributions of the source code must retain the above copyright notice.
12*----------------------------------------------------------------------------*/
13/** @file */
14
15#include "colony_map.h"
16#include "Cpl/Container/Key.h"
17#include <stdarg.h>
18#include <iostream>
19
20///
21namespace Cpl {
22///
23namespace Text {
24
25/** This abstract class defines the operations that can be before on
26 a NULL terminated string. The intent is to provide a simple mechanism
27 for manipulating and "passing-around" strings rather than using error
28 prone explicit string operations.
29
30 NOTES:
31
32 o The memory management for the string data is determined by
33 the concrete sub-class!
34 o The 'String' class IS A Cpl::Container::Key, but it is NOT a
35 Cpl::Container::xxxItem instance. Use the 'StringItem' class
36 for storing a string directly in a container.
37 */
38
40{
41public:
42 /// Virtual destructor!
43 virtual ~String() {}
44
45
46public:
47 ///@{
48 /** Assignment of a string. The argument string is copied
49 to the internal storage of the object.
50 */
51 virtual Cpl::Text::String& operator=( const String& string ) = 0;
52
53 /** Assignment of a string. The argument string is copied
54 to the internal storage of the object.
55 */
56 virtual Cpl::Text::String& operator=( const char* string ) = 0;
57
58 /** Assigns the character to first position in string and then
59 NULL terminates the string.
60 */
61 virtual Cpl::Text::String& operator=( char c ) = 0;
62
63 /** Converts the 'number' to a string and stores its in
64 the internal storage of the object.
65 */
66 virtual Cpl::Text::String& operator=( int num ) = 0;
67
68 /** Converts the 'number' to a string and stores its in
69 the internal storage of the object.
70 */
71 virtual Cpl::Text::String& operator=( unsigned int num ) = 0;
72
73 /** Converts the 'number' to a string and stores its in
74 the internal storage of the object.
75 */
76 virtual Cpl::Text::String& operator=( long num ) = 0;
77
78 /** Converts the 'number' to a string and stores its in
79 the internal storage of the object.
80 */
81 virtual Cpl::Text::String& operator=( long long num ) = 0;
82
83 /** Converts the 'number' to a string and stores its in
84 the internal storage of the object.
85 */
86 virtual Cpl::Text::String& operator=( unsigned long num ) = 0;
87
88 /** Converts the 'number' to a string and stores its in
89 the internal storage of the object.
90 */
91 virtual Cpl::Text::String& operator=( unsigned long long num ) = 0;
92
93 /** This method copies the content of the source string,
94 up to 'n' characters, into internal storage of the object.
95 NOTE: The internal storage is always guaranteed to be NULL
96 terminated.
97 */
98 virtual void copyIn( const char* string, int n ) = 0;
99
100 /** This method inserts the specified string starting at
101 the specified offset. The original contents of the string
102 beginning with 'insertOffset' is "shifted to the right" to
103 make room for the string being inserted. If an attempt is
104 made to insert the string past the current end-of-string, then
105 the stringToInsert is simply appended. If an attempt is made
106 to insert the string at a negative offset, then the stringToInsert
107 is prepended to the string.
108 */
109 virtual void insertAt( int insertOffset, const char* stringToInsert ) = 0;
110
111 /** Clears the internal string (i.e. string[0] = '\0').
112 */
113 virtual void clear() = 0;
114 ///@}
115
116public:
117 ///@{
118 /** Append a string. The argument string is concatenated
119 to the end of the string object.
120 */
121 virtual Cpl::Text::String& operator +=( const Cpl::Text::String& string ) = 0;
122
123 /** Append a string. The argument string is concatenated
124 to the end of the string object.
125 */
126 virtual Cpl::Text::String& operator +=( const char* string ) = 0;
127
128 /** Appends the character to the end of the string.
129 */
130 virtual Cpl::Text::String& operator +=( char c ) = 0;
131
132 /** Converts the 'number' to a string and appends its to the
133 end of the string.
134 */
135 virtual Cpl::Text::String& operator +=( int num ) = 0;
136
137 /** Converts the 'number' to a string and appends its to the
138 end of the string.
139 */
140 virtual Cpl::Text::String& operator +=( unsigned int num ) = 0;
141
142 /** Converts the 'number' to a string and appends its to the
143 end of the string.
144 */
145 virtual Cpl::Text::String& operator +=( long num ) = 0;
146
147 /** Converts the 'number' to a string and appends its to the
148 end of the string.
149 */
150 virtual Cpl::Text::String& operator +=( long long num ) = 0;
151
152 /** Converts the 'number' to a string and appends its to the
153 end of the string.
154 */
155 virtual Cpl::Text::String& operator +=( unsigned long num ) = 0;
156
157 /** Converts the 'number' to a string and appends its to the
158 end of the string.
159 */
160 virtual Cpl::Text::String& operator +=( unsigned long long num ) = 0;
161
162 /** This method appends the content of the source string,
163 up to 'n' characters, into internal storage of the object.
164 NOTE: The internal storage is always guaranteed to be NULL
165 terminated.
166 */
167 virtual void appendTo( const char* string, int n ) = 0;
168
169 ///@}
170
171
172public:
173 ///@{
174 /// Cast to character -->i.e returns the first character in the string.
175 virtual operator char() const = 0;
176
177 /// Read-only Access to the "raw" string.
178 virtual const char* getString() const = 0;
179
180 /// Cast to read-only character string pointer.
181 inline operator const char* ( ) const { return getString(); }
182
183 /// Returns a Read-only pointer to the "raw" (short-hand for getString())
184 inline const char* operator()() const { return getString(); }
185
186 /// Returns the specified character. If n is out-bounds, then '\0' is returned
187 inline char operator[] ( int i ) const { return i<0 || i>length() ? '\0' : getString()[i]; }
188 ///@}
189
190
191public:
192 ///@{
193 /** Returns the length, in bytes, of the string. The size returned does
194 NOT include NULL terminator (i.e. if the returned size is 15, my
195 internal storage must be at least 15+1)
196 */
197 virtual int length() const = 0;
198
199 /** Returns the max allowed size of the string. The size returned does
200 NOT include NULL terminator (i.e. if the returned size is 15, my
201 internal storage must be at least 15+1)
202 */
203 virtual int maxLength() const = 0;
204
205 /** Returns the available space left in the internal buffer. The size
206 returned does NOT include NULL terminator (i.e. if the returned size
207 is 15, my internal storage must be at least 15+1).
208 */
209 inline int availLength() { return maxLength() - length(); }
210
211 /// Returns true if the string is empty (i.e. string[0] == '\0')
212 virtual bool isEmpty() const = 0;
213
214 /** Returns true if the last String "write" operation caused the
215 String contents to be truncated. This flag is reset after every
216 string "write" operation.
217 */
218 virtual bool truncated() const = 0;
219 ///@}
220
221
222public:
223 ///@{
224 /** Compares two Strings and returns true if they are equal
225 NOTE: if 'string' is a null pointer, then false is returned.
226 */
227 virtual bool operator ==( const char* string ) const = 0;
228
229 /// Compares two Strings and returns true if they are equal
230 virtual bool operator ==( const Cpl::Text::String& string ) const = 0;
231
232 /** Compares two Strings and returns true if they are NOT equal
233 NOTE: if 'string' is a null pointer, then true is returned.
234 */
235 virtual bool operator !=( const char* string ) const = 0;
236
237 /// Compares two Strings and returns true if they are NOT equal
238 virtual bool operator !=( const Cpl::Text::String& string ) const = 0;
239
240 /** Compares two strings independently of case and return true if equal
241 NOTE: if 'string' is a null pointer, then false is returned.
242 */
243 virtual bool isEqualIgnoreCase( const char* string ) const = 0;
244
245 /// Compares the specified character to the first character in the string
246 virtual bool operator ==( char c ) const = 0;
247
248 /** Compares two strings and returns <0, 0, >0 if this string is less than,
249 equal, or greater than respectively to the specified string.
250 NOTE: if 'string' is a null pointer, then -1 is returned.
251 */
252 virtual int compare( const char* string ) const = 0;
253
254 /// Same as compare(), but case insensitive.
255 virtual int compareIgnoreCase( const char* string ) const = 0;
256
257 /** Compares the specified substring with the specified string. Substring
258 is inclusive of the endpoints. If the endpoints are out-of-range, they
259 are clamped by the actual bounds of the string. Position parameters are
260 zero-based indexes into this String object.
261 NOTE: if 'string' is a null pointer, then false is returned.
262 */
263 virtual bool isEqualSubstring( int startOffset, int endOffset, const char* string ) const = 0;
264
265 /// Same as above, except case insensitive comparison.
266 virtual bool isEqualSubstringIgnoreCase( int startOffset, int endOffset, const char* string ) const = 0;
267
268 /** Returns true if the string starts with the specified sub-string.
269 Note: if 'string' is a null pointer, then false is returned
270 */
271 virtual bool startsWith( const char* string ) const = 0;
272
273 /** Sames as starsWith(), but begins the search at index 'startOffset'.
274 Note: If 'string' is a null pointer or 'startOffset' is out-of-bound,
275 then false is returned.
276 */
277 virtual bool startsWith( const char* string, int startOffset ) const = 0;
278
279 /** Returns true if the string ends with the specified sub-string.
280 Note: if 'string' is a null pointer, then false is returned
281 */
282 virtual bool endsWith( const char* string ) const = 0;
283
284 ///@}
285
286public:
287 ///@{
288 /** Returns the index of the first match for the specified character. If
289 no match is found, -1 is returned.
290 */
291 virtual int indexOf( char c ) const = 0;
292
293 /** Same as indexOf(char), but starts at 'startOffset' instead of the beginning
294 of the string.
295
296 Note: If 'startOffset' is out-of-bounds than -1 is returned.
297 */
298 virtual int indexOf( char c, int startOffset ) const = 0;
299
300 /** Returns the index (of the starting character) of the first match
301 for the specified sub-string. If no match is found, -1 is returned.
302
303 Note: If 'string' is null, then -1 is returned
304 */
305 virtual int indexOf( const char* string ) const = 0;
306
307 /** Same as indexOf(const char*), but starts at 'startOffset' instead of the
308 beginning of the string.
309
310 Note: If 'string' is null or 'startOffset' is out-of-bounds than -1
311 is returned.
312 */
313 virtual int indexOf( const char* string, int startOffset ) const = 0;
314
315 /** Returns the index of the last match for the specified character. If
316 no match is found, -1 is returned.
317 */
318 virtual int lastIndexOf( char c ) const = 0;
319
320 /** Returns the number of times the specified character appears in
321 the string.
322 */
323 virtual int count( char c ) const = 0;
324 ///@}
325
326
327public:
328 ///@{
329 /** Allows "printf" formatting of the string. NOTE: Since the
330 amount of information being written to the string is variable,
331 this method does NOT guarantee how much, if any, of the
332 information is actually written to the string. The method
333 does guarantee that the string's internal storage will NOT
334 be exceeded.
335
336 NOTE: if 'format' is null, then nothing is done
337 */
338 virtual void format( const char* format, ... ) = 0;
339
340 /// Same as format() - but appends "formatting" to the end of the string
341 virtual void formatAppend( const char* format, ... ) = 0;
342
343 /** This method is the same as format(), except when 'appendFlag' is true
344 then it behaves as formatAppend().
345 */
346 virtual void formatOpt( bool appendFlag, const char* format, ... ) = 0;
347
348
349 /** Same as format(), except that it is called with a va_list
350 instead of a variable number of arguments.
351 */
352 virtual void vformat( const char* format, va_list ap ) = 0;
353
354 /** Same as formatAppend(), except that it is called with a va_list
355 instead of a variable number of arguments.
356 */
357 virtual void vformatAppend( const char* format, va_list ap ) = 0;
358
359 /** This method is the same as vformat(), except when 'appendFlag' is true
360 then it behaves as vformatAppend().
361 */
362 virtual void vformatOpt( bool appendFlag, const char* format, va_list ap ) = 0;
363 ///@}
364
365
366public:
367 ///@{
368 /** Forces the entire string to upper case characters. Also returns
369 the newly modified string contents.
370 */
371 virtual const char* toUpper() = 0;
372
373 /** Forces the entire string to lower case characters. Also returns
374 the newly modified string contents.
375 */
376 virtual const char* toLower() = 0;
377
378 /** Removes any leading white-space from the string (white-space
379 is defined by the standard isspace() function).
380 */
381 virtual void removeLeadingSpaces() = 0;
382
383 /** Removes any trailing white-space from the string (white-space
384 is defined by the standard isspace() function).
385 */
386 virtual void removeTrailingSpaces() = 0;
387
388 /** Removes the specified leading characters from the string.
389
390 NOTE: if 'charsSet' is null, nothing is done.
391 */
392 virtual void removeLeadingChars( const char* charsSet ) = 0;
393
394 /** Removes the specified trailing characters from the string.
395
396 NOTE: if 'charsSet' is null, nothing is done.
397 */
398 virtual void removeTrailingChars( const char* charsSet ) = 0;
399
400 /** Removes the characters beginning with 'startOffset' and ending with
401 'endOffset'. The characters after 'endOffset' are shifted left
402 to form a continuous string. If the position fields are invalid
403 or out-of-bounds, nothing is done.
404 */
405 virtual void cut( int startOffset, int endOffset ) = 0;
406
407 /** Removes 'n' characters from the start-of-string. If 'n' is greater
408 than the length of the string or less than zero, nothing is done.
409 */
410 inline void trimLeft( int n ) { cut( 0, n - 1 ); }
411
412 /** Removes 'n' characters from the end-of-string. If 'n' is greater
413 than the length of the string or less than zero, nothing is done.
414 */
415 virtual void trimRight( int n ) = 0;
416
417 /** Replaces the character at index 'atOffset' with 'newchar'.
418 If 'atOffset' is out-of-bounds or greater than the length of the
419 actual string, then nothing is done.
420 */
421 virtual void setChar( int atOffset, char newchar ) = 0;
422
423 /** Replaces all instances of targetChar in the string with
424 newChar. Returns the number characters (if any) replaced.
425 NOTE: If targetChar == newChar, nothing is done and zero
426 is returned.
427 */
428 virtual int replace( char targetChar, char newChar ) = 0;
429 ///@}
430
431
432public:
433 ///@{
434 /** Returns a "writable" pointer to the string's internal storage.
435 The caller is RESPONSIBLE for insuring that the string is
436 left in a valid state - that is the string is NULL terminated
437 and has NOT exceeded the 'maxAllowedLength'. Also The pointer
438 and length have a VERY LIMITED life span - the values become
439 invalid when any of the another string methods are called!
440 NOTE: This method always clears the 'truncated' flags/status.
441
442 *** THIS METHOD SHOULD BE USED WITH EXTREME CAUTION AND AVOIDED WHENEVER POSSIBLE! ***
443 */
444 virtual char* getBuffer( int& maxAllowedLength ) = 0;
445 ///@}
446};
447
448
449}; // end namespaces
450};
451
452
453
454/* Note: I could only make the stream operators work if there were NOT declared
455 within the Cpl::Text namespace. Fortunately, I don't actually have to
456 make the stream operators 'friends' for the necessary implementation.
457
458 If someone could explain/show me how to make it work using the
459 'friend' approach - please contact me.
460*/
461namespace std {
462
463/// Support stream output operator directly
464ostream& operator<< ( ostream &out, const Cpl::Text::String& outstring );
465
466/// Support stream input operator directly
467istream& operator>> ( istream &in, Cpl::Text::String& instring );
468
469};
470
471#endif // end header latch
This abstract class defines the interface that a contained object must support if it has comparable k...
Definition Key.h:32
This abstract class defines the operations that can be before on a NULL terminated string.
Definition String.h:40
const char * operator()() const
Returns a Read-only pointer to the "raw" (short-hand for getString())
Definition String.h:184
virtual void formatOpt(bool appendFlag, const char *format,...)=0
This method is the same as format(), except when 'appendFlag' is true then it behaves as formatAppend...
virtual void removeTrailingSpaces()=0
Removes any trailing white-space from the string (white-space is defined by the standard isspace() fu...
virtual bool startsWith(const char *string, int startOffset) const =0
Sames as starsWith(), but begins the search at index 'startOffset'.
virtual void removeTrailingChars(const char *charsSet)=0
Removes the specified trailing characters from the string.
virtual void removeLeadingChars(const char *charsSet)=0
Removes the specified leading characters from the string.
virtual void insertAt(int insertOffset, const char *stringToInsert)=0
This method inserts the specified string starting at the specified offset.
virtual void format(const char *format,...)=0
Allows "printf" formatting of the string.
virtual bool truncated() const =0
Returns true if the last String "write" operation caused the String contents to be truncated.
virtual Cpl::Text::String & operator+=(const Cpl::Text::String &string)=0
Append a string.
virtual bool isEqualSubstring(int startOffset, int endOffset, const char *string) const =0
Compares the specified substring with the specified string.
virtual void appendTo(const char *string, int n)=0
This method appends the content of the source string, up to 'n' characters, into internal storage of ...
virtual bool isEqualIgnoreCase(const char *string) const =0
Compares two strings independently of case and return true if equal NOTE: if 'string' is a null point...
virtual Cpl::Text::String & operator=(unsigned long long num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual int count(char c) const =0
Returns the number of times the specified character appears in the string.
virtual void copyIn(const char *string, int n)=0
This method copies the content of the source string, up to 'n' characters, into internal storage of t...
void trimLeft(int n)
Removes 'n' characters from the start-of-string.
Definition String.h:410
virtual void removeLeadingSpaces()=0
Removes any leading white-space from the string (white-space is defined by the standard isspace() fun...
virtual ~String()
Virtual destructor!
Definition String.h:43
virtual void vformatOpt(bool appendFlag, const char *format, va_list ap)=0
This method is the same as vformat(), except when 'appendFlag' is true then it behaves as vformatAppe...
virtual char * getBuffer(int &maxAllowedLength)=0
Returns a "writable" pointer to the string's internal storage.
virtual int indexOf(char c) const =0
Returns the index of the first match for the specified character.
virtual const char * toUpper()=0
Forces the entire string to upper case characters.
virtual void vformatAppend(const char *format, va_list ap)=0
Same as formatAppend(), except that it is called with a va_list instead of a variable number of argum...
virtual void vformat(const char *format, va_list ap)=0
Same as format(), except that it is called with a va_list instead of a variable number of arguments.
virtual int indexOf(char c, int startOffset) const =0
Same as indexOf(char), but starts at 'startOffset' instead of the beginning of the string.
virtual Cpl::Text::String & operator=(char c)=0
Assigns the character to first position in string and then NULL terminates the string.
virtual void cut(int startOffset, int endOffset)=0
Removes the characters beginning with 'startOffset' and ending with 'endOffset'.
virtual bool operator==(const char *string) const =0
Compares two Strings and returns true if they are equal NOTE: if 'string' is a null pointer,...
virtual Cpl::Text::String & operator=(const String &string)=0
Assignment of a string.
virtual void formatAppend(const char *format,...)=0
Same as format() - but appends "formatting" to the end of the string.
virtual Cpl::Text::String & operator=(long num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual bool isEqualSubstringIgnoreCase(int startOffset, int endOffset, const char *string) const =0
Same as above, except case insensitive comparison.
virtual Cpl::Text::String & operator=(long long num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual int compareIgnoreCase(const char *string) const =0
Same as compare(), but case insensitive.
virtual int indexOf(const char *string) const =0
Returns the index (of the starting character) of the first match for the specified sub-string.
virtual int indexOf(const char *string, int startOffset) const =0
Same as indexOf(const char*), but starts at 'startOffset' instead of the beginning of the string.
virtual bool operator!=(const char *string) const =0
Compares two Strings and returns true if they are NOT equal NOTE: if 'string' is a null pointer,...
virtual bool isEmpty() const =0
Returns true if the string is empty (i.e. string[0] == '\0')
virtual void setChar(int atOffset, char newchar)=0
Replaces the character at index 'atOffset' with 'newchar'.
virtual void trimRight(int n)=0
Removes 'n' characters from the end-of-string.
virtual Cpl::Text::String & operator=(int num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual Cpl::Text::String & operator=(unsigned int num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual const char * toLower()=0
Forces the entire string to lower case characters.
virtual const char * getString() const =0
Read-only Access to the "raw" string.
virtual bool endsWith(const char *string) const =0
Returns true if the string ends with the specified sub-string.
char operator[](int i) const
Returns the specified character. If n is out-bounds, then '\0' is returned.
Definition String.h:187
int availLength()
Returns the available space left in the internal buffer.
Definition String.h:209
virtual int replace(char targetChar, char newChar)=0
Replaces all instances of targetChar in the string with newChar.
virtual int compare(const char *string) const =0
Compares two strings and returns <0, 0, >0 if this string is less than, equal, or greater than respec...
virtual int maxLength() const =0
Returns the max allowed size of the string.
virtual int lastIndexOf(char c) const =0
Returns the index of the last match for the specified character.
virtual Cpl::Text::String & operator=(unsigned long num)=0
Converts the 'number' to a string and stores its in the internal storage of the object.
virtual Cpl::Text::String & operator=(const char *string)=0
Assignment of a string.
virtual void clear()=0
Clears the internal string (i.e.
virtual int length() const =0
Returns the length, in bytes, of the string.
virtual bool startsWith(const char *string) const =0
Returns true if the string starts with the specified sub-string.
The 'Cpl' namespace is the root name space for the Colony.
Definition Api16.h:20