nlib
nn::nlib::StringView Class Reference

The class for using the member functions of std::string without constructing std::string. More...

#include "nn/nlib/StringView.h"

Public Types

typedef char charT
 Currently a char-type specific class.
 
typedef charT value_type
 The type for a character.
 
typedef const charTpointer
 The type for a character pointer. The same as const_pointer.
 
typedef const charTconst_pointer
 The type for a character pointer.
 
typedef const charTreference
 A reference to a character. The same as const_reference.
 
typedef const charTconst_reference
 A reference to a character.
 
typedef const_pointer const_iterator
 A character string iterator.
 
typedef const_iterator iterator
 A string iterator. The same as const_iterator.
 
typedef std::reverse_iterator< const_iteratorconst_reverse_iterator
 A string reverse iterator.
 
typedef const_reverse_iterator reverse_iterator
 A string reverse iterator. The same as const_reverse_iterator.
 
typedef size_t size_type
 A non-negative integer type, currently defined in size_t using typedef.
 
typedef ptrdiff_t difference_type
 The type returned when you take the difference between iterators.
 

Public Member Functions

constexpr StringView () noexcept
 Instantiates the object with default parameters (default constructor). Initialized as an empty string.
 
 StringView (const charT *str) noexcept
 Initialized to reference str. The string length is calculated internally.
 
 StringView (const charT *str, size_type len) noexcept
 Initializes using the specified calculated string length.
 
const_iterator begin () const noexcept
 Returns the iterator pointing to the first character. More...
 
const_iterator end () const noexcept
 Returns the iterator pointing to the character following the last character. More...
 
const_iterator cbegin () const noexcept
 Returns the iterator pointing to the first character. More...
 
const_iterator cend () const noexcept
 Returns the iterator pointing to the character following the last character. More...
 
const_reverse_iterator rbegin () const noexcept
 Returns the reverse iterator pointing to the last character. More...
 
const_reverse_iterator rend () const noexcept
 Returns the reverse iterator pointing to the character before the first character. More...
 
const_reverse_iterator crbegin () const noexcept
 Returns the reverse iterator pointing to the last character. More...
 
const_reverse_iterator crend () const noexcept
 Returns the reverse iterator pointing to the character before the first character. More...
 
size_type size () const noexcept
 Returns the length of the string. More...
 
size_type length () const noexcept
 Returns the length of the string. More...
 
size_type max_size () const noexcept
 Returns the maximum value for the string length. More...
 
bool empty () const noexcept
 Checks whether it is an empty string. More...
 
const charToperator[] (size_type pos) const
 Gets the nth character, where n is specified by pos. More...
 
const charTat (size_type pos) const
 Gets the nth character, where n is specified by pos. More...
 
const charTfront () const
 Gets the first character. More...
 
const charTback () const
 Gets the last character. More...
 
const charTdata () const noexcept
 Returns the pointer to the first character. More...
 
void clear () noexcept
 Sets an empty string.
 
void remove_prefix (size_type n) noexcept
 Removes the first n characters. More...
 
void remove_suffix (size_type n) noexcept
 Removes the last n characters. More...
 
StringView substr (size_type pos, size_type n=npos) const noexcept
 Returns a substring [pos, pos + n). More...
 
int compare (const StringView &s) const noexcept
 Compares strings. More...
 
int compare (const charT *s) const noexcept
 Compares strings. More...
 
bool starts_with (const StringView &s) const noexcept
 Checks whether the string has s as its prefix. More...
 
bool starts_with (charT c) const noexcept
 Checks whether the string begins with the character specified for c. More...
 
bool starts_with (const charT *s) const noexcept
 Checks whether the string has s as its prefix. More...
 
bool ends_with (const StringView &s) const noexcept
 Checks whether the string has s as its suffix. More...
 
bool ends_with (charT c) const noexcept
 Checks whether the string ends with the character specified for c. More...
 
bool ends_with (const charT *s) const noexcept
 Checks whether the string has s as its suffix. More...
 
bool ToCstring (charT *str, size_type buf_size) const noexcept
 Converts to a C string. More...
 
template<size_type N>
bool ToCstring (charT(&str)[N]) const noexcept
 
bool ToCstring (UniquePtr< charT[]> &str) const noexcept
 
errno_t ToInteger (int32_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (int64_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (uint32_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (uint64_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (int8_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (int16_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (uint8_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToInteger (uint16_t *v, size_type *idx=NULL, int base=10) const noexcept
 Converts a string to an integer. More...
 
errno_t ToFloat (float *v, size_type *idx=NULL) const noexcept
 Converts a string to a double-precision floating-point number. More...
 
errno_t ToDouble (double *v, size_type *idx=NULL) const noexcept
 Converts a string to a double-precision floating-point number. More...
 
bool TrimLeft () noexcept
 Trims the white space from the start of the string. More...
 
bool TrimRight () noexcept
 Trims the white space from the end of the string. More...
 
void Trim () noexcept
 Trims the white space from the start and the end of the string.
 
StringView GetLine () noexcept
 Gets the string from the start to the end of the line. More...
 
bool Proceed (const StringView &kwd) noexcept
 Advances by the amount of the string length of kwd, providing that the prefix matches kwd. More...
 
bool Proceed (const charT *kwd) noexcept
 Advances by the amount of the string length of kwd, providing that the prefix matches kwd. More...
 
bool Proceed (charT ch) noexcept
 Checks the lead character and advances by one character if it matches. More...
 
StringView GetName () noexcept
 Gets a string that is generally a variable name. More...
 

Static Public Attributes

static const size_type npos = size_type(-1)
 Used the same way as npos of std::string.
 

Detailed Description

The class for using the member functions of std::string without constructing std::string.

Description
Implemented with reference to the string_view class proposed for C++1y.
You can use these functions by using code like the following.
// Initializes from a C string.
const char str[] = .......;
StringView view(str);
// Initializes from a C++ string.
string str = ....;
StringView view(&str[0], str.len());
// Gets a substring.
StringView substr = view.substr(1, 10);
// Deletes leading and trailing white spaces.
view.TrimLeft();
view.TrimRight();
view.Trim();
// Checks the prefix and suffix.
view.starts_with(char);
view.starts_with(C string);
view.starts_with(StringView);
view.ends_with(char);
view.ends_with(C string);
view.ends_with(StringView);
// Copies to a C string.
view.ToCstring(buf, bufsize);
// Parses numeric strings.
view.ToInteger(&v);
view.ToFloat(&f);
view.ToDouble(&d);
....
See also
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html
Examples:
misc/stringutils/stringutils.cpp.

Definition at line 28 of file StringView.h.

Member Function Documentation

◆ at()

nn::nlib::StringView::at ( size_type  pos) const
inline

Gets the nth character, where n is specified by pos.

Parameters
[in]posAn index of a character.
Returns
The nth character, (where n is set in pos).

Definition at line 86 of file StringView.h.

◆ back()

nn::nlib::StringView::back ( ) const
inline

Gets the last character.

Returns
The last character.

Definition at line 91 of file StringView.h.

◆ begin()

nn::nlib::StringView::begin ( ) const
inlinenoexcept

Returns the iterator pointing to the first character.

Returns
The iterator pointing to the first character.

Definition at line 56 of file StringView.h.

◆ cbegin()

nn::nlib::StringView::cbegin ( ) const
inlinenoexcept

Returns the iterator pointing to the first character.

Returns
The iterator pointing to the first character.

Definition at line 61 of file StringView.h.

◆ cend()

nn::nlib::StringView::cend ( ) const
inlinenoexcept

Returns the iterator pointing to the character following the last character.

Returns
The iterator pointing to the character following the last character.

Definition at line 62 of file StringView.h.

◆ compare() [1/2]

nn::nlib::StringView::compare ( const StringView s) const
noexcept

Compares strings.

Parameters
[in]sThe string to compare with.
Returns
Returns the same values as std::strcmp function.

◆ compare() [2/2]

nn::nlib::StringView::compare ( const charT s) const
inlinenoexcept

Compares strings.

Parameters
[in]sThe string to compare with.
Returns
Returns the same values as std::strcmp function.

Definition at line 126 of file StringView.h.

◆ crbegin()

nn::nlib::StringView::crbegin ( ) const
inlinenoexcept

Returns the reverse iterator pointing to the last character.

Returns
The reverse iterator pointing to the last character.

Definition at line 66 of file StringView.h.

◆ crend()

nn::nlib::StringView::crend ( ) const
inlinenoexcept

Returns the reverse iterator pointing to the character before the first character.

Returns
The reverse iterator pointing to the character before the first character.

Definition at line 67 of file StringView.h.

◆ data()

nn::nlib::StringView::data ( ) const
inlinenoexcept

Returns the pointer to the first character.

Returns
The pointer to the first character.

Definition at line 92 of file StringView.h.

◆ empty()

nn::nlib::StringView::empty ( ) const
inlinenoexcept

Checks whether it is an empty string.

Returns
Returns true if it is an empty string.

Definition at line 79 of file StringView.h.

◆ end()

nn::nlib::StringView::end ( ) const
inlinenoexcept

Returns the iterator pointing to the character following the last character.

Returns
The iterator pointing to the character following the last character.

Definition at line 60 of file StringView.h.

◆ ends_with() [1/3]

nn::nlib::StringView::ends_with ( const StringView s) const
inlinenoexcept

Checks whether the string has s as its suffix.

Parameters
[in]sThe suffix string.
Returns
Returns true if it has s as its suffix.

Definition at line 136 of file StringView.h.

◆ ends_with() [2/3]

nn::nlib::StringView::ends_with ( charT  c) const
inlinenoexcept

Checks whether the string ends with the character specified for c.

Parameters
[in]cA character.
Returns
Returns true if it ends with c.

Definition at line 142 of file StringView.h.

◆ ends_with() [3/3]

nn::nlib::StringView::ends_with ( const charT s) const
inlinenoexcept

Checks whether the string has s as its suffix.

Parameters
[in]sThe suffix string.
Returns
Returns true if it has s as its suffix.

Definition at line 143 of file StringView.h.

◆ front()

nn::nlib::StringView::front ( ) const
inline

Gets the first character.

Returns
The first character.

Definition at line 90 of file StringView.h.

◆ GetLine()

nn::nlib::StringView::GetLine ( )
noexcept

Gets the string from the start to the end of the line.

Returns
One line's worth of substring (not including the newline character).
Description
The owned object moves to the beginning of the next line.

◆ GetName()

StringView nn::nlib::StringView::GetName ( )
inlinenoexcept

Gets a string that is generally a variable name.

Returns
The matching string.
Description
Returns the leading substring that matches the regular expression [a-zA-Z_][a-zA-Z0-9_]*. The owned object moves beyond the obtained string.

Definition at line 283 of file StringView.h.

◆ length()

nn::nlib::StringView::length ( ) const
inlinenoexcept

Returns the length of the string.

Returns
The string length.

Definition at line 74 of file StringView.h.

◆ max_size()

nn::nlib::StringView::max_size ( ) const
inlinenoexcept

Returns the maximum value for the string length.

Returns
The maximum string length.

Definition at line 78 of file StringView.h.

◆ operator[]()

nn::nlib::StringView::operator[] ( size_type  pos) const
inline

Gets the nth character, where n is specified by pos.

Parameters
[in]posAn index of a character.
Returns
The nth character, (where n is set in pos).

Definition at line 82 of file StringView.h.

◆ Proceed() [1/3]

nn::nlib::StringView::Proceed ( const StringView kwd)
inlinenoexcept

Advances by the amount of the string length of kwd, providing that the prefix matches kwd.

Parameters
[in]kwdThe string to check.
Returns
Returns true if the prefix matches kwd.

Definition at line 178 of file StringView.h.

◆ Proceed() [2/3]

nn::nlib::StringView::Proceed ( const charT kwd)
inlinenoexcept

Advances by the amount of the string length of kwd, providing that the prefix matches kwd.

Parameters
[in]kwdThe string to check.
Returns
Returns true if the prefix matches kwd.

Definition at line 184 of file StringView.h.

◆ Proceed() [3/3]

bool nn::nlib::StringView::Proceed ( charT  ch)
inlinenoexcept

Checks the lead character and advances by one character if it matches.

Parameters
[in]chThe character to check.
Returns
Returns true if the lead character is ch.

Definition at line 273 of file StringView.h.

◆ rbegin()

nn::nlib::StringView::rbegin ( ) const
inlinenoexcept

Returns the reverse iterator pointing to the last character.

Returns
The reverse iterator pointing to the last character.

Definition at line 64 of file StringView.h.

◆ remove_prefix()

nn::nlib::StringView::remove_prefix ( size_type  n)
inlinenoexcept

Removes the first n characters.

Parameters
[in]nThe number of characters to remove.
Description
The result is the same as the following code.
*this = this->substr(n, npos);

Definition at line 99 of file StringView.h.

◆ remove_suffix()

nn::nlib::StringView::remove_suffix ( size_type  n)
inlinenoexcept

Removes the last n characters.

Parameters
[in]nThe number of characters to remove.
Description
The result is the same as the following code.
*this = this->substr(0, this->size() - n);

Definition at line 108 of file StringView.h.

◆ rend()

nn::nlib::StringView::rend ( ) const
inlinenoexcept

Returns the reverse iterator pointing to the character before the first character.

Returns
The reverse iterator pointing to the character before the first character.

Definition at line 65 of file StringView.h.

◆ size()

nn::nlib::StringView::size ( ) const
inlinenoexcept

Returns the length of the string.

Returns
The string length.

Definition at line 70 of file StringView.h.

◆ starts_with() [1/3]

nn::nlib::StringView::starts_with ( const StringView s) const
inlinenoexcept

Checks whether the string has s as its prefix.

Parameters
[in]sThe prefix string.
Returns
Returns true if it has s as its prefix.

Definition at line 127 of file StringView.h.

◆ starts_with() [2/3]

nn::nlib::StringView::starts_with ( charT  c) const
inlinenoexcept

Checks whether the string begins with the character specified for c.

Parameters
[in]cA character.
Returns
Returns true if it begins with c.

Definition at line 132 of file StringView.h.

◆ starts_with() [3/3]

nn::nlib::StringView::starts_with ( const charT s) const
inlinenoexcept

Checks whether the string has s as its prefix.

Parameters
[in]sThe prefix string.
Returns
Returns true if it has s as its prefix.

Definition at line 133 of file StringView.h.

◆ substr()

nn::nlib::StringView::substr ( size_type  pos,
size_type  n = npos 
) const
inlinenoexcept

Returns a substring [pos, pos + n).

Parameters
[in]posThe first position of the substring.
[in]nLength of the substring.
Returns
StringView object indicating the substring [pos, pos + n)

Definition at line 117 of file StringView.h.

◆ ToCstring() [1/3]

nn::nlib::StringView::ToCstring ( charT str,
size_type  buf_size 
) const
noexcept

Converts to a C string.

Parameters
[out]strThe buffer where the C string is stored.
[in]buf_sizeThe size of the buffer (including the null character).
Returns
Returns true if successful, and false if the buffer is not large enough.

◆ ToCstring() [2/3]

template<size_type N>
nn::nlib::StringView::ToCstring ( charT(&)  str[N]) const
inlinenoexcept
Template Parameters
NThe size of the buffer (including the null character).
Parameters
[in]strThe buffer where the C string is stored.
Returns
Returns true if successful, and false if the buffer is not large enough.

Definition at line 146 of file StringView.h.

◆ ToCstring() [3/3]

nn::nlib::StringView::ToCstring ( UniquePtr< charT[]> &  str) const
inlinenoexcept
Parameters
[out]strThe reference to the UniquePtr where the C string is stored.
Returns
Returns true if successful, and false if the dynamic allocation of memory failed.

Definition at line 152 of file StringView.h.

◆ ToDouble()

nn::nlib::StringView::ToDouble ( double *  v,
size_type idx = NULL 
) const
noexcept

Converts a string to a double-precision floating-point number.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
Description
If ERANGE, the number of read characters is stored in idx. For other errors, the value is not stored there.

◆ ToFloat()

nn::nlib::StringView::ToFloat ( float *  v,
size_type idx = NULL 
) const
noexcept

Converts a string to a double-precision floating-point number.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
Description
If ERANGE, the number of read characters is stored in idx. For other errors, the value is not stored there.

◆ ToInteger() [1/8]

nn::nlib::StringView::ToInteger ( int32_t *  v,
size_type idx = NULL,
int  base = 10 
) const
noexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

◆ ToInteger() [2/8]

nn::nlib::StringView::ToInteger ( int64_t *  v,
size_type idx = NULL,
int  base = 10 
) const
noexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

◆ ToInteger() [3/8]

nn::nlib::StringView::ToInteger ( uint32_t *  v,
size_type idx = NULL,
int  base = 10 
) const
noexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

◆ ToInteger() [4/8]

nn::nlib::StringView::ToInteger ( uint64_t *  v,
size_type idx = NULL,
int  base = 10 
) const
noexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

◆ ToInteger() [5/8]

errno_t nn::nlib::StringView::ToInteger ( int8_t *  v,
size_type idx = NULL,
int  base = 10 
) const
inlinenoexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

Definition at line 234 of file StringView.h.

◆ ToInteger() [6/8]

errno_t nn::nlib::StringView::ToInteger ( int16_t *  v,
size_type idx = NULL,
int  base = 10 
) const
inlinenoexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

Definition at line 244 of file StringView.h.

◆ ToInteger() [7/8]

errno_t nn::nlib::StringView::ToInteger ( uint8_t *  v,
size_type idx = NULL,
int  base = 10 
) const
inlinenoexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

Definition at line 254 of file StringView.h.

◆ ToInteger() [8/8]

errno_t nn::nlib::StringView::ToInteger ( uint16_t *  v,
size_type idx = NULL,
int  base = 10 
) const
inlinenoexcept

Converts a string to an integer.

Parameters
[out]vPointer to where the numerical value is stored.
[out]idxPointer to where the number of read characters is stored.
[in]baseThe number base to use. Can be 0, 8, 10, or 16. If 0, works the same was as the std::strtol function.
Return values
0Success.
ELISEQIndicates that an invalid character was detected in the numerical value.
EINVALv is set to NULL.
ERANGEIndicates an overflow or an underflow.
ENOTSUPIndicates that base was a value other than 0, 8, 10, or 16.

Definition at line 264 of file StringView.h.

◆ TrimLeft()

nn::nlib::StringView::TrimLeft ( )
noexcept

Trims the white space from the start of the string.

Returns
Returns true if white space was trimmed.

◆ TrimRight()

nn::nlib::StringView::TrimRight ( )
noexcept

Trims the white space from the end of the string.

Returns
Returns true if white space was trimmed.

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