C++ Locale Library - length



Description

It returns the number of external characters in the range [from,from_end) that could be translated into at maximum of max internal characters, output as if applying codecvt::in.

Declaration

Following is the declaration for std::ctype::length.

C++98

	
int length (state_type& state, const extern_type* from,
            const extern_type* from_end, size_t max) const;

C++11

int length (state_type& state, const extern_type* from,
            const extern_type* from_end, size_t max) const;

Parameters

  • state − It is a state object.

  • from, from_end − It used to find an initial and final characters of the source sequence.

  • max − It is used to find maximum length of the translated sequence.

Return Value

It returns the length of the sequence of characters, in terms of translated internal characters.

Exceptions

No-throw guarantee − never throws exceptions even if an exception is thrown, there are no changes in the facet object.

Data races

The facet object is accessed.

Example

In below example explains about std::ctype::length.

#include <iostream>
#include <locale>
#include <cwchar>
#include <cstddef>

int main () {
   typedef std::codecvt<wchar_t,char,std::mbstate_t> facet_type;

   std::locale loc;
   const facet_type& myfacet = std::use_facet<facet_type>(loc);

   const char source[] = "sairamkrishna mammahe";
  
   std::mbstate_t mystate;
   const char * pc;
   wchar_t * pwc;

   std::size_t length = myfacet.length (mystate, source, source+sizeof(source), 30);

   wchar_t* dest = new wchar_t[length];
   myfacet.in (mystate, source, source+sizeof(source), pc, dest, dest+length, pwc);

   std::wcout << dest << std::endl;

   delete[] dest;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

sairamkrishna mammahe
locale.htm
Advertisements