- text_encoding[meta header]
- std[meta namespace]
- class[meta id-type]
- cpp26[meta cpp]
namespace std {
struct text_encoding;
}
std::text_encodingクラスは、IANA文字集合レジストリに基づいてテキストエンコーディングを識別するクラスである。エンコーディング名、MIB番号(Management Information Base enumeration)、およびエイリアスによるエンコーディングの識別・比較機能を提供する。
このクラスは、ロケールに依存せずにエンコーディングを取得・比較する手段を提供する。従来のC++ではエンコーディング情報はロケール経由でしか取得できなかったが、std::text_encodingはロケールとは独立して動作する。
std::text_encodingはトリビアルコピー可能な型であり、例外を送出しない。CHAR_BIT == 8が要求される。
IANAに登録されていないエンコーディングも、id::otherとして名前で管理できる。
| 名前 |
説明 |
対応バージョン |
mib |
MIB列挙値の取得 |
C++26 |
name |
エンコーディング名の取得 |
C++26 |
aliases |
エイリアス一覧の取得 |
C++26 |
| 名前 |
説明 |
対応バージョン |
id |
MIB列挙型 (enum class) |
C++26 |
aliases_view |
エイリアスのビュー (class) |
C++26 |
| 名前 |
説明 |
対応バージョン |
static constexpr size_t max_name_length = 63; |
エンコーディング名の最大長 |
C++26 |
| 名前 |
説明 |
対応バージョン |
template<> struct hash<text_encoding> |
hashの特殊化 |
C++26 |
| 名前 |
説明 |
id mib_ = id::unknown; |
MIB値を保持する |
char name_[max_name_length + 1] = {0}; |
エンコーディング名を保持する |
comp-name |
エンコーディング名の比較(Unicode Technical Standard #22準拠) |
#include <text_encoding>
#include <print>
void print(const std::text_encoding& enc) {
std::println("{} (IANA MIB: {})", enc.name(), static_cast<int>(enc.mib()));
std::println("Aliases:");
for (auto&& alias : enc.aliases()) {
std::println(" {}", alias);
}
}
int main() {
std::println("Literal encoding: ");
::print(std::text_encoding::literal());
std::println("Environment encoding: ");
::print(std::text_encoding::environment());
}
- name()[link text_encoding/name.md]
- mib()[link text_encoding/mib.md]
- aliases()[link text_encoding/aliases.md]
- literal()[link text_encoding/literal.md]
- environment()[link text_encoding/environment.md]
Literal encoding: UTF-8 (IANA MIB: 106)
Aliases:
UTF-8
csUTF8
Environment encoding: UTF-8 (IANA MIB: 106)
Aliases:
UTF-8
csUTF8
#include <text_encoding>
#include <cassert>
#include <string_view>
int main() {
using namespace std::string_view_literals;
// エンコーディング名から構築
std::text_encoding my_utf8{"utf8"};
assert(my_utf8.name() == "utf8"sv);
assert(my_utf8.mib() == std::text_encoding::id::UTF8);
// MIB値から構築
std::text_encoding my_utf8_2{std::text_encoding::id::UTF8};
assert(my_utf8_2.mib() == std::text_encoding::id::UTF8);
// 異なる名前でも同じエンコーディングなら等値
assert(my_utf8 == my_utf8_2);
// 未登録のエンコーディングもサポート
std::text_encoding wtf8{"WTF-8"};
assert(wtf8.name() == "WTF-8"sv);
assert(wtf8.mib() == std::text_encoding::id::other);
// otherのMIBを持つエンコーディングは名前で比較される
assert(wtf8 == std::text_encoding{"___wtf8__"});
}
- name()[link text_encoding/name.md]
- mib()[link text_encoding/mib.md]
- id::UTF8[link text_encoding/id.md]
- id::other[link text_encoding/id.md]