Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
SkStringUtils.cpp File Reference
#include "src/core/SkStringUtils.h"
#include "include/core/SkString.h"
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkTArray.h"
#include "src/base/SkFloatBits.h"
#include "src/base/SkUTF.h"
#include <cstring>

Go to the source code of this file.

Functions

void SkAppendScalar (SkString *str, SkScalar value, SkScalarAsStringType asType)
 
SkString SkTabString (const SkString &string, int tabCnt)
 
SkString SkStringFromUTF16 (const uint16_t *src, size_t count)
 
void SkStrSplit (const char *str, const char *delimiters, SkStrSplitMode splitMode, TArray< SkString > *out)
 

Function Documentation

◆ SkAppendScalar()

void SkAppendScalar ( SkString str,
SkScalar  value,
SkScalarAsStringType  asType 
)

Definition at line 20 of file SkStringUtils.cpp.

20 {
21 switch (asType) {
23 str->appendf("SkBits2Float(0x%08x)", SkFloat2Bits(value));
24 break;
26 SkString tmp;
27 tmp.printf("%.9g", value);
28 if (tmp.contains('.')) {
29 tmp.appendUnichar('f');
30 }
31 str->append(tmp);
32 break;
33 }
34 }
35}
static uint32_t SkFloat2Bits(float value)
Definition SkFloatBits.h:41
@ kHex_SkScalarAsStringType
@ kDec_SkScalarAsStringType
void appendUnichar(SkUnichar uni)
Definition SkString.h:207
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
void append(const char text[])
Definition SkString.h:203
bool contains(const char substring[]) const
Definition SkString.h:152
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:550

◆ SkStringFromUTF16()

SkString SkStringFromUTF16 ( const uint16_t *  src,
size_t  count 
)

Definition at line 64 of file SkStringUtils.cpp.

64 {
65 SkString ret;
66 const uint16_t* stop = src + count;
67 if (count > 0) {
68 SkASSERT(src);
69 size_t n = 0;
70 const uint16_t* end = src + count;
71 for (const uint16_t* ptr = src; ptr < end;) {
72 const uint16_t* last = ptr;
73 SkUnichar u = SkUTF::NextUTF16(&ptr, stop);
74 size_t s = SkUTF::ToUTF8(u);
75 if (n > UINT32_MAX - s) {
76 end = last; // truncate input string
77 break;
78 }
79 n += s;
80 }
81 ret = SkString(n);
82 char* out = ret.data();
83 for (const uint16_t* ptr = src; ptr < end;) {
84 out += SkUTF::ToUTF8(SkUTF::NextUTF16(&ptr, stop), out);
85 }
86 SkASSERT(out == ret.data() + n);
87 }
88 return ret;
89}
int count
#define SkASSERT(cond)
Definition SkAssert.h:116
int32_t SkUnichar
Definition SkTypes.h:175
const char * data() const
Definition SkString.h:132
struct MyStruct s
glong glong end
SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence]=nullptr)
SK_SPI SkUnichar NextUTF16(const uint16_t **ptr, const uint16_t *end)
Definition SkUTF.cpp:159

◆ SkStrSplit()

void SkStrSplit ( const char *  str,
const char *  delimiters,
SkStrSplitMode  splitMode,
TArray< SkString > *  out 
)

Definition at line 91 of file SkStringUtils.cpp.

94 {
95 if (splitMode == kCoalesce_SkStrSplitMode) {
96 // Skip any delimiters.
97 str += strspn(str, delimiters);
98 }
99 if (!*str) {
100 return;
101 }
102
103 while (true) {
104 // Find a token.
105 const size_t len = strcspn(str, delimiters);
106 if (splitMode == kStrict_SkStrSplitMode || len > 0) {
107 out->push_back().set(str, len);
108 str += len;
109 }
110
111 if (!*str) {
112 return;
113 }
114 if (splitMode == kCoalesce_SkStrSplitMode) {
115 // Skip any delimiters.
116 str += strspn(str, delimiters);
117 } else {
118 // Skip one delimiter.
119 str += 1;
120 }
121 }
122}
@ kStrict_SkStrSplitMode
@ kCoalesce_SkStrSplitMode

◆ SkTabString()

SkString SkTabString ( const SkString string,
int  tabCnt 
)

Indents every non-empty line of the string by tabCnt tabs

Definition at line 37 of file SkStringUtils.cpp.

37 {
38 if (tabCnt <= 0) {
39 return string;
40 }
41 SkString tabs;
42 for (int i = 0; i < tabCnt; ++i) {
43 tabs.append("\t");
44 }
46 static const char newline[] = "\n";
47 const char* input = string.c_str();
48 int nextNL = SkStrFind(input, newline);
49 while (nextNL >= 0) {
50 if (nextNL > 0) {
51 result.append(tabs);
52 }
53 result.append(input, nextNL + 1);
54 input += nextNL + 1;
55 nextNL = SkStrFind(input, newline);
56 }
57 if (*input != '\0') {
58 result.append(tabs);
59 result.append(input);
60 }
61 return result;
62}
static int SkStrFind(const char string[], const char substring[])
Definition SkString.h:41
const char * c_str() const
Definition SkString.h:133
GAsyncResult * result