FreeNOS
String.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBSTD_STRING_H
19#define __LIBSTD_STRING_H
20
21#include "Types.h"
22#include "Macros.h"
23#include "Assert.h"
24#include "Sequence.h"
25#include "List.h"
26
36#define STRING_DEFAULT_SIZE 64
37
41class String : public Sequence<char>
42{
43 using Sequence<char>::compareTo;
44 using Sequence<char>::equals;
45
46 public:
47
53 String();
54
60 String(const String & str);
61
68 String(char *s, const bool copy = true);
69
76 String(const char *s, const bool copy = false);
77
86 String(const int number);
87
91 virtual ~String();
92
98 virtual Size size() const;
99
105 virtual Size count() const;
106
112 Size length() const;
113
121 static Size length(char *str);
122
130 static Size length(const char *str);
131
139 virtual bool resize(const Size size);
140
148 virtual bool reserve(const Size count);
149
157 virtual const char * get(const Size position) const;
158
168 virtual const char & at(const Size position) const;
169
180 virtual const char value(const Size position) const;
181
189 virtual bool contains(const char character) const;
190
198 bool startsWith(const String & prefix) const;
199
207 bool startsWith(const char * prefix) const;
208
216 bool endsWith(const String & suffix) const;
217
225 bool endsWith(const char * suffix) const;
226
235 virtual int compareTo(const String & str) const;
236
246 virtual int compareTo(const String & str,
247 const bool caseSensitive = true) const;
248
259 virtual int compareTo(const char *str,
260 const bool caseSensitive = true,
261 const Size count = 0) const;
262
268 virtual bool equals(const String &str) const;
269
277 bool match(const char *mask) const;
278
290 String substring(const Size index, const Size size = 0) const;
291
299 List<String> split(const char delimiter) const;
300
308 List<String> split(const String & delimiter) const;
309
317 long toLong(const Number::Base base = Number::Dec) const;
318
330 String & pad(const Size length);
331
337 String & trim();
338
344 String & lower();
345
351 String & upper();
352
360 Size set(const long number,
361 const Number::Base base = Number::Dec,
362 char *string = ZERO);
363
372 Size setUnsigned(const ulong number,
373 const Number::Base base = Number::Dec,
374 char *string = ZERO,
375 const bool sign = false);
376
382 void operator = (const char *str);
383
389 void operator = (const String & str);
390
396 bool operator == (const String & str) const;
397
403 bool operator != (const String & str) const;
404
408 const char * operator * () const;
409
413 char * operator * ();
414
418 String & operator << (const char *str);
419
423 String & operator << (const String & str);
424
428 String & operator << (const int number);
429
433 String & operator << (const unsigned int number);
434
438 String & operator << (const void *pointer);
439
443 String & operator << (const Number::Base format);
444
445 private:
446
448 char *m_string;
449
452
455
458
461};
462
468#endif /* __LIBSTD_STRING_H */
Simple linked list template class.
Definition List.h:37
Sequences are containers that provide indexed based storage of items.
Definition Sequence.h:38
Abstraction of strings.
Definition String.h:42
const char * operator*() const
Dereference operator (read-only).
Definition String.cpp:644
Number::Base m_base
Number format to use for convertions.
Definition String.h:460
bool startsWith(const String &prefix) const
Tests if this String starts with the specified prefix.
Definition String.cpp:189
virtual Size count() const
Number of characters in the string.
Definition String.cpp:100
String & trim()
Remove leading and trailing whitespace from the String.
Definition String.cpp:357
Size m_size
Size of the string buffer, including any NULL byte(s) at the end.
Definition String.h:451
long toLong(const Number::Base base=Number::Dec) const
Convert the String to a signed long integer.
Definition String.cpp:456
virtual bool equals(const String &str) const
Alias for compareTo().
Definition String.cpp:262
String substring(const Size index, const Size size=0) const
Returns a part of the String as a copy.
Definition String.cpp:314
String & operator<<(const char *str)
Append character string to the String.
Definition String.cpp:654
Size length() const
Same as count().
Definition String.cpp:105
Size setUnsigned(const ulong number, const Number::Base base=Number::Dec, char *string=ZERO, const bool sign=false)
Set text-representation of an unsigned number.
Definition String.cpp:538
virtual bool reserve(const Size count)
Make sure at least given number of bytes available.
Definition String.cpp:157
Size set(const long number, const Number::Base base=Number::Dec, char *string=ZERO)
Set text-representation of a signed number.
Definition String.cpp:533
virtual bool contains(const char character) const
Check if the given character occurs in the String.
Definition String.cpp:180
virtual const char * get(const Size position) const
Returns the item at the given position.
Definition String.cpp:165
bool endsWith(const String &suffix) const
Tests if this String ends with the specified suffix.
Definition String.cpp:210
char * m_string
Current value of the String.
Definition String.h:448
String()
Default constructor.
Definition String.cpp:22
virtual Size size() const
Calculates the length of the String.
Definition String.cpp:95
String & lower()
Convert all Characters to lower case.
Definition String.cpp:386
void operator=(const char *str)
Assignment operator.
Definition String.cpp:610
String & pad(const Size length)
Pad line with trailing whitespace.
Definition String.cpp:332
bool operator==(const String &str) const
Comparision operator.
Definition String.cpp:634
Size m_count
Length of the string text, excluding NULL byte(s) at the end.
Definition String.h:454
virtual int compareTo(const String &str) const
Compares this String to the given String.
Definition String.cpp:231
virtual ~String()
Destructor.
Definition String.cpp:86
bool operator!=(const String &str) const
Inequal operator.
Definition String.cpp:639
List< String > split(const char delimiter) const
Split the String into parts separated by a delimiter.
Definition String.cpp:408
String & upper()
Convert all Characters to upper case.
Definition String.cpp:397
virtual const char & at(const Size position) const
Returns a reference to the item at the given position.
Definition String.cpp:170
bool match(const char *mask) const
Matches the String against a mask.
Definition String.cpp:267
bool m_allocated
True if the string buffer is a deep copy, false otherwise.
Definition String.h:457
virtual const char value(const Size position) const
Return value at the given position.
Definition String.cpp:175
virtual bool resize(const Size size)
Change the size of the String buffer.
Definition String.cpp:125
unsigned long ulong
Unsigned long number.
Definition Types.h:47
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
void copy(Terminal *term, const teken_rect_t *rect, const teken_pos_t *pos)
Copy bytes to the terminal.
Definition Terminal.cpp:282
Base
Numeral system base type.
Definition Types.h:169
@ Dec
Definition Types.h:170