UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
StringView

文字列を保持せず、参照するクラスです。std::string_view と同義です。std::string と異なり、ヒープアロケーションが発生しないため効率の良い文字列操作を行えます。次のように実装されています。

class StringView
{
const char* data;
size_t size;
};

data ポインタは確保済みの領域を指すのみで、メモリの所有権 (メモリを解放する義務) を持ちません。

個別インクルード

std::string との違い

std::string は動的にメモリを確保し文字列を保持します。そのため下記の例では 3 度、ヒープへのメモリアロケーションが発生します。

std::string src = "hello world !!";
std::string hello = src.substr(0, 5); // hello
std::string world = src.substr(6, 5); // world

StringView の場合、ヒープアロケーションは発生しません。"hello world !!" という、データ領域に確保されている文字列をポインタで指しているためです。そのためアロケーションは発生しませんが std::string と違い文字列の書き換えはできません。

Udon::StringView src = "hello world !!";
Udon::StringView hello = src.substr(0, 5); // hello
Udon::StringView world = src.substr(6, 5); // world
BasicStringView substr(const size_type pos, const size_type n=npos) const
指定された範囲のビューを作成する
Definition StringView.hpp:189
BasicStringView< char > StringView
Definition StringView.hpp:385

std::ostream オブジェクトへの出力

標準出力へ

#include <iostream>
int main()
{
Udon::StringView sv = "hello";
std::cout << sv << std::endl;
}

ファイルへ

#include <fstream>
int main()
{
Udon::StringView sv = "hello";
std::ofstream file{ "output.txt" };
file << sv;
}

USB シリアルへの出力

Arduino 環境では USB シリアルへ出力できます。出力値はシリアルモニターで確認できます。

void setup()
{
Serial.begin(115200);
}
void loop()
{
Udon::StringView sv = "hello";
sv.show(); // hello
sv.showRaw(); // [ h, e, l, l, o ]
}

サイズ

Udon::StringView sv = "hello";
size_t size = sv.size(); // 5
bool empty = sv.empty(); // false
constexpr size_type size() const noexcept
ビューのサイズを取得する
Definition StringView.hpp:137
constexpr bool empty() const noexcept
ビュー終端にヌル終端文字を含むことは保証されないので c_str() は提供しない
Definition StringView.hpp:147

文字アクセス

Udon::StringView sv = "hello";
char front = sv.front(); // h
char back = sv.back(); // 0
char l = sv.at(2); // l
char h = sv[0]; // h
const char* data = sv.data(); // 文字列先頭ポインタ
constexpr const_pointer data() const noexcept
ビューへのポインタを取得する
Definition StringView.hpp:130
constexpr const_reference back() const noexcept
ビューの終端の文字を取得する
Definition StringView.hpp:180
const_reference at(const size_type index) const noexcept
指定されたインデックスの文字を取得する
Definition StringView.hpp:161
constexpr const_reference front() const noexcept
ビューの先頭文字を取得する
Definition StringView.hpp:173

部分文字列作成

■ 指定された位置から N 文字の部分文字列を作成

Udon::StringView sv = "hello world";
Udon::StringView result = sv.substr(6, 5); // w から 5文字取得
// result: { "world" }
Udon::StringView sv = "hello world";
Udon::StringView result = sv.substr(1); // e から末尾まで
// result: { "ello world" }

■ 指定された終端文字までの部分文字列を作成

Udon::StringView sv = "hello world\n hogehoge";
Udon::StringView result = sv.substrUntil('\n');
// result: { "hello world" }
BasicStringView substrUntil(const char_type terminate) const
指定された終端文字までのビューを作成する
Definition StringView.hpp:213

■ 先頭の N 文字を削除した部分文字列を作成

Udon::StringView sv = "message: error";
// result: { "error" }
BasicStringView removePrefix(const size_type n) const
先頭のN文字を削除したビューを作成する
Definition StringView.hpp:224

■ 末尾の N 文字を削除した部分文字列を作成

Udon::StringView sv = "message: error";
// result: { "message" }
BasicStringView removeSuffix(const size_type n) const
末尾のN文字を削除したビューを作成する
Definition StringView.hpp:239

■ 指定された区切り文字で区切り、部分文字列のリストを作成

Udon::StringView sv = "Hello I am Japanese";
std::vector<Udon::StringView> result = sv.split(' ');
// result: { "Hello", "I", "am", "Japanese" }
std::vector< BasicStringView > split(const char_type delimiter) const
指定された区切り文字で区切り、ビューのリストを作成する
Definition StringView.hpp:270

文字列比較

■ 単純な文字列比較

Udon::StringView a = "hello";
Udon::StringView b = "hello";
bool eq = a == b; // true
bool ne = a != b; // false

■ 指定の文字列で始まっているか判定

Udon::StringView src = "hello world";
bool result = src.startsWith("hello"); // result: true
bool startsWith(const BasicStringView &string) const noexcept
ビューが指定したビューから始まっているか判定する
Definition StringView.hpp:254

■ 指定の文字列で終わっているか判定

Udon::StringView src = "hello world";
bool result = src.endsWith("world"); // result: true
bool endsWith(const BasicStringView &string) const noexcept
ビューが指定したビューで終わっているか判定する
Definition StringView.hpp:262

イテレータ

イテレータを定義しているため for 文などでイテレーションできます。

Udon::StringView sv = "hello";
for (char c : sv)
{
}
Udon::StringView sv = "hello world";
auto it = std::find(sv.begin(), sv.end(), 'w');
if (it != sv.end())
{
int index = std::distance(sv.begin(), it);
// index: 6
}

数値への変換

Udon::StringView string = "1234";
if (Udon::Optional<int> numopt = string.toNumber<int>())
{
int number = *numopt;
// number: 1234
}
オプショナル型
Definition Optional.hpp:62
Udon::StringView string = "Double: 1234.12";
if (Udon::Optional<double> numopt = string.removePrefix(8).toNumber<double>())
{
double number = *numopt;
// number: 1234.12
}

入れ替え

Udon::StringView sv1 = "hello";
Udon::StringView sv2 = "world";
std::swap(sv1, sv2);
// sv1: { "world" }
// sv2: { "hello" }
Udon::StringView sv1 = "hello";
Udon::StringView sv2 = "world";
sv1.swap(sv2);
// sv1: { "world" }
// sv2: { "hello" }
void swap(BasicStringView &other) noexcept
ビューを入れ替える
Definition StringView.hpp:313

文字列リテラル

using namespace Udon::Literals;
auto sv = "hello world"_sv;
Definition StringView.hpp:393

std::string への変換

Udon::StringView sv = "hello";
std::string str = sv.toString();
std::basic_string< char_type > toString() const
STL の文字列に変換する
Definition StringView.hpp:306