Adapted from SciTE UniConversion.cxx. Copyright 1998-2001 by Neil Hodgson Modified for Artistic Style by Jim Pattee. Compute the length of an output utf-8 file given a utf-16 file. Input tlen is the size in BYTES (not wchar_t).
| 2074 | // Compute the length of an output utf-8 file given a utf-16 file. |
| 2075 | // Input tlen is the size in BYTES (not wchar_t). |
| 2076 | size_t ASConsole::Utf8LengthFromUtf16(const char* data, size_t tlen, FileEncoding encoding) const |
| 2077 | { |
| 2078 | enum { SURROGATE_LEAD_FIRST = 0xD800 }; |
| 2079 | enum { SURROGATE_TRAIL_LAST = 0xDFFF }; |
| 2080 | |
| 2081 | size_t len = 0; |
| 2082 | size_t wcharLen = tlen / 2; |
| 2083 | const short* uptr = reinterpret_cast<const short*>(data); |
| 2084 | for (size_t i = 0; i < wcharLen && uptr[i];) |
| 2085 | { |
| 2086 | size_t uch = encoding == UTF_16BE ? swap16bit(uptr[i]) : uptr[i]; |
| 2087 | if (uch < 0x80) |
| 2088 | { |
| 2089 | len++; |
| 2090 | } |
| 2091 | else if (uch < 0x800) |
| 2092 | { |
| 2093 | len += 2; |
| 2094 | } |
| 2095 | else if ((uch >= SURROGATE_LEAD_FIRST) && (uch <= SURROGATE_TRAIL_LAST)) |
| 2096 | { |
| 2097 | len += 4; |
| 2098 | i++; |
| 2099 | } |
| 2100 | else |
| 2101 | { |
| 2102 | len += 3; |
| 2103 | } |
| 2104 | i++; |
| 2105 | } |
| 2106 | return len; |
| 2107 | } |
| 2108 | |
| 2109 | // Adapted from SciTE Utf8_16.cxx. |
| 2110 | // Copyright (C) 2002 Scott Kirkwood. |
nothing calls this directly
no outgoing calls
no test coverage detected