Samchon Framework for CPP  1.0.0
samchon::WeakString Class Reference

A string class only references characeters, reference only. More...

#include <WeakString.hpp>

Public Member Functions

 WeakString ()
 Default Constructor does not reference any character. More...
 
 WeakString (const char *data, size_t size)
 Constructor by characters with specified size. More...
 
 WeakString (const char *begin, const char *end)
 Constructor by characters of begin and end. More...
 
 WeakString (const char *data)
 Constructor by characters. More...
 
 WeakString (const char &ch)
 Constructor by a single character. More...
 
 WeakString (std::initializer_list< char > &il)
 Constructor by a initializer list. More...
 
 WeakString (const std::string &str)
 Constructor by string. More...
 
auto data () const -> const char *
 Get string data; referenced characeters. More...
 
auto size () const -> size_t
 Returns size of the characters which are being referenced. More...
 
auto empty () const -> bool
 Tests wheter string is emtpy. More...
 
auto at (size_t index) const -> const char &
 Get character of string Returns a const reference to the character at the specified position. More...
 
auto operator[] (size_t index) const -> const char &
 Get character of string Returns a const reference to the character at the specified position. More...
 
auto find (const WeakString &delim, size_t startIndex=NULL) const -> size_t
 Finds first occurence in string. More...
 
auto rfind (const WeakString &delim, size_t endIndex=SIZE_MAX) const -> size_t
 Finds last occurence in string. More...
 
auto finds (const std::vector< std::string > &delims, size_t startIndex=0) const -> IndexPair< WeakString >
 Finds first occurence in string. More...
 
auto rfinds (const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) const -> IndexPair< WeakString >
 Finds last occurence in string. More...
 
auto substr (size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
 Generates a substring. More...
 
auto substring (size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
 Generates a substring. More...
 
auto between (const WeakString &start={}, const WeakString &end={}) const -> WeakString
 Generates a substring. More...
 
auto split (const WeakString &delim) const -> std::vector< WeakString >
 Generates substrings. More...
 
auto betweens (const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
 Generates substrings. More...
 
auto trim (const std::vector< std::string > &delims) const -> WeakString
 Removes all designated characters from the beginning and end of the specified string. More...
 
auto ltrim (const std::vector< std::string > &delims) const -> WeakString
 Removes all designated characters from the beginning of the specified string. More...
 
auto rtrim (const std::vector< std::string > &delims) const -> WeakString
 Removes all designated characters from the end of the specified string. More...
 
auto replace (const WeakString &before, const WeakString &after) const -> std::string
 Replace portion of string once. More...
 
auto replaceAll (const WeakString &before, const WeakString &after) const -> std::string
 Returns a string specified word is replaced. More...
 
auto replaceAll (const std::vector< std::pair< std::string, std::string >> &pairs) const -> std::string
 Returns a string specified words are replaced. More...
 
auto toLowerCase () const -> std::string
 Convert uppercase letters to lowercase. More...
 
auto toUpperCase () const -> std::string
 Convert uppercase letters to lowercase. More...
 
auto str () const -> std::string
 Get the string content. More...
 

Static Public Attributes

static const size_t npos = -1
 Maximum value for size_t. More...
 

Private Attributes

const char * data_
 Referenced characters's pointer of begining position. More...
 
size_t size_
 (Specified) size of referenced characters More...
 

Static Private Attributes

static const std::vector< std::string > SPACE_ARRAY = { " ", "\t", "\r", "\n" }
 An array containing whitespaces. More...
 

Detailed Description

A string class only references characeters, reference only.

WeakSring does not consider any construction, modification and destruction of characters.

Thus, you can have greater advantages than std::string on the side of performance and memory, but of course, you can't modify the characeters at all.

library_string.png
Warning
  • WeakString not copy(strcpy) characeters but only references characeters. Be careful about destruction of the characters being referenced by the WeakString
  • WeakString will be used for basic data type in most case. Avoid to use WeakString by pre-definition in header (*.hpp)
See also
samchon::library
Author
Jeongho Nam http://samchon.org

Definition at line 32 of file WeakString.hpp.

Constructor & Destructor Documentation

WeakString::WeakString ( )

Default Constructor does not reference any character.

Constructs an empty string, with zero size

Definition at line 18 of file WeakString.cpp.

Referenced by rfinds(), substr(), and substring().

Here is the caller graph for this function:

WeakString::WeakString ( const char *  data,
size_t  size 
)

Constructor by characters with specified size.

Constructs by characters to be referenced with limited size.

Although the original size of data is over the specified size, you can limit referencing size of the characters

  • Referencing a part of characters
Warning
WeakString only references. Be careful about destruction of the characters (data)
Parameters
dataTarget characters to be referenced by string
size

Specified limit-size of characters to be referenced.

If the specified size is greater than original size, it will be ignored

Definition at line 23 of file WeakString.cpp.

WeakString::WeakString ( const char *  begin,
const char *  end 
)

Constructor by characters of begin and end.

Constructs by characters to be referenced with its end position.

Although the original end point of data is over the specified end, you can limit end point of the characters.

  • Referencing a part of characters
Warning
WeakString only references. Be careful about destruction of the characters (data).
Parameters
beginTarget characters to be referenced by string
end

Specified end point of characters to be referenced.

If the specified end point is greater than original end point, it will be ignored.

Definition at line 29 of file WeakString.cpp.

WeakString::WeakString ( const char *  data)

Constructor by characters.

References the null-terminated character sequence pointed by ptr

Warning
WeakString only references. Be careful about destruction of the characeters (data)
Parameters
dataTarget characters to be referenced by string

Definition at line 33 of file WeakString.cpp.

References data(), data_, and size_.

Here is the call graph for this function:

WeakString::WeakString ( const char &  ch)

Constructor by a single character.

References a single character

Warning
WeakString only references. Be careful about destruction of the characeter (data)
Parameters
chTarget character to be referenced by string

Definition at line 42 of file WeakString.cpp.

References data_, and size_.

WeakString::WeakString ( std::initializer_list< char > &  il)

Constructor by a initializer list.

References initializer list of character

Warning
WeakString only references. Be careful about destruction of the characeters (data)
Parameters
ilTarget initializer list of characters to be referenced by string

Definition at line 47 of file WeakString.cpp.

References data_, and size_.

WeakString::WeakString ( const std::string &  str)

Constructor by string.

References whole chracters of the string

Warning
WeakString only references. Be careful about destruction of the string
Parameters
strTarget string to be referenced by string

Definition at line 56 of file WeakString.cpp.

References data_, and size_.

Member Function Documentation

auto WeakString::data ( ) const -> const char*

Get string data; referenced characeters.

Returns a pointer to an array that contains a null-terminated sequence of characters representing the current value of the string object

Warning
Returned pointer's size can be longer than string's specified size if the string references only a part of the characters
Returns
A pointer of characters being referenced by the string

Definition at line 65 of file WeakString.cpp.

References data_.

Referenced by WeakString().

Here is the caller graph for this function:

auto WeakString::size ( ) const -> size_t

Returns size of the characters which are being referenced.

Returns the length of the string, in terms of number of referenced characters

Returns
size of characters being referenced by string

Definition at line 69 of file WeakString.cpp.

References size_.

Referenced by betweens(), replace(), replaceAll(), split(), substr(), toUpperCase(), and samchon::library::XML::XML().

Here is the caller graph for this function:

auto WeakString::empty ( ) const -> bool

Tests wheter string is emtpy.

Returns wheter characters' size is zero or not.

Of course, string references nothing, then returns false, too.

Returns
Wheter size is zero or not

Definition at line 74 of file WeakString.cpp.

References data_, and size_.

Referenced by betweens(), samchon::library::XML::push_back(), and rfind().

Here is the caller graph for this function:

auto WeakString::at ( size_t  index) const -> const char&

Get character of string Returns a const reference to the character at the specified position.

Returns
const reference of character at the specified index

Definition at line 79 of file WeakString.cpp.

References data_.

Referenced by toUpperCase().

Here is the caller graph for this function:

auto WeakString::operator[] ( size_t  index) const -> const char&

Get character of string Returns a const reference to the character at the specified position.

Returns
const reference of character at the specified index

Definition at line 83 of file WeakString.cpp.

References data_.

auto WeakString::find ( const WeakString delim,
size_t  startIndex = NULL 
) const -> size_t

Finds first occurence in string.

Finds the string after startIndex and returns the position of first occurence of delim.

If delim is not found, returns -1 (npos)

Parameters
delimThe substring of the string which to find
startIndexSpecified starting index of find. Default is 0
Returns
Index of first occurence of the specified substring or -1

Definition at line 91 of file WeakString.cpp.

References data_, npos, and size_.

Referenced by between(), betweens(), finds(), samchon::library::HTTPLoader::load(), replace(), replaceAll(), samchon::library::Datetime::set(), samchon::library::Date::set(), split(), samchon::library::URLVariables::URLVariables(), and samchon::library::XML::XML().

Here is the caller graph for this function:

auto WeakString::rfind ( const WeakString delim,
size_t  endIndex = SIZE_MAX 
) const -> size_t

Finds last occurence in string.

Finds the string before endIndex and returns the position of last occurence of delim. If delim is not found, returns -1 (npos)

Parameters
delimThe substring of the string which to find
endIndexSpecified last index of find. Default is size() - 1
Returns
Index of first occurence of the specified substring or -1

Definition at line 103 of file WeakString.cpp.

References data_, empty(), npos, and size_.

Referenced by betweens(), rfinds(), and samchon::library::XML::XML().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::finds ( const std::vector< std::string > &  delims,
size_t  startIndex = 0 
) const -> IndexPair<WeakString>

Finds first occurence in string.

Finds first occurence position of each delim in the string after startIndex and returns the minimum position of them.

Note
  • If startIndex is not specified, then starts from 0.
  • If failed to find any substring, returns -1 (npos)
Parameters
delimsThe substrings of target(str) which to find
startIndexSpecified starting index of find. Default is 0
Returns
pair<size_t := position, string := matched substring>

Definition at line 121 of file WeakString.cpp.

References find(), samchon::IndexPair< T >::getIndex(), and samchon::IndexPair< T >::getValue().

Referenced by samchon::library::StringUtil::finds().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::rfinds ( const std::vector< std::string > &  delims,
size_t  endIndex = SIZE_MAX 
) const -> IndexPair<WeakString>

Finds last occurence in string.

Finds last occurence position of each delim in the string before endIndex and returns the maximum position of them.

Note
  • If index is not specified, then starts str.size() - 1
  • If failed to find any substring, returns -1 (npos)
Parameters
delimsThe substrings of target(str) which to find
endIndexSpecified starting index of find. Default is size() - 1
Returns
pair<size_t := position, string := matched substring>

Definition at line 141 of file WeakString.cpp.

References samchon::IndexPair< T >::getIndex(), samchon::IndexPair< T >::getValue(), rfind(), and WeakString().

Referenced by samchon::library::StringUtil::rfinds().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::substr ( size_t  startIndex,
size_t  endIndex = SIZE_MAX 
) const -> WeakString

Generates a substring.

Extracts a substring consisting of the characters starts from startIndex and with a size specified size.

Parameters
startIndex

Index of the first character.

If startIndex is greater than endIndex, those will be swapped.

Parameters
endIndex

Number of characters to include in substring.

If the specified size is greater than last index of characeters, it will be shrinked.

Returns
Sub string by specified index and size

Definition at line 174 of file WeakString.cpp.

References data_, size(), size_, and WeakString().

Referenced by between(), samchon::library::HTTPLoader::load(), replace(), replaceAll(), samchon::library::URLVariables::URLVariables(), and samchon::library::XML::XML().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::substring ( size_t  startIndex,
size_t  size = SIZE_MAX 
) const -> WeakString

Generates a substring.

Extracts a substring consisting of the character specified by startIndex and all characters up to endIndex - 1.

  • If endIndex is not specified, string::size() will be used instead.
  • If endIndex is greater than startIndex, then those will be swapped
Parameters
startIndex

Index of the first character.

If startIndex is greater than endIndex, those will be swapped.

Parameters
size

Index of the last character - 1.

If not specified, then string::size() will be used instead.

Returns
Sub string by specified index(es)

Definition at line 181 of file WeakString.cpp.

References data_, size_, and WeakString().

Referenced by between(), betweens(), replaceAll(), split(), samchon::library::StringUtil::substring(), and samchon::library::XML::XML().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::between ( const WeakString start = {},
const WeakString end = {} 
) const -> WeakString

Generates a substring.

Extracts a substring consisting of the characters from specified start to end.

It's same with substring( ? = (str.find(start) + start.size()), find(end, ?) )

  • between("ABCD[EFGH]IJK", "[", "]") => "EFGH"
Note
  • If start is not specified, extracts from begin of the string to end
  • If end is not specified, extracts from start to end of the string
  • If start and end are all omitted, returns str, itself.
Parameters
startA string for separating substring at the front
endA string for separating substring at the end
Returns
substring by specified terms

Definition at line 194 of file WeakString.cpp.

References find(), substr(), and substring().

Referenced by samchon::library::StringUtil::between(), samchon::library::HTTPLoader::load(), samchon::protocol::IWebServer::open(), samchon::library::Datetime::set(), samchon::library::Date::set(), and samchon::library::XML::XML().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::split ( const WeakString delim) const -> std::vector<WeakString>

Generates substrings.

Splits a string in to an array of substrings dividing by the specified delimiter

Parameters
delimThe pattern which specifies where to split the string
Returns
An array of substrings

Definition at line 212 of file WeakString.cpp.

References find(), npos, size(), and substring().

Referenced by betweens(), samchon::library::HTTPLoader::load(), samchon::library::Date::set(), samchon::library::StringUtil::split(), and samchon::library::URLVariables::URLVariables().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::betweens ( const WeakString start = {},
const WeakString end = {} 
) const -> std::vector<WeakString>

Generates substrings.

Splits a string into an array of substrings dividing by delimeters of start and end.

It's the array of substrings adjusted the between.

Note
  • If start is omitted, it's same with the split by endStr not having last item
  • If end is omitted, it's same with the split by startStr not having first item
  • If start and end are all omitted, returns string, itself
Parameters
start

A string for separating substring at the front.

If omitted, it's same with split(end) not having last item.

Parameters
end

A string for separating substring at the end.

If omitted, it's same with split(start) not having first item.

Returns
An array of substrings

Definition at line 237 of file WeakString.cpp.

References data_, empty(), find(), samchon::IndexPair< T >::getIndex(), samchon::IndexPair< T >::getValue(), ltrim(), npos, rfind(), rtrim(), size(), size_, SPACE_ARRAY, split(), str(), substring(), and trim().

Referenced by samchon::library::StringUtil::betweens(), and samchon::protocol::IClient::listen().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::trim ( const std::vector< std::string > &  delims) const -> WeakString

Removes all designated characters from the beginning and end of the specified string.

Parameters
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the beginning and end

Definition at line 374 of file WeakString.cpp.

References trim().

Referenced by betweens(), samchon::library::StringUtil::colorPercentFormat(), samchon::library::XML::hasProperty(), samchon::library::Date::set(), trim(), and samchon::library::XML::XML().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::ltrim ( const std::vector< std::string > &  delims) const -> WeakString

Removes all designated characters from the beginning of the specified string.

Parameters
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the beginning

Definition at line 382 of file WeakString.cpp.

References ltrim().

Referenced by betweens(), samchon::library::StringUtil::colorPercentFormat(), and ltrim().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::rtrim ( const std::vector< std::string > &  delims) const -> WeakString

Removes all designated characters from the end of the specified string.

Parameters
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the end

Definition at line 390 of file WeakString.cpp.

References rtrim().

Referenced by betweens(), samchon::library::StringUtil::colorPercentFormat(), and rtrim().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::replace ( const WeakString before,
const WeakString after 
) const -> std::string

Replace portion of string once.

Parameters
beforeA specific word you want to be replaced
afterA specific word you want to replace
Returns
A string specific word is replaced once

Definition at line 402 of file WeakString.cpp.

References find(), npos, size(), str(), and substr().

Here is the call graph for this function:

auto WeakString::replaceAll ( const WeakString before,
const WeakString after 
) const -> std::string

Returns a string specified word is replaced.

Parameters
beforeA specific word you want to be replaced
afterA specific word you want to replace
Returns
A string specified word is replaced

Definition at line 418 of file WeakString.cpp.

Referenced by samchon::library::XML::hasProperty(), replaceAll(), and samchon::library::StringUtil::toUpperCase().

Here is the caller graph for this function:

auto WeakString::replaceAll ( const std::vector< std::pair< std::string, std::string >> &  pairs) const -> std::string

Returns a string specified words are replaced.

Parameters
strTarget string to replace
pairsA specific word's pairs you want to replace and to be replaced
Returns
A string specified words are replaced

Definition at line 423 of file WeakString.cpp.

References find(), npos, replaceAll(), size(), str(), substr(), and substring().

Here is the call graph for this function:

auto WeakString::toLowerCase ( ) const -> std::string

Convert uppercase letters to lowercase.

Returns a string that all uppercase characters are converted to lowercase.

Parameters
wstrTarget string to convert uppercase to lowercase
Returns
A string converted to lowercase

Definition at line 491 of file WeakString.cpp.

References str().

Referenced by samchon::library::StringUtil::toLowerCase().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::toUpperCase ( ) const -> std::string

Convert uppercase letters to lowercase.

Returns a string all lowercase characters are converted to uppercase.

Parameters
strTarget string to convert lowercase to uppercase
Returns
A string converted to uppercase

Definition at line 500 of file WeakString.cpp.

References at(), size(), and str().

Referenced by samchon::library::StringUtil::toUpperCase().

Here is the call graph for this function:

Here is the caller graph for this function:

auto WeakString::str ( ) const -> std::string

Get the string content.

Returns a string object with a copy of the current contents in the WeakString.

Returns
A new string copied from the WeakString

Definition at line 546 of file WeakString.cpp.

References data_, and size_.

Referenced by betweens(), samchon::protocol::IHTMLEntity::IHTMLEntity(), samchon::protocol::InvokeParameter::InvokeParameter(), samchon::library::HTTPLoader::load(), replace(), replaceAll(), samchon::library::XML::setProperty(), samchon::library::XML::setValue(), toLowerCase(), and toUpperCase().

Here is the caller graph for this function:

Member Data Documentation

const vector< std::string > WeakString::SPACE_ARRAY = { " ", "\t", "\r", "\n" }
staticprivate

An array containing whitespaces.

Definition at line 38 of file WeakString.hpp.

Referenced by betweens().

const size_t samchon::WeakString::npos = -1
static

Maximum value for size_t.

npos is a static member constant value with the greatest possible value for an element of type size_t.

This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string". As a return value, it is usually used to indicate no matches.

This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

Definition at line 53 of file WeakString.hpp.

Referenced by betweens(), find(), replace(), replaceAll(), rfind(), and split().

const char* samchon::WeakString::data_
private

Referenced characters's pointer of begining position.

Definition at line 59 of file WeakString.hpp.

Referenced by at(), betweens(), data(), empty(), find(), operator[](), rfind(), str(), substr(), substring(), and WeakString().

size_t samchon::WeakString::size_
private

(Specified) size of referenced characters

Definition at line 64 of file WeakString.hpp.

Referenced by betweens(), empty(), find(), rfind(), size(), str(), substr(), substring(), and WeakString().


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