nlib
nn::nlib::StringView クラス

std::stringが持つメソッドをstd::stringを構築せずに利用するためのクラスです。 C++17のstring_viewにtypedefされる予定です。 StringViewを引数に取るフリー関数についてはこちらを御覧ください。 [詳解]

#include "nn/nlib/StringView.h"

公開型

typedef char charT
 現在はchar型専用のクラスです。
 
typedef charT value_type
 文字の型です。
 
typedef const charTpointer
 const_pointerと同一です。
 
typedef const charTconst_pointer
 要素への読み取り専用ポインタです。
 
typedef const charTreference
 const_referenceと同一です。
 
typedef const charTconst_reference
 要素への読み取り専用参照です。
 
typedef const_pointer const_iterator
 読み取り専用ランダムアクセス反復子です。
 
typedef const_iterator iterator
 const_iteratorと同一です。
 
typedef std::reverse_iterator< const_iteratorconst_reverse_iterator
 読み取り専用逆反復子です。
 
typedef const_reverse_iterator reverse_iterator
 const_reverse_iteratorと同一です。
 
typedef size_t size_type
 非負整数型で、現在はsize_ttypedefされています。
 
typedef ptrdiff_t difference_type
 反復子の差分をとったときに返される型です。
 

公開メンバ関数

constexpr StringView () noexcept
 デフォルトコンストラクタです。 空文字列で初期化されます。
 
 StringView (const charT *str) noexcept
 str を参照するように初期化されます。内部で文字列長が計算されます。
 
 StringView (const charT *str, size_type len) noexcept
 (計算済みの)文字列長を与えて初期化します。
 
const_iterator begin () const noexcept
 先頭要素を指す読み取り専用反復子を取得します。
 
const_iterator end () const noexcept
 末尾の次を指す読み取り専用反復子を取得します。
 
const_iterator cbegin () const noexcept
 先頭要素を指す読み取り専用反復子を取得します。
 
const_iterator cend () const noexcept
 末尾の次を指す読み取り専用反復子を取得します。
 
const_reverse_iterator rbegin () const noexcept
 末尾要素を指す読み取り専用逆反復子を取得します。
 
const_reverse_iterator rend () const noexcept
 先頭の前を指す読み取り専用逆反復子を取得します。
 
const_reverse_iterator crbegin () const noexcept
 末尾要素を指す読み取り専用逆反復子を取得します。
 
const_reverse_iterator crend () const noexcept
 先頭の前を指す読み取り専用逆反復子を取得します。
 
size_type size () const noexcept
 文字列長を返します。
 
size_type length () const noexcept
 文字列長を返します。
 
size_type max_size () const noexcept
 文字列長の最大値を返します。
 
bool empty () const noexcept
 空文字列であればtrue、そうでなければfalseを返します。
 
const charToperator[] (size_type pos) const
 pos 番目の文字を取得します。 [詳解]
 
const charTat (size_type pos) const
 pos 番目の文字を取得します。 [詳解]
 
const charTfront () const
 最初の要素への参照を取得します。
 
const charTback () const
 最後の要素への参照を取得します。
 
const charTdata () const noexcept
 最初の文字へのポインタを返します。
 
void clear () noexcept
 空文字列を設定します。
 
void remove_prefix (size_type n) noexcept
 最初のn 文字を取り除きます。 [詳解]
 
void remove_suffix (size_type n) noexcept
 最後のn 文字を取り除きます。 [詳解]
 
StringView substr (size_type pos, size_type n=npos) const noexcept
 部分文字列[pos, pos + n)を返します。 [詳解]
 
int compare (const StringView &s) const noexcept
 文字列を比較します。 [詳解]
 
int compare (const charT *s) const noexcept
 文字列を比較します。 [詳解]
 

静的公開変数類

static const size_type npos = size_type(-1)
 std::stringnposと同様に用います。
 

詳解

std::stringが持つメソッドをstd::stringを構築せずに利用するためのクラスです。 C++17のstring_viewにtypedefされる予定です。 StringViewを引数に取るフリー関数についてはこちらを御覧ください。

説明
以下がコード例です。
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
*/
参照
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3609.html
各種例:
misc/stringutils/stringutils.cpp.

StringView.h46 行目に定義があります。

関数詳解

◆ at()

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

pos 番目の文字を取得します。

引数
[in]pos文字へのインデックス
戻り値
pos 番目の文字

StringView.h104 行目に定義があります。

◆ compare() [1/2]

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

文字列を比較します。

引数
[in]s比較対象の文字列
戻り値
std::strcmp()と同様の値

◆ compare() [2/2]

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

文字列を比較します。

引数
[in]s比較対象の文字列
戻り値
std::strcmp()と同様の値

StringView.h144 行目に定義があります。

◆ operator[]()

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

pos 番目の文字を取得します。

引数
[in]pos文字へのインデックス
戻り値
pos 番目の文字

StringView.h100 行目に定義があります。

◆ remove_prefix()

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

最初のn 文字を取り除きます。

引数
[in]n除去する文字数
説明
効果は以下のコードと一緒です。
*this = this->substr(n, npos);
各種例:
misc/stringutils/stringutils.cpp.

StringView.h117 行目に定義があります。

◆ remove_suffix()

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

最後のn 文字を取り除きます。

引数
[in]n除去する文字数
説明
効果は以下のコードと一緒です。
*this = this->substr(0, this->size() - n);

StringView.h126 行目に定義があります。

◆ substr()

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

部分文字列[pos, pos + n)を返します。

引数
[in]pos部分文字列の最初の位置
[in]n部分文字列の長さ
戻り値
部分文字列[pos, pos + n)、を表すStringViewオブジェクト

StringView.h135 行目に定義があります。


このクラス詳解は次のファイルから抽出されました: