FreeNOS
Public Member Functions | Static Public Member Functions | Private Attributes
String Class Reference

Abstraction of strings. More...

#include <String.h>

Inheritance diagram for String:
Sequence< char > Container Comparable< Sequence< char > >

Public Member Functions

 String ()
 Default constructor. More...
 
 String (const String &str)
 Copy constructor. More...
 
 String (char *s, const bool copy=true)
 Constructor. More...
 
 String (const char *s, const bool copy=false)
 Constant constructor. More...
 
 String (const int number)
 Signed integer constructor. More...
 
virtual ~String ()
 Destructor. More...
 
virtual Size size () const
 Calculates the length of the String. More...
 
virtual Size count () const
 Number of characters in the string. More...
 
Size length () const
 Same as count(). More...
 
virtual bool resize (const Size size)
 Change the size of the String buffer. More...
 
virtual bool reserve (const Size count)
 Make sure at least given number of bytes available. More...
 
virtual const char * get (const Size position) const
 Returns the item at the given position. More...
 
virtual const char & at (const Size position) const
 Returns a reference to the item at the given position. More...
 
virtual const char value (const Size position) const
 Return value at the given position. More...
 
virtual bool contains (const char character) const
 Check if the given character occurs in the String. More...
 
bool startsWith (const String &prefix) const
 Tests if this String starts with the specified prefix. More...
 
bool startsWith (const char *prefix) const
 Tests if this String starts with the specified prefix. More...
 
bool endsWith (const String &suffix) const
 Tests if this String ends with the specified suffix. More...
 
bool endsWith (const char *suffix) const
 Tests if this String ends with the specified suffix. More...
 
virtual int compareTo (const String &str) const
 Compares this String to the given String. More...
 
virtual int compareTo (const String &str, const bool caseSensitive=true) const
 Compare with another String. More...
 
virtual int compareTo (const char *str, const bool caseSensitive=true, const Size count=0) const
 Compare with a character string. More...
 
virtual bool equals (const String &str) const
 Alias for compareTo(). More...
 
bool match (const char *mask) const
 Matches the String against a mask. More...
 
String substring (const Size index, const Size size=0) const
 Returns a part of the String as a copy. More...
 
List< Stringsplit (const char delimiter) const
 Split the String into parts separated by a delimiter. More...
 
List< Stringsplit (const String &delimiter) const
 Split the String into parts separated by a delimiter. More...
 
long toLong (const Number::Base base=Number::Dec) const
 Convert the String to a signed long integer. More...
 
Stringpad (const Size length)
 Pad line with trailing whitespace. More...
 
Stringtrim ()
 Remove leading and trailing whitespace from the String. More...
 
Stringlower ()
 Convert all Characters to lower case. More...
 
Stringupper ()
 Convert all Characters to upper case. More...
 
Size set (const long number, const Number::Base base=Number::Dec, char *string=ZERO)
 Set text-representation of a signed number. More...
 
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. More...
 
void operator= (const char *str)
 Assignment operator. More...
 
void operator= (const String &str)
 Assignment operator. More...
 
bool operator== (const String &str) const
 Comparision operator. More...
 
bool operator!= (const String &str) const
 Inequal operator. More...
 
const char * operator* () const
 Dereference operator (read-only). More...
 
char * operator* ()
 Dereference operator. More...
 
Stringoperator<< (const char *str)
 Append character string to the String. More...
 
Stringoperator<< (const String &str)
 Append String to another String. More...
 
Stringoperator<< (const int number)
 Append the given signed number as text to the String. More...
 
Stringoperator<< (const unsigned int number)
 Append the given unsigned number as text to the String. More...
 
Stringoperator<< (const void *pointer)
 Append the pointer as hexidecimal unsigned number to the String. More...
 
Stringoperator<< (const Number::Base format)
 Change the default number format representation. More...
 
- Public Member Functions inherited from Sequence< char >
virtual int insert (const char &item)
 Adds the given item to the Sequence, if possible. More...
 
virtual bool insert (Size position, const char &item)
 Inserts the given item at the given position. More...
 
virtual void fill (char value)
 Fill the Sequence with the given value. More...
 
virtual int remove (char value)
 Remove all items with the given value. More...
 
virtual bool removeAt (Size position)
 Removes the item at the given position. More...
 
virtual void clear ()
 Removes all items from the Sequence. More...
 
virtual int compareTo (const Sequence< char > &s) const
 Compare this Sequence to another Sequence. More...
 
virtual bool equals (const Sequence< char > &s) const
 Test if this Sequence is equal to an other Sequence. More...
 
const char & operator[] (int i) const
 Returns the item at the given position in the Sequence. More...
 
const char & operator[] (Size i) const
 Returns the item at the given position in the Sequence. More...
 
char & operator[] (int i)
 Returns the item at the given position in the Sequence. More...
 
char & operator[] (Size i)
 Returns the item at the given position in the Sequence. More...
 
- Public Member Functions inherited from Container
 Container ()
 Constructor. More...
 
virtual ~Container ()
 Destructor. More...
 
virtual bool isEmpty () const
 Check if the Container is empty. More...
 
virtual bool shrink (Size size)
 Shrink the container size by the given amount of items. More...
 
virtual Size squeeze ()
 Try to minimize the memory required in the Container. More...
 
- Public Member Functions inherited from Comparable< Sequence< char > >
virtual ~Comparable ()
 Class destructor. More...
 

Static Public Member Functions

static Size length (char *str)
 Get the length of the given character string. More...
 
static Size length (const char *str)
 Get the length of a constant character string. More...
 

Private Attributes

char * m_string
 Current value of the String. More...
 
Size m_size
 Size of the string buffer, including any NULL byte(s) at the end. More...
 
Size m_count
 Length of the string text, excluding NULL byte(s) at the end. More...
 
bool m_allocated
 True if the string buffer is a deep copy, false otherwise. More...
 
Number::Base m_base
 Number format to use for convertions. More...
 

Detailed Description

Abstraction of strings.

Definition at line 41 of file String.h.

Constructor & Destructor Documentation

◆ String() [1/5]

String::String ( )

Default constructor.

Constructs an empty string with the default size.

Definition at line 22 of file String.cpp.

References Number::Dec, m_allocated, m_base, m_count, m_size, m_string, STRING_DEFAULT_SIZE, and ZERO.

◆ String() [2/5]

String::String ( const String str)

Copy constructor.

Parameters
strString reference.

Definition at line 32 of file String.cpp.

References MemoryBlock::copy(), m_allocated, m_base, m_count, m_size, and m_string.

◆ String() [3/5]

String::String ( char *  s,
const bool  copy = true 
)

Constructor.

Parameters
sInitial value of the String.
copyIf true allocate and copy the input buffer.

Definition at line 42 of file String.cpp.

References MemoryBlock::copy(), copy(), Number::Dec, length(), m_allocated, m_base, m_count, m_size, m_string, and STRING_DEFAULT_SIZE.

◆ String() [4/5]

String::String ( const char *  s,
const bool  copy = false 
)

Constant constructor.

Parameters
sInitial value of the String.
copyIf true allocate and copy the input buffer.

Definition at line 58 of file String.cpp.

References MemoryBlock::copy(), copy(), Number::Dec, length(), m_allocated, m_base, m_count, m_size, m_string, and STRING_DEFAULT_SIZE.

◆ String() [5/5]

String::String ( const int  number)

Signed integer constructor.

Creates a String with the textual representation of the given signed integer.

Parameters
numberInteger number of the new String.

Definition at line 74 of file String.cpp.

References Number::Dec, m_allocated, m_base, m_count, m_size, m_string, set(), STRING_DEFAULT_SIZE, and ZERO.

◆ ~String()

String::~String ( )
virtual

Destructor.

Definition at line 86 of file String.cpp.

References m_allocated, and m_string.

Member Function Documentation

◆ at()

const char & String::at ( const Size  position) const
virtual

Returns a reference to the item at the given position.

Parameters
positionValid index inside this array.
Returns
Reference to the item at the given position
Note
Position must be a valid index.

Implements Sequence< char >.

Definition at line 170 of file String.cpp.

References m_string.

◆ compareTo() [1/3]

int String::compareTo ( const char *  str,
const bool  caseSensitive = true,
const Size  count = 0 
) const
virtual

Compare with a character string.

Parameters
strCharacter string to compare against.
caseSensitiveTrue if uppercase characters are considered not equal to lowercase, false otherwise.
countNumber of character to compare or ZERO for whole strings.
Returns
Zero if equal, negative if smaller or positive if greater.

Definition at line 241 of file String.cpp.

References count(), Character::lower(), and m_string.

◆ compareTo() [2/3]

int String::compareTo ( const String str) const
virtual

Compares this String to the given String.

Parameters
strThe String to compare us to.
Returns
int < 0, 0, > 0 if we are greater than, equal to or less then the given String.

Definition at line 231 of file String.cpp.

Referenced by compareTo(), equals(), FileSystemClient::findMount(), Directory::get(), NetworkClient::initialize(), operator!=(), operator==(), FileSystemServer::redirectRequest(), and Directory::remove().

◆ compareTo() [3/3]

int String::compareTo ( const String str,
const bool  caseSensitive = true 
) const
virtual

Compare with another String.

Parameters
strString instance to compare against.
caseSensitiveTrue if uppercase characters are considered not equal to lowercase, false otherwise.
Returns
Zero if equal, negative if smaller or positive if greater.

Definition at line 236 of file String.cpp.

References compareTo(), and m_string.

◆ contains()

bool String::contains ( const char  character) const
virtual

Check if the given character occurs in the String.

Parameters
characterThe character to search for.
Returns
True if found, false otherwise.

Reimplemented from Sequence< char >.

Definition at line 180 of file String.cpp.

References m_count, and m_string.

◆ count()

Size String::count ( ) const
virtual

Number of characters in the string.

Returns
Number of characters

Implements Container.

Definition at line 100 of file String.cpp.

References m_count.

Referenced by compareTo(), length(), and reserve().

◆ endsWith() [1/2]

bool String::endsWith ( const char *  suffix) const

Tests if this String ends with the specified suffix.

Parameters
suffixThe suffix String.
Returns
True if matched, false otherwise.

Definition at line 215 of file String.cpp.

References length(), m_count, and m_string.

◆ endsWith() [2/2]

bool String::endsWith ( const String suffix) const

Tests if this String ends with the specified suffix.

Parameters
suffixThe suffix String.
Returns
True if matched, false otherwise.

Definition at line 210 of file String.cpp.

References m_string.

Referenced by Decompress::decompressFile(), and DirectoryScanner::scan().

◆ equals()

bool String::equals ( const String str) const
virtual

Alias for compareTo().

Parameters
strString instance to compare against

Definition at line 262 of file String.cpp.

References compareTo(), and m_string.

Referenced by ArgumentContainer::get(), LinnDirectory::getLinnDirectoryEntry(), BootSymbolStorage::loadSymbol(), and ProcessClient::processInfo().

◆ get()

const char * String::get ( const Size  position) const
virtual

Returns the item at the given position.

Parameters
positionThe position of the item to get.
Returns
Pointer to the item at the given position or ZERO if no item available.

Implements Sequence< char >.

Definition at line 165 of file String.cpp.

References m_count, m_string, and ZERO.

◆ length() [1/3]

Size String::length ( ) const

◆ length() [2/3]

Size String::length ( char *  str)
static

Get the length of the given character string.

Parameters
strInput string
Returns
String length in bytes

Definition at line 110 of file String.cpp.

References length().

◆ length() [3/3]

Size String::length ( const char *  str)
static

Get the length of a constant character string.

Parameters
strInput string
Returns
String length

Definition at line 115 of file String.cpp.

◆ lower()

String & String::lower ( )

Convert all Characters to lower case.

Returns
Reference to the String.

Definition at line 386 of file String.cpp.

References Character::lower(), m_count, m_string, and reserve().

◆ match()

bool String::match ( const char *  mask) const

Matches the String against a mask.

Parameters
maskPattern to match against.
Returns
True if match, false otherwise.

Definition at line 267 of file String.cpp.

References Character::isWildcard(), m_string, and ZERO.

Referenced by LinnCreate::insertDirectory().

◆ operator!=()

bool String::operator!= ( const String str) const

Inequal operator.

Parameters
strInput string

Definition at line 639 of file String.cpp.

References compareTo().

◆ operator*() [1/2]

char* String::operator* ( )

Dereference operator.

◆ operator*() [2/2]

char * String::operator* ( ) const

Dereference operator (read-only).

Definition at line 644 of file String.cpp.

References m_string.

◆ operator<<() [1/6]

String & String::operator<< ( const char *  str)

Append character string to the String.

Definition at line 654 of file String.cpp.

References MemoryBlock::copy(), length(), m_count, m_string, reserve(), and ZERO.

Referenced by operator<<().

◆ operator<<() [2/6]

String & String::operator<< ( const int  number)

Append the given signed number as text to the String.

Definition at line 673 of file String.cpp.

References m_base, m_count, m_string, reserve(), and set().

◆ operator<<() [3/6]

String & String::operator<< ( const Number::Base  format)

Change the default number format representation.

Definition at line 697 of file String.cpp.

References m_base.

◆ operator<<() [4/6]

String & String::operator<< ( const String str)

Append String to another String.

Definition at line 667 of file String.cpp.

References m_string, and operator<<().

◆ operator<<() [5/6]

String & String::operator<< ( const unsigned int  number)

Append the given unsigned number as text to the String.

Definition at line 681 of file String.cpp.

References m_base, m_count, m_string, reserve(), and setUnsigned().

◆ operator<<() [6/6]

String & String::operator<< ( const void *  pointer)

Append the pointer as hexidecimal unsigned number to the String.

Definition at line 689 of file String.cpp.

References Number::Hex, m_count, m_string, reserve(), and setUnsigned().

◆ operator=() [1/2]

void String::operator= ( const char *  str)

Assignment operator.

Parameters
strConstant string.

Definition at line 610 of file String.cpp.

References MemoryBlock::copy(), length(), m_count, m_string, reserve(), and ZERO.

◆ operator=() [2/2]

void String::operator= ( const String str)

Assignment operator.

Parameters
strInput string

Definition at line 622 of file String.cpp.

References MemoryBlock::copy(), length(), m_count, m_string, reserve(), and ZERO.

◆ operator==()

bool String::operator== ( const String str) const

Comparision operator.

Parameters
strInput string

Definition at line 634 of file String.cpp.

References compareTo().

◆ pad()

String & String::pad ( const Size  length)

Pad line with trailing whitespace.

This function ensures the last line inside the String is at least the given length in characters, by appending extra whitespace at the end of the line.

Parameters
lengthLength of the String including whitespace pads
Returns
Reference to the String

Definition at line 332 of file String.cpp.

References length(), m_count, m_string, reserve(), MemoryBlock::set(), and ZERO.

Referenced by ListFiles::printSingleFile().

◆ reserve()

bool String::reserve ( const Size  count)
virtual

Make sure at least given number of bytes available.

Parameters
countThe number of free bytes to guarantee.
Returns
True if success, false otherwise.

Reimplemented from Container.

Definition at line 157 of file String.cpp.

References count(), m_allocated, m_size, and resize().

Referenced by lower(), operator<<(), operator=(), pad(), setUnsigned(), trim(), and upper().

◆ resize()

bool String::resize ( const Size  size)
virtual

Change the size of the String buffer.

Parameters
sizeNew size of the String.
Returns
True if resized successfully, false otherwise.

Reimplemented from Container.

Definition at line 125 of file String.cpp.

References MemoryBlock::copy(), m_allocated, m_count, m_size, m_string, size(), and ZERO.

Referenced by reserve().

◆ set()

Size String::set ( const long  number,
const Number::Base  base = Number::Dec,
char *  string = ZERO 
)

Set text-representation of a signed number.

Parameters
numberNumber value to use
baseNumeric base of the given value
stringDestination character string buffer or ZERO to fill internal String buffer.

Definition at line 533 of file String.cpp.

References setUnsigned().

Referenced by itoa(), operator<<(), LogLevelFile::read(), and String().

◆ setUnsigned()

Size String::setUnsigned ( const ulong  number,
const Number::Base  base = Number::Dec,
char *  string = ZERO,
const bool  sign = false 
)

Set text-representation of an unsigned number.

Parameters
numberInput number.
baseNumberal base type.
stringDestination character string buffer or ZERO to fill internal String buffer.
signThreat input number as signed.

Definition at line 538 of file String.cpp.

References Number::Dec, Number::Hex, m_count, m_string, reserve(), and STRING_DEFAULT_SIZE.

Referenced by operator<<(), set(), and vsnprintf().

◆ size()

Size String::size ( ) const
virtual

Calculates the length of the String.

Returns
Length of the current String value.

Implements Container.

Definition at line 95 of file String.cpp.

References m_size.

Referenced by resize(), and substring().

◆ split() [1/2]

List< String > String::split ( const char  delimiter) const

Split the String into parts separated by a delimiter.

Parameters
delimiterCharacter to use a delimiter.
Returns
List of Strings representing the parts.

Definition at line 408 of file String.cpp.

References ZERO.

Referenced by Shell::exec(), ArgumentParser::parse(), MpiHost::parseHostsFile(), RecoveryServer::restartProcess(), MpiProxy::startLocalProcess(), MpiProxy::startRemoteProcess(), and IPV4::toAddress().

◆ split() [2/2]

List< String > String::split ( const String delimiter) const

Split the String into parts separated by a delimiter.

Parameters
delimiterString which acts as a delimiter.
Returns
List of Strings representing the parts.

Definition at line 416 of file String.cpp.

References List< T >::append(), copy(), m_count, m_string, and substring().

◆ startsWith() [1/2]

bool String::startsWith ( const char *  prefix) const

Tests if this String starts with the specified prefix.

Parameters
prefixString prefix.
Returns
True if matched, false otherwise.

Definition at line 194 of file String.cpp.

References length(), m_count, and m_string.

◆ startsWith() [2/2]

bool String::startsWith ( const String prefix) const

Tests if this String starts with the specified prefix.

Parameters
prefixString prefix.
Returns
True if matched, false otherwise.

Definition at line 189 of file String.cpp.

References m_string.

◆ substring()

String String::substring ( const Size  index,
const Size  size = 0 
) const

Returns a part of the String as a copy.

This function copies the input starting from index (inclusive), and copies at most size characters.

Parameters
indexThe begin index to create the substring of.
sizeThe maximum size of the substring.
Returns
String by value

Definition at line 314 of file String.cpp.

References m_count, m_string, size(), and ZERO.

Referenced by Decompress::decompressFile(), ArgumentParser::parse(), and split().

◆ toLong()

long String::toLong ( const Number::Base  base = Number::Dec) const

Convert the String to a signed long integer.

Parameters
baseNumberBase to use. Default is decimal.
Returns
Signed long integer

Definition at line 456 of file String.cpp.

References Number::Dec, Number::Hex, Character::isAlpha(), Character::isDigit(), Character::isUpper(), Character::isWhitespace(), LONG_MAX, LONG_MIN, and m_string.

Referenced by atoi(), NetSend::exec(), NetSend::initialize(), MpiTarget::initializeSlave(), main(), strtol(), IPV4::toAddress(), and LogLevelFile::write().

◆ trim()

String & String::trim ( )

Remove leading and trailing whitespace from the String.

Returns
Reference to the String.

Definition at line 357 of file String.cpp.

References MemoryBlock::copy(), Character::isWhitespace(), m_count, m_string, and reserve().

◆ upper()

String & String::upper ( )

Convert all Characters to upper case.

Returns
Reference to the String.

Definition at line 397 of file String.cpp.

References m_count, m_string, reserve(), and Character::upper().

◆ value()

const char String::value ( const Size  position) const
virtual

Return value at the given position.

If position is not within bounds of this array, this function will return a default constructed T.

Parameters
positionIndex inside this array.
Returns
T at the given position or default constructed T.

Definition at line 175 of file String.cpp.

References m_string.

Field Documentation

◆ m_allocated

bool String::m_allocated
private

True if the string buffer is a deep copy, false otherwise.

Definition at line 457 of file String.h.

Referenced by reserve(), resize(), String(), and ~String().

◆ m_base

Number::Base String::m_base
private

Number format to use for convertions.

Definition at line 460 of file String.h.

Referenced by operator<<(), and String().

◆ m_count

Size String::m_count
private

Length of the string text, excluding NULL byte(s) at the end.

Definition at line 454 of file String.h.

Referenced by contains(), count(), endsWith(), get(), lower(), operator<<(), operator=(), pad(), resize(), setUnsigned(), split(), startsWith(), String(), substring(), trim(), and upper().

◆ m_size

Size String::m_size
private

Size of the string buffer, including any NULL byte(s) at the end.

Definition at line 451 of file String.h.

Referenced by reserve(), resize(), size(), and String().

◆ m_string

char* String::m_string
private

The documentation for this class was generated from the following files: