Flutter Engine
The Flutter Engine
Classes | Functions | Variables
SkString.h File Reference
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkTo.h"
#include "include/private/base/SkTypeTraits.h"
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>

Go to the source code of this file.

Classes

class  SkString
 

Functions

static bool SkStrStartsWith (const char string[], const char prefixStr[])
 
static bool SkStrStartsWith (const char string[], const char prefixChar)
 
bool SkStrEndsWith (const char string[], const char suffixStr[])
 
bool SkStrEndsWith (const char string[], const char suffixChar)
 
int SkStrStartsWithOneOf (const char string[], const char prefixes[])
 
static int SkStrFind (const char string[], const char substring[])
 
static int SkStrFindLastOf (const char string[], const char subchar)
 
static bool SkStrContains (const char string[], const char substring[])
 
static bool SkStrContains (const char string[], const char subchar)
 
char * SkStrAppendU32 (char buffer[], uint32_t)
 
char * SkStrAppendU64 (char buffer[], uint64_t, int minDigits)
 
char * SkStrAppendS32 (char buffer[], int32_t)
 
char * SkStrAppendS64 (char buffer[], int64_t, int minDigits)
 
char * SkStrAppendScalar (char buffer[], SkScalar)
 
SK_API SkString SkStringPrintf (const char *format,...) SK_PRINTF_LIKE(1
 Creates a new string and writes into it using a printf()-style format. More...
 
SK_API SkString static SkString SkStringPrintf ()
 
static void swap (SkString &a, SkString &b)
 

Variables

static constexpr int kSkStrAppendU32_MaxSize = 10
 
static constexpr int kSkStrAppendU64_MaxSize = 20
 
static constexpr int kSkStrAppendS32_MaxSize = kSkStrAppendU32_MaxSize + 1
 
static constexpr int kSkStrAppendS64_MaxSize = kSkStrAppendU64_MaxSize + 1
 
static constexpr int kSkStrAppendScalar_MaxSize = 15
 

Function Documentation

◆ SkStrAppendS32()

char * SkStrAppendS32 ( char  buffer[],
int32_t  dec 
)

Definition at line 120 of file SkString.cpp.

120 {
121 uint32_t udec = dec;
122 if (dec < 0) {
123 *string++ = '-';
124 udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
125 }
126 return SkStrAppendU32(string, udec);
127}
char * SkStrAppendU32(char string[], uint32_t dec)
Definition: SkString.cpp:100

◆ SkStrAppendS64()

char * SkStrAppendS64 ( char  buffer[],
int64_t  dec,
int  minDigits 
)

Definition at line 155 of file SkString.cpp.

155 {
156 uint64_t udec = dec;
157 if (dec < 0) {
158 *string++ = '-';
159 udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
160 }
161 return SkStrAppendU64(string, udec, minDigits);
162}
char * SkStrAppendU64(char string[], uint64_t dec, int minDigits)
Definition: SkString.cpp:129

◆ SkStrAppendScalar()

char * SkStrAppendScalar ( char  buffer[],
SkScalar  value 
)

Write the scalar in decimal format into buffer, and return a pointer to the next char after the last one written. Note: a terminating 0 is not written into buffer, which must be at least kSkStrAppendScalar_MaxSize. Thus if the caller wants to add a 0 at the end, buffer must be at least kSkStrAppendScalar_MaxSize + 1 bytes large.

Definition at line 164 of file SkString.cpp.

164 {
165 // Handle infinity and NaN ourselves to ensure consistent cross-platform results.
166 // (e.g.: `inf` versus `1.#INF00`, `nan` versus `-nan` for high-bit-set NaNs)
167 if (SkIsNaN(value)) {
168 strcpy(string, "nan");
169 return string + 3;
170 }
171 if (!SkIsFinite(value)) {
172 if (value > 0) {
173 strcpy(string, "inf");
174 return string + 3;
175 } else {
176 strcpy(string, "-inf");
177 return string + 4;
178 }
179 }
180
181 // since floats have at most 8 significant digits, we limit our %g to that.
182 static const char gFormat[] = "%.8g";
183 // make it 1 larger for the terminating 0
185 int len = snprintf(buffer, sizeof(buffer), gFormat, value);
186 memcpy(string, buffer, len);
188 return string + len;
189}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr bool SkIsNaN(T x)
static bool SkIsFinite(T x, Pack... values)
static constexpr int kSkStrAppendScalar_MaxSize
Definition: SkString.h:101
uint8_t value
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ SkStrAppendU32()

char * SkStrAppendU32 ( char  buffer[],
uint32_t  dec 
)

Definition at line 100 of file SkString.cpp.

100 {
101 SkDEBUGCODE(char* start = string;)
102
104 char* p = buffer + sizeof(buffer);
105
106 do {
107 *--p = SkToU8('0' + dec % 10);
108 dec /= 10;
109 } while (dec != 0);
110
111 SkASSERT(p >= buffer);
112 size_t cp_len = buffer + sizeof(buffer) - p;
113 memcpy(string, p, cp_len);
114 string += cp_len;
115
117 return string;
118}
static constexpr int kSkStrAppendU32_MaxSize
Definition: SkString.h:84
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22

◆ SkStrAppendU64()

char * SkStrAppendU64 ( char  buffer[],
uint64_t  dec,
int  minDigits 
)

Definition at line 129 of file SkString.cpp.

129 {
130 SkDEBUGCODE(char* start = string;)
131
133 char* p = buffer + sizeof(buffer);
134
135 do {
136 *--p = SkToU8('0' + (int32_t) (dec % 10));
137 dec /= 10;
138 minDigits--;
139 } while (dec != 0);
140
141 while (minDigits > 0) {
142 *--p = '0';
143 minDigits--;
144 }
145
146 SkASSERT(p >= buffer);
147 size_t cp_len = buffer + sizeof(buffer) - p;
148 memcpy(string, p, cp_len);
149 string += cp_len;
150
152 return string;
153}
static constexpr int kSkStrAppendU64_MaxSize
Definition: SkString.h:86

◆ SkStrContains() [1/2]

static bool SkStrContains ( const char  string[],
const char  subchar 
)
inlinestatic

Definition at line 58 of file SkString.h.

58 {
59 SkASSERT(string);
60 char tmp[2];
61 tmp[0] = subchar;
62 tmp[1] = '\0';
63 return (-1 != SkStrFind(string, tmp));
64}
static int SkStrFind(const char string[], const char substring[])
Definition: SkString.h:41

◆ SkStrContains() [2/2]

static bool SkStrContains ( const char  string[],
const char  substring[] 
)
inlinestatic

Definition at line 53 of file SkString.h.

53 {
54 SkASSERT(string);
55 SkASSERT(substring);
56 return (-1 != SkStrFind(string, substring));
57}

◆ SkStrEndsWith() [1/2]

bool SkStrEndsWith ( const char  string[],
const char  suffixChar 
)

Definition at line 77 of file SkString.cpp.

77 {
78 SkASSERT(string);
79 size_t strLen = strlen(string);
80 if (0 == strLen) {
81 return false;
82 } else {
83 return (suffixChar == string[strLen-1]);
84 }
85}

◆ SkStrEndsWith() [2/2]

bool SkStrEndsWith ( const char  string[],
const char  suffixStr[] 
)

Definition at line 68 of file SkString.cpp.

68 {
69 SkASSERT(string);
70 SkASSERT(suffixStr);
71 size_t strLen = strlen(string);
72 size_t suffixLen = strlen(suffixStr);
73 return strLen >= suffixLen &&
74 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen);
75}

◆ SkStrFind()

static int SkStrFind ( const char  string[],
const char  substring[] 
)
inlinestatic

Definition at line 41 of file SkString.h.

41 {
42 const char *first = strstr(string, substring);
43 if (nullptr == first) return -1;
44 return SkToInt(first - &string[0]);
45}
constexpr int SkToInt(S x)
Definition: SkTo.h:29

◆ SkStrFindLastOf()

static int SkStrFindLastOf ( const char  string[],
const char  subchar 
)
inlinestatic

Definition at line 47 of file SkString.h.

47 {
48 const char* last = strrchr(string, subchar);
49 if (nullptr == last) return -1;
50 return SkToInt(last - &string[0]);
51}

◆ SkStringPrintf() [1/2]

SK_API SkString static SkString SkStringPrintf ( )
inlinestatic

This makes it easier to write a caller as a VAR_ARGS function where the format string is optional.

Definition at line 287 of file SkString.h.

287{ return SkString(); }

◆ SkStringPrintf() [2/2]

SK_API SkString SkStringPrintf ( const char *  format,
  ... 
)

Creates a new string and writes into it using a printf()-style format.

◆ SkStrStartsWith() [1/2]

static bool SkStrStartsWith ( const char  string[],
const char  prefixChar 
)
inlinestatic

Definition at line 31 of file SkString.h.

31 {
32 SkASSERT(string);
33 return (prefixChar == *string);
34}

◆ SkStrStartsWith() [2/2]

static bool SkStrStartsWith ( const char  string[],
const char  prefixStr[] 
)
inlinestatic

Definition at line 26 of file SkString.h.

26 {
27 SkASSERT(string);
28 SkASSERT(prefixStr);
29 return !strncmp(string, prefixStr, strlen(prefixStr));
30}

◆ SkStrStartsWithOneOf()

int SkStrStartsWithOneOf ( const char  string[],
const char  prefixes[] 
)

Definition at line 87 of file SkString.cpp.

87 {
88 int index = 0;
89 do {
90 const char* limit = strchr(prefixes, '\0');
91 if (!strncmp(string, prefixes, limit - prefixes)) {
92 return index;
93 }
94 prefixes = limit + 1;
95 index++;
96 } while (prefixes[0]);
97 return -1;
98}

◆ swap()

static void swap ( SkString a,
SkString b 
)
inlinestatic

Definition at line 289 of file SkString.h.

289 {
290 a.swap(b);
291}
static bool b
struct MyStruct a[10]

Variable Documentation

◆ kSkStrAppendS32_MaxSize

constexpr int kSkStrAppendS32_MaxSize = kSkStrAppendU32_MaxSize + 1
staticconstexpr

Definition at line 89 of file SkString.h.

◆ kSkStrAppendS64_MaxSize

constexpr int kSkStrAppendS64_MaxSize = kSkStrAppendU64_MaxSize + 1
staticconstexpr

Definition at line 91 of file SkString.h.

◆ kSkStrAppendScalar_MaxSize

constexpr int kSkStrAppendScalar_MaxSize = 15
staticconstexpr

Floats have at most 8 significant digits, so we limit our g to that. However, the total string could be 15 characters: -1.2345678e-005

In theory we should only expect up to 2 digits for the exponent, but on some platforms we have seen 3 (as in the example above).

Definition at line 101 of file SkString.h.

◆ kSkStrAppendU32_MaxSize

constexpr int kSkStrAppendU32_MaxSize = 10
staticconstexpr

Definition at line 84 of file SkString.h.

◆ kSkStrAppendU64_MaxSize

constexpr int kSkStrAppendU64_MaxSize = 20
staticconstexpr

Definition at line 86 of file SkString.h.