Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/codecvt mode"

From cppreference.com
< cpp‎ | locale
m (Text replace - "{{cpp|" to "{{c|")
m (Text replace - "{{tdcl list begin" to "{{dcl list begin")
Line 13: Line 13:
 
===Constants===
 
===Constants===
  
{{tdcl list begin}}
+
{{dcl list begin}}
 
{{tdcl list header | locale}}
 
{{tdcl list header | locale}}
 
{{tdcl list hitem | Value | Meaning }}
 
{{tdcl list hitem | Value | Meaning }}
Line 22: Line 22:
  
 
The recognized byte order marks are:
 
The recognized byte order marks are:
{{tdcl list begin}}
+
{{dcl list begin}}
 
{{tdcl list item | {{tt|0xfe 0xff}} | UTF-16 big-endian }}
 
{{tdcl list item | {{tt|0xfe 0xff}} | UTF-16 big-endian }}
 
{{tdcl list item | {{tt|0xff 0xfe}} | UTF-16 little-endian }}
 
{{tdcl list item | {{tt|0xff 0xfe}} | UTF-16 little-endian }}

Revision as of 01:15, 12 June 2012

Template:cpp/locale/sidebar

Defined in header <locale>
enum codecvt_mode {

    consume_header = 4,
    generate_header = 2,
    little_endian = 1

};

The facets std::codecvt_utf8, std::codecvt_utf16, and std::codecvt_utf8_utf16 accept an optional value of type std::codecvt_mode as a template argument, which specifies optional features of the unicode string conversion.

Constants

Template:tdcl list headerTemplate:tdcl list hitemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list end The recognized byte order marks are:
Template:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list itemTemplate:tdcl list end If std::consume_header is not selected when reading a file beginning with byte order mark, the Unicode character U+FEFF (Zero width non-breaking space) will be read as the first character of the string content.

Example

The following example demonstrates consuming the UTF-8 BOM

#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
int main()
{
    // UTF-8 data with BOM
    std::ofstream("text.txt") << u8"\ufeffz\u6c34\U0001d10b";
    // read the UTF8 file, skipping the BOM
    std::wifstream fin("text.txt");
    fin.imbue(std::locale(fin.getloc(),
                          new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
    for(wchar_t c; fin.get(c); )
        std::cout << std::hex << std::showbase << c << '\n';
}

Output:

0x7a
0x6c34
0x1d10b

See also

Template:cpp/locale/dcl list codecvtTemplate:cpp/locale/dcl list codecvt utf8Template:cpp/locale/dcl list codecvt utf16Template:cpp/locale/dcl list codecvt utf8 utf16