std:: mbrlen
|
定義済みヘッダー
<cwchar>
|
||
|
std::
size_t
mbrlen
(
const
char
*
s,
std::
size_t
n,
std::
mbstate_t
*
ps
)
;
|
||
現在の変換状態 ps が与えられたとき、 s が指す最初のバイトから始まるマルチバイト文字の残りの部分のサイズをバイト単位で決定します。
この関数は、型 std::mbstate_t の隠れオブジェクト internal に対して std:: mbrtowc ( nullptr, s, n, ps ? ps : & internal ) を呼び出すのと等価ですが、式 ps は一度だけ評価される点が異なります。
目次 |
パラメータ
| s | - | マルチバイト文字列の要素へのポインタ |
| n | - | sで検査可能なバイト数の制限 |
| ps | - | 変換状態を保持する変数へのポインタ |
戻り値
- 0 次の n バイト以内でnull文字が完成する場合
- 有効なマルチバイト文字を完成させるバイト数( 1 から n の間)
- std:: size_t ( - 1 ) エンコーディングエラーが発生した場合
- std:: size_t ( - 2 ) 次の n バイトが有効な可能性のあるマルチバイト文字の一部であり、 n バイトすべてを検査した後もまだ不完全な場合
例
#include <clocale> #include <cwchar> #include <iostream> #include <string> int main() { // mbrlen()がUTF-8マルチバイトエンコーディングで動作するように設定 std::setlocale(LC_ALL, "en_US.utf8"); // UTF-8ナローマルチバイトエンコーディング std::string str = "水"; // or u8"\u6c34" or "\xe6\xb0\xb4" std::mbstate_t mb = std::mbstate_t(); // 基本的な使用法:完全なマルチバイト文字の長さ const std::size_t len = std::mbrlen(&str[0], str.size(), &mb); std::cout << "The length of " << str << " is " << len << " bytes\n"; // 高度な使用法:マルチバイト文字の途中からの再開 const std::size_t len1 = std::mbrlen(&str[0], 1, &mb); if (len1 == std::size_t(-2)) std::cout << "The first 1 byte of " << str << " is an incomplete multibyte char (mbrlen returns -2)\n"; const std::size_t len2 = std::mbrlen(&str[1], str.size() - 1, &mb); std::cout << "The remaining " << str.size() - 1 << " bytes of " << str << " hold " << len2 << " bytes of the multibyte character\n"; // エラーケース: std::cout << "Attempting to call mbrlen() in the middle of " << str << " while in initial shift state returns " << (int)mbrlen(&str[1], str.size(), &mb) << '\n'; }
出力:
The length of 水 is 3 bytes. The first 1 byte of 水 is an incomplete multibyte char (mbrlen returns -2) The remaining 2 bytes of 水 hold 2 bytes of the multibyte character Attempting to call mbrlen() in the middle of 水 while in initial shift state returns -1
関連項目
|
マルチバイト文字をワイド文字に変換する(状態を指定)
(関数) |
|
|
次のマルチバイト文字のバイト数を返す
(関数) |
|
|
[virtual]
|
指定された
InternT
バッファへの変換で消費される
ExternT
文字列の長さを計算する
(
std::codecvt<InternT,ExternT,StateT>
の仮想保護メンバ関数)
|
|
Cドキュメント
for
mbrlen
|
|