Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mbrlen

      From cppreference.com
      <cpp‎ |string‎ |multibyte
       
       
       
       
      Defined in header<cwchar>
      std::size_t mbrlen(constchar* s,std::size_t n,std::mbstate_t* ps);

      Determines the size, in bytes, of the remainder of the multibyte character whose first byte is pointed to bys, given the current conversion stateps.

      This function is equivalent to the callstd::mbrtowc(nullptr, s, n, ps? ps:&internal) for some hidden objectinternal of typestd::mbstate_t, except that the expressionps is evaluated only once.

      Contents

      [edit]Parameters

      s - pointer to an element of a multibyte character string
      n - limit on the number of bytes in s that can be examined
      ps - pointer to the variable holding the conversion state

      [edit]Return value

      • 0 if the nextn or fewer bytes complete the null character.
      • The number of bytes (between1 andn) that complete a valid multibyte character.
      • std::size_t(-1) if encoding error occurs.
      • std::size_t(-2) if the nextn bytes are part of a possibly valid multibyte character, which is still incomplete after examining alln bytes.

      [edit]Example

      Run this code
      #include <clocale>#include <cwchar>#include <iostream>#include <string> int main(){// allow mbrlen() to work with UTF-8 multibyte encodingstd::setlocale(LC_ALL,"en_US.utf8"); // UTF-8 narrow multibyte encodingstd::string str="水";// or u8"\u6c34" or "\xe6\xb0\xb4"std::mbstate_t mb=std::mbstate_t(); // simple use: length of a complete multibyte characterconststd::size_t len= std::mbrlen(&str[0], str.size(),&mb);std::cout<<"The length of "<< str<<" is "<< len<<" bytes\n"; // advanced use: restarting in the middle of a multibyte characterconststd::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"; conststd::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"; // error case: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';}

      Output:

      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 characterAttempting to call mbrlen() in the middle of 水 while in initial shift state returns -1

      [edit]See also

      converts the next multibyte character to wide character, given state
      (function)[edit]
      returns the number of bytes in the next multibyte character
      (function)[edit]
      [virtual]
      calculates the length of theExternT string that would be consumed by conversion into givenInternT buffer
      (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>)[edit]
      C documentation formbrlen
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/multibyte/mbrlen&oldid=176689"

      [8]ページ先頭

      ©2009-2025 Movatter.jp