最終更新日時(UTC):
が更新

履歴 編集

class
<text_encoding>

std::text_encoding(C++26)

namespace std {
  struct text_encoding;
}

概要

std::text_encodingクラスは、IANA文字集合レジストリに基づいてテキストエンコーディングを識別するクラスである。エンコーディング名、MIB番号(Management Information Base enumeration)、およびエイリアスによるエンコーディングの識別・比較機能を提供する。

このクラスは、ロケールに依存せずにエンコーディングを取得・比較する手段を提供する。従来のC++ではエンコーディング情報はロケール経由でしか取得できなかったが、std::text_encodingはロケールとは独立して動作する。

  • literal()consteval関数であり、コンパイル時にリテラルエンコーディングを取得できる
  • environment()setlocale()の影響を受けず、プログラム起動時の環境エンコーディングを返す
  • environment_is()により、特定のエンコーディングかどうかを効率的に判定できる

std::text_encodingはトリビアルコピー可能な型であり、例外を送出しない。CHAR_BIT == 8が要求される。

IANAに登録されていないエンコーディングも、id::otherとして名前で管理できる。

メンバ関数

構築

名前 説明 対応バージョン
(constructor) コンストラクタ C++26

観測

名前 説明 対応バージョン
mib MIB列挙値の取得 C++26
name エンコーディング名の取得 C++26
aliases エイリアス一覧の取得 C++26

静的メンバ関数

名前 説明 対応バージョン
literal リテラルエンコーディングの取得 C++26
environment 環境エンコーディングの取得 C++26
environment_is 環境エンコーディングが指定したエンコーディングかを判定 C++26

メンバ型

名前 説明 対応バージョン
id MIB列挙型 (enum class) C++26
aliases_view エイリアスのビュー (class) C++26

メンバ定数

名前 説明 対応バージョン
static constexpr size_t max_name_length = 63; エンコーディング名の最大長 C++26

非メンバ関数

名前 説明 対応バージョン
operator== 等値比較 C++26
operator!= 非等値比較 (==により使用可能) 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());
}

出力例

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__"});
}

出力

バージョン

言語

  • C++26

処理系

参照