The Pedigree Project 0.1
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
String Class Reference

#include <String.h>

+ Inheritance diagram for String:
+ Collaboration diagram for String:

Public Member Functions

 String ()
 
 String (const char *s)
 
 String (const char *s, size_t length)
 
 String (const char *s, size_t length, bool unsafe)
 
 String (const Cord &x)
 
 String (const String &x)
 
 String (const StringView &x)
 
 String (String &&x) noexcept
 
String & operator= (String &&x) noexcept
 
String & operator= (const Cord &x)
 
String & operator= (const String &x)
 
String & operator= (const char *s)=delete
 
 operator const char * () const
 
 operator StringView () const
 
String & operator+= (const String &x)
 
String & operator+= (const char *s)
 
bool operator== (const String &s) const
 
bool operator== (const StringView &s) const
 
bool operator== (const char *s) const
 
bool compare (const char *s, size_t len) const
 
template<size_t N>
bool compare (const char(&s)[N]) const
 
char operator[] (size_t i) const
 
const char * cstr () const
 
size_t length () const
 
size_t size () const
 
uint32_t hash () const
 
uint32_t hash ()
 
size_t nextCharacter (size_t c) const
 
size_t prevCharacter (size_t c) const
 
void lchomp ()
 
void chomp ()
 
void strip ()
 
void lstrip ()
 
void rstrip ()
 
void ltrim (size_t n)
 
void rtrim (size_t n)
 
String split (size_t offset)
 
void split (size_t offset, String &back)
 
Vector< String > tokenise (char token)
 
void tokenise (char token, Vector< String > &output) const
 
void tokenise (char token, Vector< StringView > &output) const
 
void Format (const char *format,...) FORMAT(printf
 
template<size_t N>
void void assign (const char(&s)[N])
 
void assign (const String &x)
 
void assign (const Cord &x)
 
void assign (const char *s, size_t len=0, bool unsafe=false)
 
void reserve (size_t size)
 
virtual void clear () noexcept
 
void downsize ()
 
bool endswith (const char c) const
 
bool endswith (const String &s) const
 
bool endswith (const char *s, size_t len=0) const
 
bool startswith (const char c) const
 
bool startswith (const String &s) const
 
bool startswith (const char *s, size_t len=0) const
 
ssize_t find (const char c) const
 
ssize_t rfind (const char c) const
 
String copy () const
 
StringView view () const
 

Static Public Member Functions

static size_t Utf32ToUtf8 (uint32_t utf32, char *utf8)
 

Protected Member Functions

bool iswhitespace (const char c) const
 
void reserve (size_t size, bool zero)
 
void computeHash ()
 
uint32_t computeHash () const
 
void setLength (size_t n)
 
void setSize (size_t n)
 

Private Member Functions

uint32_t maybeHash () const
 
virtual char * extract () const
 
void move (String &&other) noexcept
 
virtual bool resizable () const
 
virtual bool assignable () const
 

Private Attributes

char * m_Data
 
size_t m_Length
 
size_t m_Size
 
uint32_t m_Hash
 

Detailed Description

String class for ASCII strings

Todo:
provide documentation

Definition at line 43 of file String.h.

Constructor & Destructor Documentation

◆ String() [1/8]

String::String ( )

The default constructor does nothing

Definition at line 32 of file String.cc.

◆ String() [2/8]

String::String ( const char *  s)
explicit

Definition at line 34 of file String.cc.

◆ String() [3/8]

String::String ( const char *  s,
size_t  length 
)
explicit

Definition at line 38 of file String.cc.

◆ String() [4/8]

String::String ( const char *  s,
size_t  length,
bool  unsafe 
)
explicit

Definition at line 42 of file String.cc.

◆ String() [5/8]

String::String ( const Cord &  x)
explicit

Definition at line 58 of file String.cc.

◆ String() [6/8]

String::String ( const String &  x)

Definition at line 46 of file String.cc.

◆ String() [7/8]

String::String ( const StringView &  x)
explicit

Definition at line 50 of file String.cc.

◆ String() [8/8]

String::String ( String &&  x)
noexcept

Definition at line 54 of file String.cc.

◆ ~String()

String::~String ( )
virtual

Definition at line 62 of file String.cc.

Member Function Documentation

◆ assign() [1/4]

void String::assign ( const char *  s,
size_t  len = 0,
bool  unsafe = false 
)

Assign a buffer to this string. Optionally, unsafe can be passed which will completely trust the len parameter. This may be useful for cases where the input string is not necessarily known to have a null terminator, as otherwise assign() will attempt to find the length of the given string to reduce memory usage.

Definition at line 301 of file String.cc.

References assignable(), copy(), extract(), m_Data, m_Hash, m_Length, and m_Size.

◆ assign() [2/4]

template<size_t N>
void void String::assign ( const char(&)  s[N])
inline

Definition at line 173 of file String.h.

◆ assign() [3/4]

void String::assign ( const Cord &  x)

Definition at line 283 of file String.cc.

◆ assign() [4/4]

void String::assign ( const String &  x)

Definition at line 251 of file String.cc.

◆ assignable()

bool String::assignable ( ) const
privatevirtual

Is this string assignable?

Reimplemented in ConstantString< N >.

Definition at line 793 of file String.cc.

Referenced by assign(), chomp(), lchomp(), lstrip(), ltrim(), rstrip(), rtrim(), and strip().

+ Here is the caller graph for this function:

◆ chomp()

void String::chomp ( )

Removes the last character from the string.

Definition at line 628 of file String.cc.

References assignable(), extract(), m_Hash, and m_Length.

◆ clear()

void String::clear ( )
virtualnoexcept

Definition at line 403 of file String.cc.

◆ compare() [1/2]

bool String::compare ( const char *  s,
size_t  len 
) const

Perform a string comparison with the given string.

Definition at line 202 of file String.cc.

References extract(), m_Length, and UNLIKELY.

Referenced by Ext2Directory::addEntry(), Directory::enumerate(), Ext2Filesystem::removeNode(), and PedigreeCSyscallManager::syscall().

+ Here is the caller graph for this function:

◆ compare() [2/2]

template<size_t N>
bool String::compare ( const char(&)  s[N]) const
inline

Perform a string comparison with the given constant string. You should use this rather than operator== for these types of comparisons as this uses a length hint to potentially avoid an actual comparison of the string contents.

Definition at line 96 of file String.h.

◆ computeHash() [1/2]

void String::computeHash ( )
protected

Recompute internal hash.

Definition at line 761 of file String.cc.

References extract(), m_Hash, and m_Length.

Referenced by hash(), and hash().

+ Here is the caller graph for this function:

◆ computeHash() [2/2]

uint32_t String::computeHash ( ) const
protected

Recompute internal hash but don't store it.

Definition at line 769 of file String.cc.

References extract(), and m_Length.

◆ copy()

String String::copy ( ) const

Copy the String object into a new String.

Definition at line 777 of file String.cc.

Referenced by assign().

+ Here is the caller graph for this function:

◆ cstr()

const char * String::cstr ( ) const
inline

Definition at line 103 of file String.h.

◆ downsize()

void String::downsize ( )

Resize the buffer to fit the actual string.

Definition at line 385 of file String.cc.

References m_Data, m_Length, m_Size, and resizable().

◆ endswith() [1/3]

bool String::endswith ( const char *  s,
size_t  len = 0 
) const

Definition at line 672 of file String.cc.

◆ endswith() [2/3]

bool String::endswith ( const char  c) const

Does this string end with the given string?

Definition at line 654 of file String.cc.

References extract(), and m_Length.

Referenced by UnixSocketSyscalls::bind().

+ Here is the caller graph for this function:

◆ endswith() [3/3]

bool String::endswith ( const String &  s) const

Definition at line 663 of file String.cc.

◆ extract()

char * String::extract ( ) const
privatevirtual

Extract the correct string buffer for this string.

Reimplemented in ConstantString< N >.

Definition at line 724 of file String.cc.

References m_Data.

Referenced by assign(), chomp(), compare(), computeHash(), computeHash(), endswith(), find(), lchomp(), lstrip(), nextCharacter(), operator==(), prevCharacter(), rstrip(), startswith(), tokenise(), and view().

+ Here is the caller graph for this function:

◆ find()

ssize_t String::find ( const char  c) const

Searches

Todo:
this problem only exists because we use return -1 as a status indicator. That could be changed, and we could avoid a signed/unsigned conversion here!

Definition at line 728 of file String.cc.

References extract(), and m_Length.

◆ Format()

void String::Format ( const char *  format,
  ... 
)

Definition at line 642 of file String.cc.

◆ hash() [1/2]

uint32_t String::hash ( )

Variant of hash() that computes the hash if needed.

Definition at line 229 of file String.cc.

References computeHash(), and m_Hash.

◆ hash() [2/2]

uint32_t String::hash ( ) const

Variant of hash() that might compute the hash if needed, but won't update the stored hash.

Definition at line 221 of file String.cc.

References computeHash(), and m_Hash.

Referenced by operator==().

+ Here is the caller graph for this function:

◆ iswhitespace()

bool String::iswhitespace ( const char  c) const
protected

Is the given character whitespace? (for *strip())

Definition at line 720 of file String.cc.

Referenced by lstrip(), and rstrip().

+ Here is the caller graph for this function:

◆ lchomp()

void String::lchomp ( )

Removes the first character from the string.

Definition at line 614 of file String.cc.

References assignable(), extract(), m_Hash, and m_Length.

◆ length()

size_t String::length ( ) const
inline

Definition at line 107 of file String.h.

◆ lstrip()

void String::lstrip ( )

Removes the whitespace from the start of the string.

Definition at line 482 of file String.cc.

References assignable(), extract(), iswhitespace(), m_Hash, and m_Length.

Referenced by strip().

+ Here is the caller graph for this function:

◆ ltrim()

void String::ltrim ( size_t  n)

Remove the first N characters from the string.

Definition at line 415 of file String.cc.

References assignable(), m_Data, m_Hash, and m_Length.

Referenced by RadixTree< T >::insert().

+ Here is the caller graph for this function:

◆ maybeHash()

uint32_t String::maybeHash ( ) const
private

Extract hash without recomputing it.

Definition at line 237 of file String.cc.

References m_Hash.

Referenced by operator==().

+ Here is the caller graph for this function:

◆ move()

void String::move ( String &&  other)
privatenoexcept

Move another string into this one.

Definition at line 66 of file String.cc.

◆ nextCharacter()

size_t String::nextCharacter ( size_t  c) const

Given a character index, return the index of the next character, interpreting the string as UTF-8 encoded.

Definition at line 241 of file String.cc.

References extract().

◆ operator const char *()

String::operator const char * ( ) const
inlineexplicit

The const char * operator needs to be explicit to avoid unintentional conversion to const char * where a String& would be preferable. cstr() is the better option for obtaining a const char * from a String.

Definition at line 71 of file String.h.

◆ operator StringView()

String::operator StringView ( ) const
inline

Allow implicit typecasts to StringView for passing String to functions taking a StringView.

Definition at line 76 of file String.h.

◆ operator+=() [1/2]

String & String::operator+= ( const char *  s)

Definition at line 132 of file String.cc.

◆ operator+=() [2/2]

String & String::operator+= ( const String &  x)

Definition at line 103 of file String.cc.

◆ operator=() [1/4]

String & String::operator= ( const char *  s)
delete

Definition at line 92 of file String.cc.

◆ operator=() [2/4]

String & String::operator= ( const Cord &  x)

Definition at line 98 of file String.cc.

◆ operator=() [3/4]

String & String::operator= ( const String &  x)

Definition at line 86 of file String.cc.

◆ operator=() [4/4]

String & String::operator= ( String &&  x)
noexcept

Definition at line 81 of file String.cc.

◆ operator==() [1/3]

bool String::operator== ( const char *  s) const

Definition at line 184 of file String.cc.

◆ operator==() [2/3]

bool String::operator== ( const String &  s) const
Note
Even if the hashes don't exist yet, we still calculate them. The hash functions are faster than StringMatch as they operate on larger sections of the string at one time. The downside is that the worst case performance for comparison is therefore comparing two strings that do in fact match.

Definition at line 156 of file String.cc.

References extract(), hash(), LIKELY, m_Hash, m_Length, and maybeHash().

◆ operator==() [3/3]

bool String::operator== ( const StringView &  s) const

Definition at line 179 of file String.cc.

◆ operator[]()

char String::operator[] ( size_t  i) const

Definition at line 215 of file String.cc.

◆ prevCharacter()

size_t String::prevCharacter ( size_t  c) const

Given a character index, return the index of the previous character, interpreting the string as UTF-8 encoded.

Definition at line 246 of file String.cc.

References extract().

◆ reserve() [1/2]

void String::reserve ( size_t  size)

Definition at line 363 of file String.cc.

◆ reserve() [2/2]

void String::reserve ( size_t  size,
bool  zero 
)
protected

Internal doer for reserve()

Definition at line 367 of file String.cc.

References m_Data, m_Size, and resizable().

◆ resizable()

bool String::resizable ( ) const
privatevirtual

Is this string resizable?

Reimplemented in ConstantString< N >.

Definition at line 789 of file String.cc.

Referenced by downsize(), and reserve().

+ Here is the caller graph for this function:

◆ rfind()

ssize_t String::rfind ( const char  c) const

Definition at line 747 of file String.cc.

◆ rstrip()

void String::rstrip ( )

Removes the whitespace from the end of the string.

Definition at line 507 of file String.cc.

References assignable(), extract(), iswhitespace(), m_Hash, and m_Length.

Referenced by Symlink::initialise(), and strip().

+ Here is the caller graph for this function:

◆ rtrim()

void String::rtrim ( size_t  n)

Remove the last N characters from the string.

Definition at line 434 of file String.cc.

References assignable(), m_Data, m_Hash, and m_Length.

◆ setLength()

void String::setLength ( size_t  n)
protected

Internal setter for length.

Definition at line 797 of file String.cc.

References m_Length.

◆ setSize()

void String::setSize ( size_t  n)
protected

Internal setter for size.

Definition at line 801 of file String.cc.

References m_Size.

◆ size()

size_t String::size ( ) const
inline

Definition at line 111 of file String.h.

◆ split() [1/2]

String String::split ( size_t  offset)

Splits the string at the given offset - the front portion will be kept in this string, the back portion (including the character at 'offset' will be returned in a new string.

Definition at line 452 of file String.cc.

References split().

Referenced by split().

+ Here is the caller graph for this function:

◆ split() [2/2]

void String::split ( size_t  offset,
String &  back 
)

Definition at line 458 of file String.cc.

◆ startswith() [1/3]

bool String::startswith ( const char *  s,
size_t  len = 0 
) const

Definition at line 705 of file String.cc.

◆ startswith() [2/3]

bool String::startswith ( const char  c) const

Does this string start with the given string?

Definition at line 687 of file String.cc.

References extract(), and m_Length.

◆ startswith() [3/3]

bool String::startswith ( const String &  s) const

Definition at line 696 of file String.cc.

◆ strip()

void String::strip ( )

Removes the whitespace from the both ends of the string.

Definition at line 475 of file String.cc.

References assignable(), lstrip(), and rstrip().

◆ tokenise() [1/3]

Vector< String > String::tokenise ( char  token)

Definition at line 532 of file String.cc.

◆ tokenise() [2/3]

void String::tokenise ( char  token,
Vector< String > &  output 
) const

Definition at line 603 of file String.cc.

◆ tokenise() [3/3]

void String::tokenise ( char  token,
Vector< StringView > &  output 
) const

No-copy version of tokenise() that provides views instead of Strings

Definition at line 566 of file String.cc.

References extract(), m_Length, and view().

◆ Utf32ToUtf8()

size_t String::Utf32ToUtf8 ( uint32_t  utf32,
char *  utf8 
)
static

Converts a UTF-32 character to its UTF-8 representation.

Parameters
[in]utf32Input UTF-32 character.
[out]utf8Pointer to a buffer at least 6 bytes long.
Returns
The number of bytes in the UTF-8 string.

Definition at line 538 of file String.cc.

◆ view()

StringView String::view ( ) const

Get a StringView of this String.

Note
this view may become invalid if the String is modified.

Definition at line 783 of file String.cc.

References extract(), m_Hash, and m_Length.

Referenced by Ext2Directory::addEntry(), FatDirectory::addEntry(), Directory::addEphemeralFile(), VFS::find(), VFS::findRetained(), Symlink::followLinkRetained(), File::publishEvent(), Filesystem::removeChild(), and tokenise().

+ Here is the caller graph for this function:

Member Data Documentation

◆ m_Data

char* String::m_Data
private

Pointer to the zero-terminated ASCII string

Definition at line 232 of file String.h.

Referenced by assign(), downsize(), extract(), ltrim(), reserve(), and rtrim().

◆ m_Hash

uint32_t String::m_Hash
private

Hash of the string.

Definition at line 238 of file String.h.

Referenced by assign(), chomp(), computeHash(), hash(), hash(), lchomp(), lstrip(), ltrim(), maybeHash(), operator==(), rstrip(), rtrim(), and view().

◆ m_Length

size_t String::m_Length
private

◆ m_Size

size_t String::m_Size
private

The size of the reserved space for the string

Definition at line 236 of file String.h.

Referenced by assign(), downsize(), reserve(), and setSize().


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