Flutter Engine
The Flutter Engine
SkUnicode.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
11
12using namespace skia_private;
13
14SkString SkUnicode::convertUtf16ToUtf8(const char16_t* utf16, int utf16Units) {
15
16 int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, (uint16_t*)utf16, utf16Units);
17 if (utf8Units < 0) {
18 SkDEBUGF("Convert error: Invalid utf16 input");
19 return SkString();
20 }
21 AutoTArray<char> utf8(utf8Units);
22 SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8.data(), utf8Units, (uint16_t*)utf16, utf16Units);
23 SkASSERT(dstLen == utf8Units);
24
25 return SkString(utf8.data(), utf8Units);
26}
27
28SkString SkUnicode::convertUtf16ToUtf8(const std::u16string& utf16) {
29 return convertUtf16ToUtf8(utf16.c_str(), utf16.size());
30}
31
32std::u16string SkUnicode::convertUtf8ToUtf16(const char* utf8, int utf8Units) {
33
34 int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
35 if (utf16Units < 0) {
36 SkDEBUGF("Convert error: Invalid utf8 input");
37 return std::u16string();
38 }
39
40 AutoTArray<uint16_t> utf16(utf16Units);
41 SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.data(), utf16Units, utf8, utf8Units);
42 SkASSERT(dstLen == utf16Units);
43
44 return std::u16string((char16_t *)utf16.data(), utf16Units);
45}
46
48 return convertUtf8ToUtf16(utf8.c_str(), utf8.size());
49}
50
53}
54
57}
58
61}
62
65}
66
69}
70
73}
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkDEBUGF(...)
Definition: SkDebug.h:24
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
static std::u16string convertUtf8ToUtf16(const char *utf8, int utf8Units)
Definition: SkUnicode.cpp:32
static bool hasSoftLineBreakFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:59
static bool hasTabulationFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:51
static bool hasGraphemeStartFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:63
static bool hasControlFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:67
static bool hasPartOfWhiteSpaceBreakFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:71
static bool hasHardLineBreakFlag(SkUnicode::CodeUnitFlags flags)
Definition: SkUnicode.cpp:55
@ kPartOfWhiteSpaceBreak
Definition: SkUnicode.h:81
@ kHardLineBreakBefore
Definition: SkUnicode.h:84
@ kTabulation
Definition: SkUnicode.h:87
@ kGraphemeStart
Definition: SkUnicode.h:82
@ kControl
Definition: SkUnicode.h:86
@ kSoftLineBreakBefore
Definition: SkUnicode.h:83
static SkString convertUtf16ToUtf8(const char16_t *utf16, int utf16Units)
Definition: SkUnicode.cpp:14
const T * data() const
Definition: SkTemplates.h:133
FlutterSemanticsFlag flags
SK_SPI int UTF8ToUTF16(uint16_t dst[], int dstCapacity, const char src[], size_t srcByteLength)
Definition: SkUTF.cpp:259
SK_SPI int UTF16ToUTF8(char dst[], int dstCapacity, const uint16_t src[], size_t srcLength)
Definition: SkUTF.cpp:291