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

#include <String.h>

+ Collaboration diagram for String:

Public Member Functions

 String ()
 
 String (const char *s)
 
 String (const char *s, size_t length)
 
 String (const String &x)
 
 String (String &&x)
 
Stringoperator= (String &&x)
 
Stringoperator= (const String &x)
 
Stringoperator= (const char *s)
 
 operator const char * () const
 
 operator StringView () const
 
Stringoperator+= (const String &x)
 
Stringoperator+= (const char *s)
 
bool operator== (const String &s) const
 
bool operator== (const StringView &s) const
 
bool operator== (const char *s) 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 ()
 
String split (size_t offset)
 
void split (size_t offset, String &back)
 
Vector< Stringtokenise (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
 
void void assign (const String &x)
 
void assign (const char *s, size_t len=0, bool unsafe=false)
 
void reserve (size_t size)
 
void free ()
 
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)
 

Private Member Functions

void reserve (size_t size, bool zero)
 
char * extract () const
 
void computeHash ()
 
uint32_t computeHash () const
 
void move (String &&other)
 
bool iswhitespace (const char c) const
 

Private Attributes

char * m_Data
 
const char * m_ConstData
 
size_t m_Length
 
size_t m_Size
 
char m_Static [StaticSize]
 
bool m_HeapData
 
uint32_t m_Hash
 

Static Private Attributes

static constexpr const size_t StaticSize = 64
 

Detailed Description

String class for ASCII strings

Todo:
provide documentation

Definition at line 49 of file String.h.

Constructor & Destructor Documentation

String::String ( )

The default constructor does nothing

Definition at line 27 of file String.cc.

References free(), m_Static, and move().

Referenced by StringView::substring().

+ Here is the caller graph for this function:

Member Function Documentation

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 290 of file String.cc.

References assert, computeHash(), m_ConstData, m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

void String::chomp ( )

Removes the last character from the string.

Definition at line 660 of file String.cc.

References computeHash(), extract(), m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

Referenced by FatDirectory::cacheDirectoryContents().

+ Here is the caller graph for this function:

void String::computeHash ( )
private

Recompute internal hash.

Definition at line 849 of file String.cc.

References extract(), m_Hash, and m_Length.

Referenced by assign(), chomp(), hash(), lchomp(), lstrip(), move(), rstrip(), StringView::setHashingEnable(), split(), and StringView::substring().

+ Here is the caller graph for this function:

uint32_t String::computeHash ( ) const
private

Recompute internal hash but don't store it.

Definition at line 861 of file String.cc.

References extract(), and m_Length.

String String::copy ( ) const

Copy the String object into a new String.

Definition at line 873 of file String.cc.

bool String::endswith ( const char  c) const

Does this string end with the given string?

Definition at line 710 of file String.cc.

References extract(), and m_Length.

Referenced by UnixSocketSyscalls::bind(), and FatDirectory::cacheDirectoryContents().

+ Here is the caller graph for this function:

char * String::extract ( ) const
private

Extract the correct string buffer for this string.

Definition at line 791 of file String.cc.

References m_ConstData, m_Data, m_HeapData, m_Length, m_Static, and StaticSize.

Referenced by chomp(), computeHash(), endswith(), find(), lchomp(), lstrip(), move(), nextCharacter(), prevCharacter(), rstrip(), split(), startswith(), tokenise(), and view().

+ Here is the caller graph for this function:

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 810 of file String.cc.

References extract(), and m_Length.

void String::free ( )
Note
free does not clear old static data

Definition at line 405 of file String.cc.

References m_Data, m_Hash, m_HeapData, m_Length, m_Size, and m_Static.

Referenced by split(), and String().

+ Here is the caller graph for this function:

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 218 of file String.cc.

References computeHash(), and m_Hash.

Referenced by move(), and StringView::setHashingEnable().

+ Here is the caller graph for this function:

uint32_t String::hash ( )

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

Definition at line 228 of file String.cc.

References computeHash(), and m_Hash.

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

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

Definition at line 786 of file String.cc.

Referenced by lstrip(), and rstrip().

+ Here is the caller graph for this function:

void String::lchomp ( )

Removes the first character from the string.

Definition at line 635 of file String.cc.

References computeHash(), extract(), m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

void String::lstrip ( )

Removes the whitespace from the start of the string.

Definition at line 466 of file String.cc.

References computeHash(), extract(), iswhitespace(), m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

Referenced by strip().

+ Here is the caller graph for this function:

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

Move another string into this one.

Definition at line 61 of file String.cc.

References computeHash(), extract(), hash(), m_ConstData, m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

Referenced by String().

+ Here is the caller graph for this function:

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 238 of file String.cc.

References extract().

String::operator StringView ( ) const
inline

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

Definition at line 85 of file String.h.

References FORMAT, and operator==().

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 244 of file String.cc.

References assert, ERROR, extract(), m_ConstData, m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

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

Internal doer for reserve()

Definition at line 368 of file String.cc.

References m_Data, m_HeapData, m_Size, m_Static, and StaticSize.

void String::rstrip ( )

Removes the whitespace from the end of the string.

Definition at line 501 of file String.cc.

References computeHash(), extract(), iswhitespace(), m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

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

+ Here is the caller graph for this function:

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 418 of file String.cc.

References computeHash(), extract(), free(), m_Data, m_Hash, m_HeapData, m_Length, m_Size, m_Static, and StaticSize.

bool String::startswith ( const char  c) const

Does this string start with the given string?

Definition at line 748 of file String.cc.

References extract(), and m_Length.

void String::strip ( )

Removes the whitespace from the both ends of the string.

Definition at line 460 of file String.cc.

References lstrip(), and rstrip().

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

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

Definition at line 579 of file String.cc.

References Vector< T >::clear(), extract(), m_Length, Vector< T >::pushBack(), and view().

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 543 of file String.cc.

Referenced by TextIO::translate().

+ Here is the caller graph for this function:

StringView String::view ( ) const

Get a StringView of this String.

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

Definition at line 880 of file String.cc.

References extract(), m_Hash, and m_Length.

Referenced by Filesystem::find(), VFS::find(), VFS::lookupFilesystem(), StringView::setHashingEnable(), tokenise(), and UnixSocket::writeBytewise().

+ Here is the caller graph for this function:

Member Data Documentation

const char* String::m_ConstData
private

Pointer to a constant version of the string.

Definition at line 206 of file String.h.

Referenced by assign(), extract(), move(), and prevCharacter().

char* String::m_Data
private

Pointer to the zero-terminated ASCII string

Definition at line 204 of file String.h.

Referenced by assign(), chomp(), extract(), free(), lchomp(), lstrip(), move(), prevCharacter(), reserve(), rstrip(), and split().

uint32_t String::m_Hash
private
bool String::m_HeapData
private

Is m_Data heap allocated? Used for e.g. constexpr strings.

Definition at line 214 of file String.h.

Referenced by assign(), chomp(), extract(), free(), lchomp(), lstrip(), move(), prevCharacter(), reserve(), rstrip(), and split().

size_t String::m_Length
private
size_t String::m_Size
private

The size of the reserved space for the string

Definition at line 210 of file String.h.

Referenced by assign(), chomp(), free(), ProcFs::initialise(), lchomp(), lstrip(), move(), prevCharacter(), reserve(), PciDevicesFile::resync(), rstrip(), and split().

char String::m_Static[StaticSize]
private

Static string storage (avoid heap overhead for small strings)

Definition at line 212 of file String.h.

Referenced by assign(), chomp(), extract(), free(), lchomp(), lstrip(), move(), prevCharacter(), reserve(), rstrip(), split(), and String().

constexpr const size_t String::StaticSize = 64
staticprivate

Size of static string storage (over this threshold, the heap is used)

Definition at line 202 of file String.h.

Referenced by assign(), chomp(), extract(), lchomp(), lstrip(), move(), prevCharacter(), reserve(), rstrip(), and split().


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