Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Macros | Functions | Variables
SkString.cpp File Reference
#include "include/core/SkString.h"
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkFloatingPoint.h"
#include "include/private/base/SkMalloc.h"
#include "include/private/base/SkTPin.h"
#include "include/private/base/SkTo.h"
#include "src/base/SkSafeMath.h"
#include "src/base/SkUTF.h"
#include "src/base/SkUtils.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <new>
#include <string_view>
#include <utility>

Go to the source code of this file.

Classes

struct  StringBuffer
 

Macros

#define SizeOfRec()   (gEmptyRec.data() - (const char*)&gEmptyRec)
 

Functions

template<int SIZE>
static StringBuffer apply_format_string (const char *format, va_list args, char(&stackBuffer)[SIZE], SkString *heapBuffer) SK_PRINTF_LIKE(1
 
template<int SIZE>
static StringBuffer static StringBuffer apply_format_string (const char *format, va_list args, char(&stackBuffer)[SIZE], SkString *heapBuffer)
 
bool SkStrEndsWith (const char string[], const char suffixStr[])
 
bool SkStrEndsWith (const char string[], const char suffixChar)
 
int SkStrStartsWithOneOf (const char string[], const char prefixes[])
 
char * SkStrAppendU32 (char string[], uint32_t dec)
 
char * SkStrAppendS32 (char string[], int32_t dec)
 
char * SkStrAppendU64 (char string[], uint64_t dec, int minDigits)
 
char * SkStrAppendS64 (char string[], int64_t dec, int minDigits)
 
char * SkStrAppendScalar (char string[], SkScalar value)
 
static uint32_t trim_size_t_to_u32 (size_t value)
 
static size_t check_add32 (size_t base, size_t extra)
 
SkString SkStringPrintf (const char *format,...)
 

Variables

static const size_t kBufferSize = 1024
 

Macro Definition Documentation

◆ SizeOfRec

#define SizeOfRec ( )    (gEmptyRec.data() - (const char*)&gEmptyRec)

Definition at line 195 of file SkString.cpp.

Function Documentation

◆ apply_format_string() [1/2]

template<int SIZE>
static StringBuffer static StringBuffer apply_format_string ( const char *  format,
va_list  args,
char(&)  stackBuffer[SIZE],
SkString heapBuffer 
)
static

Definition at line 39 of file SkString.cpp.

40 {
41 // First, attempt to print directly to the stack buffer.
42 va_list argsCopy;
43 va_copy(argsCopy, args);
44 int outLength = std::vsnprintf(stackBuffer, SIZE, format, args);
45 if (outLength < 0) {
46 SkDebugf("SkString: vsnprintf reported error.");
47 va_end(argsCopy);
48 return {stackBuffer, 0};
49 }
50 if (outLength < SIZE) {
51 va_end(argsCopy);
52 return {stackBuffer, outLength};
53 }
54
55 // Our text was too long to fit on the stack! However, we now know how much space we need to
56 // format it. Format the string into our heap buffer. `set` automatically reserves an extra
57 // byte at the end of the buffer for a null terminator, so we don't need to add one here.
58 heapBuffer->set(nullptr, outLength);
59 char* heapBufferDest = heapBuffer->data();
60 SkDEBUGCODE(int checkLength =) std::vsnprintf(heapBufferDest, outLength + 1, format, argsCopy);
61 SkASSERT(checkLength == outLength);
62 va_end(argsCopy);
63 return {heapBufferDest, outLength};
64}
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
const char * data() const
Definition SkString.h:132
void set(const SkString &src)
Definition SkString.h:186
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
va_end(args)
Definition ref_ptr.h:256
#define SIZE

◆ apply_format_string() [2/2]

template<int SIZE>
static StringBuffer apply_format_string ( const char *  format,
va_list  args,
char(&)  stackBuffer[SIZE],
SkString heapBuffer 
)
static

◆ check_add32()

static size_t check_add32 ( size_t  base,
size_t  extra 
)
static

Definition at line 206 of file SkString.cpp.

206 {
207 SkASSERT(base <= UINT32_MAX);
208 if (sizeof(size_t) > sizeof(uint32_t)) {
209 if (base + extra > UINT32_MAX) {
210 extra = UINT32_MAX - base;
211 }
212 }
213 return extra;
214}

◆ SkStrAppendS32()

char * SkStrAppendS32 ( char  string[],
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  string[],
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}
static bool SkIsFinite(T x, Pack... values)
static bool SkIsNaN(T x)
static constexpr int kSkStrAppendScalar_MaxSize
Definition SkString.h:101
static const uint8_t buffer[]

◆ SkStrAppendU32()

char * SkStrAppendU32 ( char  string[],
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
constexpr uint8_t SkToU8(S x)
Definition SkTo.h:22

◆ SkStrAppendU64()

char * SkStrAppendU64 ( char  string[],
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

◆ 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}

◆ SkStringPrintf()

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

Definition at line 629 of file SkString.cpp.

629 {
630 SkString formattedOutput;
631 va_list args;
633 formattedOutput.printVAList(format, args);
634 va_end(args);
635 return formattedOutput;
636}
void void printVAList(const char format[], va_list) SK_PRINTF_LIKE(2
Definition SkString.cpp:541
va_start(args, format)

◆ 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}

◆ trim_size_t_to_u32()

static uint32_t trim_size_t_to_u32 ( size_t  value)
static

Definition at line 197 of file SkString.cpp.

197 {
198 if (sizeof(size_t) > sizeof(uint32_t)) {
199 if (value > UINT32_MAX) {
200 value = UINT32_MAX;
201 }
202 }
203 return (uint32_t)value;
204}
uint8_t value

Variable Documentation

◆ kBufferSize

const size_t kBufferSize = 1024
static

Definition at line 27 of file SkString.cpp.