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 
41 class String : public Sequence<char>
42 {
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 */
String::value
virtual const char value(const Size position) const
Return value at the given position.
Definition: String.cpp:175
copy
void copy(Terminal *term, const teken_rect_t *rect, const teken_pos_t *pos)
Copy bytes to the terminal.
Definition: Terminal.cpp:282
String::upper
String & upper()
Convert all Characters to upper case.
Definition: String.cpp:397
String::resize
virtual bool resize(const Size size)
Change the size of the String buffer.
Definition: String.cpp:125
Macros.h
String::pad
String & pad(const Size length)
Pad line with trailing whitespace.
Definition: String.cpp:332
ulong
unsigned long ulong
Unsigned long number.
Definition: Types.h:47
Types.h
String::length
Size length() const
Same as count().
Definition: String.cpp:105
String::operator==
bool operator==(const String &str) const
Comparision operator.
Definition: String.cpp:634
String
Abstraction of strings.
Definition: String.h:41
String::~String
virtual ~String()
Destructor.
Definition: String.cpp:86
Sequence.h
String::at
virtual const char & at(const Size position) const
Returns a reference to the item at the given position.
Definition: String.cpp:170
Assert.h
String::set
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
Number::Base
Base
Numeral system base type.
Definition: Types.h:168
Number::Dec
@ Dec
Definition: Types.h:170
String::get
virtual const char * get(const Size position) const
Returns the item at the given position.
Definition: String.cpp:165
String::compareTo
virtual int compareTo(const String &str) const
Compares this String to the given String.
Definition: String.cpp:231
String::m_base
Number::Base m_base
Number format to use for convertions.
Definition: String.h:460
String::reserve
virtual bool reserve(const Size count)
Make sure at least given number of bytes available.
Definition: String.cpp:157
String::lower
String & lower()
Convert all Characters to lower case.
Definition: String.cpp:386
String::split
List< String > split(const char delimiter) const
Split the String into parts separated by a delimiter.
Definition: String.cpp:408
Sequence
Sequences are containers that provide indexed based storage of items.
Definition: Sequence.h:37
String::String
String()
Default constructor.
Definition: String.cpp:22
String::operator*
const char * operator*() const
Dereference operator (read-only).
Definition: String.cpp:644
String::contains
virtual bool contains(const char character) const
Check if the given character occurs in the String.
Definition: String.cpp:180
String::match
bool match(const char *mask) const
Matches the String against a mask.
Definition: String.cpp:267
String::count
virtual Size count() const
Number of characters in the string.
Definition: String.cpp:100
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
String::trim
String & trim()
Remove leading and trailing whitespace from the String.
Definition: String.cpp:357
String::operator!=
bool operator!=(const String &str) const
Inequal operator.
Definition: String.cpp:639
String::substring
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<<
String & operator<<(const char *str)
Append character string to the String.
Definition: String.cpp:654
String::m_string
char * m_string
Current value of the String.
Definition: String.h:448
String::size
virtual Size size() const
Calculates the length of the String.
Definition: String.cpp:95
String::m_count
Size m_count
Length of the string text, excluding NULL byte(s) at the end.
Definition: String.h:454
String::endsWith
bool endsWith(const String &suffix) const
Tests if this String ends with the specified suffix.
Definition: String.cpp:210
String::m_size
Size m_size
Size of the string buffer, including any NULL byte(s) at the end.
Definition: String.h:451
String::operator=
void operator=(const char *str)
Assignment operator.
Definition: String.cpp:610
String::startsWith
bool startsWith(const String &prefix) const
Tests if this String starts with the specified prefix.
Definition: String.cpp:189
List< String >
String::equals
virtual bool equals(const String &str) const
Alias for compareTo().
Definition: String.cpp:262
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
String::toLong
long toLong(const Number::Base base=Number::Dec) const
Convert the String to a signed long integer.
Definition: String.cpp:456
String::setUnsigned
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
List.h
String::m_allocated
bool m_allocated
True if the string buffer is a deep copy, false otherwise.
Definition: String.h:457