nlib
nn::nlib::StringView Class Reference

The class for using the member functions of std::string without constructing std::string. This class will be defined as the string_view of C++17 using typedef. For more information about free functions taking StringView as the argument, seehere.. 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 same as const_pointer.
 
typedef const charTconst_pointer
 Read-only pointer to an element.
 
typedef const charTreference
 The same as const_reference.
 
typedef const charTconst_reference
 Read-only reference to an element.
 
typedef const_pointer const_iterator
 Read-only random-access iterator.
 
typedef const_iterator iterator
 The same as const_iterator.
 
typedef std::reverse_iterator< const_iteratorconst_reverse_iterator
 Read-only reverse iterator.
 
typedef const_reverse_iterator 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
 Gets the read-only iterator pointing to the first element.
 
const_iterator end () const noexcept
 Gets the read-only iterator pointing beyond the last element.
 
const_iterator cbegin () const noexcept
 Gets the read-only iterator pointing to the first element.
 
const_iterator cend () const noexcept
 Gets the read-only iterator pointing beyond the last element.
 
const_reverse_iterator rbegin () const noexcept
 Gets the read-only reverse iterator pointing to the last element.
 
const_reverse_iterator rend () const noexcept
 Gets the read-only reverse iterator pointing ahead of the first element.
 
const_reverse_iterator crbegin () const noexcept
 Gets the read-only reverse iterator pointing to the last element.
 
const_reverse_iterator crend () const noexcept
 Gets the read-only reverse iterator pointing ahead of the first element.
 
size_type size () const noexcept
 Returns the length of the string.
 
size_type length () const noexcept
 Returns the length of the string.
 
size_type max_size () const noexcept
 Returns the maximum value for the string length.
 
bool empty () const noexcept
 Returns true if it is an empty string, otherwise returns false.
 
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 a reference to the first element.
 
const charTback () const
 Gets a reference to the last element.
 
const charTdata () const noexcept
 Returns the pointer to the first character.
 
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...
 

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. This class will be defined as the string_view of C++17 using typedef. For more information about free functions taking StringView as the argument, seehere..

Description
Sample code is provided below.
nlib_ns::StringView view("this is my input string");
auto substr = view.substr(5, 2);
char buf[16];
nlib_printf("'this is my input string'.substr(5, 2): '%s'\n", buf);
nlib_printf("StartsWith(view, \"this\"): %d\n", nlib_ns::StartsWith(view, "this"));
nlib_printf("EndsWith(view, \"string\"): %d\n", nlib_ns::EndsWith(view, "string"));
view = nlib_ns::StringView(" 12.345 ");
nlib_ns::Trim(view); // remove space characters
double val;
(void)nlib_ns::ToDouble(&val, view);
nlib_printf("ToDouble(\"12.345\"): %f\n", val);
/*
Output:
'this is my input string'.substr(5, 2): 'is'
StartsWith(view, "this"): 1
EndsWith(view, "string"): 1
ToDouble("12.345"): 12.345000
*/
See also
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html
Examples:
misc/stringutils/stringutils.cpp.

Definition at line 46 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 104 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 144 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 100 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);
Examples:
misc/stringutils/stringutils.cpp.

Definition at line 117 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 126 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 135 of file StringView.h.


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