MCPcopy Create free account
hub / github.com/CodingGay/BlackDex / CountUtf8Bytes

Function CountUtf8Bytes

Bcore/src/main/cpp/dex/utf.cc:239–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

237}
238
239size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count) {
240 size_t result = 0;
241 const uint16_t *end = chars + char_count;
242 while (chars < end) {
243 const uint16_t ch = *chars++;
244 if (LIKELY(ch != 0 && ch < 0x80)) {
245 result++;
246 continue;
247 }
248 if (ch < 0x800) {
249 result += 2;
250 continue;
251 }
252 if (ch >= 0xd800 && ch < 0xdc00) {
253 if (chars < end) {
254 const uint16_t ch2 = *chars;
255 // If we find a properly paired surrogate, we emit it as a 4 byte
256 // UTF sequence. If we find an unpaired leading or trailing surrogate,
257 // we emit it as a 3 byte sequence like would have done earlier.
258 if (ch2 >= 0xdc00 && ch2 < 0xe000) {
259 chars++;
260 result += 4;
261 continue;
262 }
263 }
264 }
265 result += 3;
266 }
267 return result;
268}
269
270static inline constexpr bool NeedsEscaping(uint16_t ch) {
271 return (ch < ' ' || ch > '~');

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected