libbio
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
bio::String Class Reference

#include <String.h>

Inheritance diagram for bio::String:
Inheritance graph
[legend]
Collaboration diagram for bio::String:
Collaboration graph
[legend]

Public Types

enum  Mode {
  INVALID = 0 , READ_ONLY , COPY_ON_WRITE , READ_WRITE ,
  HANDOFF , MODE_MAX
}
 

Public Member Functions

 String (Mode mode=INVALID)
 
 String (const char *string)
 
 String (::std::string string)
 
 String (const ImmutableString &string)
 
 String (const String &toCopy)
 
virtual ~String ()
 
virtual Stringoperator= (const String &toCopy)
 
virtual const Stringoperator= (const String &toMoveHack) const
 
virtual Stringoperator= (const ImmutableString &toAssign)
 
virtual Stringoperator= (const char *string)
 
virtual Stringoperator= (::std::string string)
 
virtual Stringoperator+= (const String &other)
 
virtual bool operator== (const ImmutableString &other) const
 
virtual bool operator== (const String &other) const
 
virtual bool operator== (const char *other) const
 
virtual bool operator== (const ::std::string &other) const
 
virtual operator::std::string () const
 
virtual operator bool () const
 
virtual Mode GetMode () const
 
virtual String SubString (::std::size_t start, ::std::size_t length) const
 
ImmutableString View () const
 
virtual bool StartsWith (const String &other, bool caseSensitive=true) const
 
virtual bool EndsWith (const String &other, bool caseSensitive=true) const
 
virtual bool Contains (const String &other, bool caseSensitive=true) const
 
virtual ::std::string AsStdString () const
 
virtual const charAsCharString () const
 
virtual bool IsBoolean () const
 
virtual bool AsBool () const
 
virtual int32_t AsInt () const
 
virtual uint32_t AsUInt () const
 
virtual float AsFloat () const
 
virtual float IsNumeric () const
 
virtual Number AsNumber () const
 
- Public Member Functions inherited from bio::ImmutableString
BIO_CONSTEXPR ImmutableString ()
 
BIO_CONSTEXPR ImmutableString (const char *string)
 
 ImmutableString (const char *string, const ::std::size_t length)
 
BIO_CONSTEXPR ImmutableString (const ImmutableString &toCopy)
 
BIO_CONSTEXPR ImmutableStringoperator= (const ImmutableString &toCopy)
 
BIO_CONSTEXPR const charData () const
 
BIO_CONSTEXPR::std::size_t Length () const
 
BIO_CONSTEXPR bool Empty () const
 
BIO_CONSTEXPR::std::size_t Find (const ImmutableString &substring) const
 
BIO_CONSTEXPR::std::size_t FirstDifference (const ImmutableString &other) const
 
BIO_CONSTEXPR ImmutableString GetImmutableSubString (const ::std::size_t start, const ::std::size_t length) const
 

Static Public Member Functions

static const charGetCloneOf (const char *source, ::std::size_t length=0)
 
static StringSetMode (const String &string, Mode desiredMode)
 
template<typename T >
static String From (const T &value)
 

Protected Member Functions

virtual void Clear ()
 

Protected Attributes

Mode mMode
 
- Protected Attributes inherited from bio::ImmutableString
const charmString
 
::std::size_t mLength
 

Detailed Description

We use our own String implementation in order to ensure consistency across the library and to provide a single point of optimization for all String operations.
We would like bio::Strings to encompass both the compile-time behavior of std::string_view and run-time versatility of std::string while being fully backwards compatible.

Like other Biology classes, String is inheritable and designed to be extended.

String is implemented as the addition of mutability to ImmutableString by way of a non-trivial destructor with the ability to allocate and delete data.
ImmutableStrings can be used in constexpr while Strings cannot.

Strings have different Modes. These Modes indicate how to handle internal operations.
The Mode of a String is set depending on how it is constructed.
You may change the String::Mode using the static SetMode method, which will produce a new String.
A READ_ONLY String cannot be set by any means. If you wish to edit a READ_ONLY String, you must SetMode first.
A COPY_ON_WRITE String will automatically become a READ_WRITE String when written to.
NOTE: There are no "WRITE" operation for COPY_ON_WRITE to use. This will likely change in a future release.

Member Enumeration Documentation

◆ Mode

Enumerator
INVALID 
READ_ONLY 
COPY_ON_WRITE 
READ_WRITE 
HANDOFF 
MODE_MAX 

Constructor & Destructor Documentation

◆ String() [1/5]

bio::String::String ( Mode  mode = INVALID)

An empty string will point to NULL, have a length of 0.
You may specify the Mode of an empty String.

◆ String() [2/5]

bio::String::String ( const char string)

Constructing a String from a const char* clones the provided contents into READ_WRITE storage.
Use ImmutableString or View() for borrowed read-only access.

Parameters
string

◆ String() [3/5]

bio::String::String ( ::std::string  string)

Constructing a String from a std::string basically gives back an std::string.
std::strings can only ever READ_WRITE, since their c_str() method goes out of scope on subsequent access.

Parameters
string

◆ String() [4/5]

bio::String::String ( const ImmutableString string)

ImmutableStrings are treated as const char* and will yield a READ_ONLY String.

Parameters
string

◆ String() [5/5]

bio::String::String ( const String toCopy)
Parameters
toCopy

◆ ~String()

bio::String::~String ( )
virtual

Member Function Documentation

◆ AsBool()

bool bio::String::AsBool ( ) const
virtual

Convert "true" or "false" to bool.
This is essentially just {== "true"} with no extra test for "false".
This behavior may change in a future release.
Case insensitive.

Returns
*this as a bool, false by default.

◆ AsCharString()

const char * bio::String::AsCharString ( ) const
virtual

Get a new const char* from *this.
YOU MUST delete THE RETURNED VALUE TO AVOID MEMORY LEAKS!

Returns
a new const char* copy of *this.

◆ AsFloat()

float bio::String::AsFloat ( ) const
virtual

convert *this to a float.

Returns
*this as a float; 0.0f by default.

◆ AsInt()

int32_t bio::String::AsInt ( ) const
virtual

convert *this to an integer.

Returns
*this as an integer; 0 by default.

◆ AsNumber()

Number bio::String::AsNumber ( ) const
virtual

Convert *this to a number.
Uses the most permissive numeric notation available.

Returns
*this as a Number; 0.0f by default.

◆ AsStdString()

std::string bio::String::AsStdString ( ) const

Get *this as an std::string.

Returns
*this as an std::string.

◆ AsUInt()

uint32_t bio::String::AsUInt ( ) const
virtual

convert *this to an unsigned integer.

Returns
*this as an unsigned integer; 0 by default.

◆ Clear()

void bio::String::Clear ( )
protectedvirtual

Will delete mString if *this is READ_WRITE, etc.

◆ Contains()

bool bio::String::Contains ( const String other,
bool  caseSensitive = true 
) const
virtual

Check if *this contains other.

Parameters
other
Returns
whether or not *this contains other.

◆ EndsWith()

bool bio::String::EndsWith ( const String other,
bool  caseSensitive = true 
) const
virtual

Check if *this ends with other.

Parameters
other
Returns
whether or not *this ends with other.

◆ From()

template<typename T >
static String bio::String::From ( const T value)
inlinestatic

Converts the given value to a string.

Parameters
value

◆ GetCloneOf()

const char * bio::String::GetCloneOf ( const char source,
::std::size_t  length = 0 
)
static

Copies the contents of source, up to length characters, into a new const char*.
NOTE: because "new" is used here, a delete needs to be called elsewhere.

Parameters
source
lengthif 0, the whole source is copied.
Returns
a new const char* with the contents of source.

◆ GetMode()

String::Mode bio::String::GetMode ( ) const
virtual
Returns
the Mode of *this.

◆ IsBoolean()

bool bio::String::IsBoolean ( ) const
virtual

Check if *this can be cast AsBool().

Returns
whether or not *this contains only text that can be converted into a boolean value.

◆ IsNumeric()

float bio::String::IsNumeric ( ) const
virtual

Check if this can be cast AsNumber().
In order to make this method more useful than just a simple boolean check, the return format is encoded such that you are given details on the numeric format of *this.
The format is like -?count(\d+).count(\d
), where the leading - is optional, first count is the number of digits before the decimal point, and the second count is the number of digits after the decimal point.
A number like 123 would return 3.0f, since it has 3 digits before the decimal point and 0 after.
A number like -1234.567 would return -4.3f, since it has 4 digits before the decimal point and 3 after and is negative.
A number like -.123 would return -0.3f, since it has 0 digits before the decimal point and 3 after.
You can use this to check if *this can be cast as a UInt, etc. (e.g. check = maybeUint.IsNumeric(); check > 1 && !std::fmod(check, 1.0).

Returns
a float with the number of numbers before and after the decimal point.

◆ operator bool()

bio::String::operator bool ( ) const
virtual

You can implicitly treat Strings as booleans the same way you do pointers (specifically char*).
NOTE: THIS IS NOT this->AsBool()!!!

Returns
whether or not *this is empty.

◆ operator+=()

String & bio::String::operator+= ( const String other)
virtual

Append other to *this.

Parameters
other
Returns
the result of appending other to *this.

◆ operator::std::string()

bio::String::operator::std::string ( ) const
virtual
Returns
*this as an std::string.

◆ operator=() [1/5]

String & bio::String::operator= ( ::std::string  string)
virtual

NOTE: Assigning *this an std::string forces it to be READ_WRITE!

Parameters
string
Returns
*this.

◆ operator=() [2/5]

String & bio::String::operator= ( const char string)
virtual
Parameters
string
Returns
*this.

◆ operator=() [3/5]

String & bio::String::operator= ( const ImmutableString toAssign)
virtual
Parameters
toAssign
Returns
*this.

◆ operator=() [4/5]

String & bio::String::operator= ( const String toCopy)
virtual

NOTE: Mode is NOT copied.

Parameters
toCopy
Returns
*this.

◆ operator=() [5/5]

const String & bio::String::operator= ( const String toMoveHack) const
virtual

This is just lies: we const_cast(this) and change the values..
This is used in places where a move operation would be performed but since that can't be done prior to c++11, we hack it.
All Values are moved, including Mode,

Parameters
toMoveHack
Returns
*this.

◆ operator==() [1/4]

bool bio::String::operator== ( const ::std::string &  other) const
virtual
Parameters
other
Returns
whether or not the contents of *this match those of other.

◆ operator==() [2/4]

bool bio::String::operator== ( const char other) const
virtual
Parameters
other
Returns
whether or not the contents of *this match those of other.

◆ operator==() [3/4]

bool bio::String::operator== ( const ImmutableString other) const
virtual

NOTE: We ignore Mode when comparing Strings.

Parameters
other
Returns
whether or not the contents of *this match those of other.

◆ operator==() [4/4]

bool bio::String::operator== ( const String other) const
virtual

NOTE: We ignore Mode when comparing Strings.

Parameters
other
Returns
whether or not the contents of *this match those of other.

◆ SetMode()

String & bio::String::SetMode ( const String string,
Mode  desiredMode 
)
static

In order to change the Mode of a String, you must create a new String.
UNDEFINED BEHAVIOR if you:

  1. create a READ_ONLY version of a READ_WRITE String and the source string becomes inaccessible.
  2. create a COPY_ON_WRITE String which does not become READ_WRITE before the source string becomes inaccessible.
    Parameters
    string
    desiredMode
    Returns
    a new String with the desiredMode or a String(), if the desiredMode is not valid.

◆ StartsWith()

bool bio::String::StartsWith ( const String other,
bool  caseSensitive = true 
) const
virtual

Check if *this starts with other.

Parameters
other
Returns
whether or not *this starts with other.

◆ SubString()

String bio::String::SubString ( ::std::size_t  start,
::std::size_t  length 
) const
virtual

Get a smaller string from *this.

Parameters
start
end
Returns
a READ_ONLY String consisting of the contents of *this between start and end.

◆ View()

ImmutableString bio::String::View ( ) const

Get a borrowed read-only view of *this without allocating or copying.
The returned view remains valid until *this is destroyed or mutated in a way that replaces the backing storage.

Returns
a non-owning view into the current contents of *this.

Member Data Documentation

◆ mMode

Mode bio::String::mMode
protected

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