Namespaces
Variants

std:: mbrlen

From cppreference.net
헤더 파일에 정의됨 <cwchar>
std:: size_t mbrlen ( const char * s, std:: size_t n, std:: mbstate_t * ps ) ;

s 가 가리키는 첫 번째 바이트로 시작하는 멀티바이트 문자 나머지 부분의 크기를 바이트 단위로 결정하며, 현재 변환 상태 ps 를 고려합니다.

이 함수는 std:: mbrtowc ( nullptr, s, n, ps ? ps : & internal ) 호출과 동등하며, 단일 숨겨진 객체 internal (타입 std::mbstate_t )를 사용한다는 점과 ps 표현식이 단 한 번만 평가된다는 점을 제외하면 동일합니다.

목차

매개변수

s - 멀티바이트 문자 문자열의 요소에 대한 포인터
n - 검사될 수 있는 s 내 바이트 수의 제한
ps - 변환 상태를 보유하는 변수에 대한 포인터

반환값

  • 0 다음 n 바이트 이내로 널 문자를 완성하는 경우
  • 유효한 멀티바이트 문자를 완성하는 바이트 수( 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]
변환 시 소비될 ExternT 문자열의 길이를 주어진 InternT 버퍼에 대해 계산합니다
( std::codecvt<InternT,ExternT,StateT> 의 가상 protected 멤버 함수)