Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::codecvt<InternT,ExternT,StateT>::length, do_length

      From cppreference.com
      <cpp‎ |locale‎ |codecvt
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:

      int length( StateT& state,const ExternT* from,const ExternT* from_end,

                 std::size_t max)const;
      (1)
      protected:

      virtualint do_length( StateT& state,const ExternT* from,const ExternT* from_end,

                             std::size_t max)const;
      (2)
      1) Public member function, calls the member functiondo_length of the most derived class.
      2) Attempts to convert theExternT characters from the character array defined by[fromfrom_end), given initial conversion statestate, to at mostmaxInternT characters, and returns the number ofExternT characters that such conversion would consume. Modifiesstate as if by executingdo_in(state, from, from_end, from, to, to+ max, to) for some imaginary[toto+ max) output buffer.

      Contents

      [edit]Return value

      The number ofExternT characters that would be consumed if converted bydo_in() until either allfrom_end- from characters were consumed ormaxInternT characters were produced, or a conversion error occurred.

      The non-converting specializationstd::codecvt<char,char,std::mbstate_t> returnsstd::min(max, from_end- from).

      [edit]Example

      Run this code
      #include <iostream>#include <locale>#include <string> int main(){using facet_type=std::codecvt<wchar_t,char,std::mbstate_t>; // narrow multibyte encodingstd::string s="z\u00df\u6c34\U0001d10b";// or u8"zß水𝄋"// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b" std::locale loc("en_US.UTF-8");    facet_typeconst& codecvt_facet=std::use_facet<facet_type>(loc);std::mbstate_t mb=std::mbstate_t();std::cout<<"Only the first "<< codecvt_facet.length(mb, s.data(), s.data()+ s.size(),2)<<" bytes out of "<< s.size()<<" would be consumed"" to produce the first 2 characters\n";}

      Output:

      Only the first 3 bytes out of 10 would be consumed to produce the first 2 characters

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 75C++98the effect onstate was not specifiedspecified
      LWG 305C++98std::codecvt<wchar_t, char, std::mbstate_t>::do_length
      was required to returnstd::min(max, from_end- from)
      not required

      [edit]See also

      [virtual]
      converts a string fromExternT toInternT, such as when reading from file
      (virtual protected member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/codecvt/length&oldid=176850"

      [8]ページ先頭

      ©2009-2025 Movatter.jp