Gorgon Game Engine
Gorgon::String Namespace Reference

Contains string related functions and classes. This namespace is not yet utf8 aware. More...

Classes

struct  CanBeStringified
 
struct  CaseInsensitiveLess
 
class  IllegalTokenError
 This error will be thrown if a parsing function encounters with an illegal token. More...
 
class  ParseError
 This error will be thrown if a parsing function encounters with a general error. More...
 
class  Tokenizer
 Tokenizer is a forward iterator that tokenizes a given string. More...
 

Enumerations

enum  LineEnding {
  None, LF, Unix, CR,
  Mac, CRLF, Standard, Windows,
  Mixed
}
 Line ending types. More...
 
enum  QuoteType { None, Single, Double, Both }
 

Functions

bool AppendUnicode (std::string &s, Char c)
 Appends a unicode code point to the string. More...
 
int CaseInsensitiveCompare (const std::string &left, const std::string &right)
 Compares two strings case insensitive. Works similar to strcmp. More...
 
template<class ... P_>
std::string Concat (const P_ &... rest)
 Streams the given parameters into a stringstream and returns the result, effectively concatinating all parameters. More...
 
std::string Extract (std::string &original, char marker, bool trim=false)
 Extracts the part of the string up to the given marker. More...
 
std::string Extract (std::string &original, const std::string &marker, bool trim=false)
 Extracts the part of the string up to the given marker. More...
 
std::string Extract_UseQuotes (std::string &original, char marker, QuoteType quotetype=QuoteType::Both)
 Extracts the part of the string up to the given marker. More...
 
std::string FixLineEndings (const std::string &in, LineEnding type=LineEnding::Standard)
 Fixes/changes line endings. More...
 
std::string From (const Resource::GID::Type &value)
 Creates a string from a GID. More...
 
template<class T_ >
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From (const T_ &e)
 
template<class T_ >
std::string From (const T_ &item)
 Creates a string from the given data. More...
 
bool InsertUnicode (std::string &s, std::size_t pos, Char c)
 Appends a unicode code point to the string. More...
 
template<class T_ >
std::string Join (const T_ &vec, const std::string &glue=", ")
 Joins a list of strings to a single string using the given glue text. More...
 
std::string PadEnd (std::string str, std::size_t len, char pad=' ')
 Pads the string to the given number of characters from the start. More...
 
std::string PadStart (std::string str, std::size_t len, char pad=' ')
 Pads the string to the given number of characters from the start. More...
 
template<class T_ >
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, T_ >::type Parse (const std::string &text)
 
std::string Replace (std::string str, const std::string &find, const std::string &replace)
 String replace that does not use regex. More...
 
template<class T_ >
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, T_ >::type To (const std::string &text)
 
template<class T_ >
T_ To (const std::string &value)
 Converts a string to another type. More...
 
std::string ToLower (std::string str)
 Converts the given string to lowercase. More...
 
std::string ToUpper (std::string str)
 Converts the given string to uppercase. More...
 
std::string Trim (std::string str, const std::string &chars=" \t\n\r")
 Strips whitespace around the given string both from start and end. More...
 
std::string TrimEnd (std::string str, const std::string &chars=" \t\n\r")
 Strips the whitespace at the end of a string. More...
 
std::string TrimStart (std::string str, const std::string &chars=" \t\n\r")
 Strips the whitespace from the start of a string. More...
 
int UnicodeGlyphCount (const std::string &s)
 
int UnicodeUTF8Bytes (Char c)
 
int UTF8Bytes (char c)
 Returns the number of bytes used by the next UTF8 codepoint. More...
 

Detailed Description

Contains string related functions and classes. This namespace is not yet utf8 aware.

Enumeration Type Documentation

◆ LineEnding

enum LineEnding
strong

Line ending types.

Enumerator
None 

None, no line endings.

LF 

line feed \x0a

Unix 

line feed \x0a

CR 

carriage return \x0d

Mac 

carriage return \x0d

CRLF 

\x0d\x0a

Standard 

\x0d\x0a

Windows 

\x0d\x0a

Mixed 

When there are multiple types of line endings present.

◆ QuoteType

enum QuoteType
strong
Enumerator
None 
Single 
Double 
Both 

Function Documentation

◆ AppendUnicode()

bool Gorgon::String::AppendUnicode ( std::string &  s,
Char  c 
)

Appends a unicode code point to the string.

If the given char is valid, this function will return true. Otherwise, it will place a three byte long replacement character and returns false.

References UnicodeUTF8Bytes().

◆ CaseInsensitiveCompare()

int Gorgon::String::CaseInsensitiveCompare ( const std::string &  left,
const std::string &  right 
)

Compares two strings case insensitive. Works similar to strcmp.

◆ Concat()

std::string Gorgon::String::Concat ( const P_ &...  rest)

Streams the given parameters into a stringstream and returns the result, effectively concatinating all parameters.

◆ Extract() [1/2]

std::string Gorgon::String::Extract ( std::string &  original,
char  marker,
bool  trim = false 
)

Extracts the part of the string up to the given marker.

Extracted string and the marker is removed from the original string. If the given string does not contain marker, entire string will be extracted. It is possible to tokenize the given string using repeated calls to this function. However, its more convenient to use Tokenizer.

Parameters
originalstring that will be processed. This string will be modified by the program
markercharacter that will be searched.
trimif set, both extracted and the remaining part of the string
Returns
Extracted string. Does not contain the marker.

References Gorgon::swap(), TrimEnd(), and TrimStart().

◆ Extract() [2/2]

std::string Gorgon::String::Extract ( std::string &  original,
const std::string &  marker,
bool  trim = false 
)

Extracts the part of the string up to the given marker.

Extracted string and the marker is removed from the original string. If the given string does not contain marker, entire string will be extracted. It is possible to tokenize the given string using repeated calls to this function. However, its more convenient to use Tokenizer.

Parameters
originalstring that will be processed. This string will be modified by the program
markerstring that will be searched.
trimif set, both extracted and the remaining part of the string
Returns
Extracted string. Does not contain the marker.

References Gorgon::swap(), TrimEnd(), and TrimStart().

◆ Extract_UseQuotes()

std::string Gorgon::String::Extract_UseQuotes ( std::string &  original,
char  marker,
QuoteType  quotetype = QuoteType::Both 
)

Extracts the part of the string up to the given marker.

This function will skipped quoted sections of the string. Both single and double quotes can be considered, however, double quotes should match with double quotes and single quotes should match with single quotes. A different quote type inside quote region is ignored. Extracted string and the marker is removed from the original string. If the given string does not contain marker outside the quotes, entire string will be extracted. It is possible to tokenize the given string using repeated calls to this function. Unbalanced quotes will be treated ending at the end of the string.

Parameters
originalstring that will be processed. This string will be modified by the program
markerstring that will be searched. It is possible to specify quote as a marker.
quotetypecontrols which type of quotes will be considered.
Returns
Extracted string. Does not contain the marker. Quotes will not be removed

References None, Single, and Gorgon::swap().

◆ FixLineEndings()

std::string FixLineEndings ( const std::string &  in,
LineEnding  type = LineEnding::Standard 
)

Fixes/changes line endings.

If none is supplied, all line endings will be removed. If mixed is set, nothing will be done.

◆ From() [1/3]

std::string Gorgon::String::From ( const Resource::GID::Type value)

Creates a string from a GID.

References Type::AsInteger().

◆ From() [2/3]

std::enable_if<decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string>::type Gorgon::String::From ( const T_ &  e)

◆ From() [3/3]

std::string Gorgon::String::From ( const T_ &  item)

Creates a string from the given data.

Similar to to_string but allows conversion of a type if it can be casted or streamed to output. Also uses std::to_string where possible.

◆ InsertUnicode()

bool Gorgon::String::InsertUnicode ( std::string &  s,
std::size_t  pos,
Char  c 
)

Appends a unicode code point to the string.

If the given char is valid, this function will return true. Otherwise, it will place a three byte long replacement character and returns false. pos is the byte offset to insert the character.

References UnicodeUTF8Bytes().

◆ Join()

std::string Gorgon::String::Join ( const T_ &  vec,
const std::string &  glue = ", " 
)

Joins a list of strings to a single string using the given glue text.

◆ PadEnd()

std::string Gorgon::String::PadEnd ( std::string  str,
std::size_t  len,
char  pad = ' ' 
)

Pads the string to the given number of characters from the start.

This function is not utf aware.

◆ PadStart()

std::string Gorgon::String::PadStart ( std::string  str,
std::size_t  len,
char  pad = ' ' 
)

Pads the string to the given number of characters from the start.

This function is not utf aware.

◆ Parse()

std::enable_if<decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, T_>::type Gorgon::String::Parse ( const std::string &  text)

References gorgon__enum_tr_loc().

◆ Replace()

std::string Gorgon::String::Replace ( std::string  str,
const std::string &  find,
const std::string &  replace 
)

String replace that does not use regex.

Works faster than regex variant. This function is not utf aware.

Parameters
stris the string to process
findis the substrings to be replaced
replaceis the string to place instead of find. Can be empty string.

◆ To() [1/2]

std::enable_if<decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, T_>::type Gorgon::String::To ( const std::string &  text)

◆ To() [2/2]

T_ Gorgon::String::To ( const std::string &  value)

Converts a string to another type.

Works for integral types and Gorgon classes including Point, Size, etc... There is no error handling. If conversion does not work, you may end up with uninitialized object. This system will be fixed at a later point.

◆ ToLower()

std::string Gorgon::String::ToLower ( std::string  str)

Converts the given string to lowercase.

This function is not utf aware.

◆ ToUpper()

std::string Gorgon::String::ToUpper ( std::string  str)

Converts the given string to uppercase.

This function is not utf aware.

◆ Trim()

std::string Gorgon::String::Trim ( std::string  str,
const std::string &  chars = " \t\n\r" 
)

Strips whitespace around the given string both from start and end.

This function is not utf aware.

Parameters
stris the string to process
charsis the characters to be considered as whitespace

◆ TrimEnd()

std::string Gorgon::String::TrimEnd ( std::string  str,
const std::string &  chars = " \t\n\r" 
)

Strips the whitespace at the end of a string.

This function is not utf aware.

Parameters
stris the string to process
charsis the characters to be considered as whitespace

◆ TrimStart()

std::string Gorgon::String::TrimStart ( std::string  str,
const std::string &  chars = " \t\n\r" 
)

Strips the whitespace from the start of a string.

This function is not utf aware.

Parameters
stris the string to process
charsis the characters to be considered as whitespace

◆ UnicodeGlyphCount()

int Gorgon::String::UnicodeGlyphCount ( const std::string &  s)

References UTF8Bytes().

◆ UnicodeUTF8Bytes()

int Gorgon::String::UnicodeUTF8Bytes ( Char  c)

◆ UTF8Bytes()

int Gorgon::String::UTF8Bytes ( char  c)

Returns the number of bytes used by the next UTF8 codepoint.