Flutter Engine
The Flutter Engine
string_utils.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "string_utils.h"
6
7#include <array>
8#include <cctype>
9#include <codecvt>
10#include <locale>
11#include <regex>
12#include <sstream>
13
14#include "flutter/fml/string_conversion.h"
15#include "third_party/dart/third_party/double-conversion/src/double-conversion.h"
16
17#include "no_destructor.h"
18
19namespace base {
20
23
24namespace {
25constexpr char kExponentChar = 'e';
26constexpr char kInfinitySymbol[] = "Infinity";
27constexpr char kNaNSymbol[] = "NaN";
28
29// The number of digits after the decimal we allow before switching to
30// exponential representation.
31constexpr int kDecimalInShortestLow = -6;
32// The number of digits before the decimal we allow before switching to
33// exponential representation.
34constexpr int kDecimalInShortestHigh = 12;
35constexpr int kConversionFlags =
37
38const DoubleToStringConverter& GetDoubleToStringConverter() {
39 static DoubleToStringConverter converter(
40 kConversionFlags, kInfinitySymbol, kNaNSymbol, kExponentChar,
41 kDecimalInShortestLow, kDecimalInShortestHigh, 0, 0);
42 return converter;
43}
44
45std::string NumberToStringImpl(double number, bool is_single_precision) {
46 if (number == 0.0) {
47 return "0";
48 }
49
50 constexpr int kBufferSize = 128;
51 std::array<char, kBufferSize> char_buffer;
52 StringBuilder builder(char_buffer.data(), char_buffer.size());
53 if (is_single_precision) {
54 GetDoubleToStringConverter().ToShortestSingle(static_cast<float>(number),
55 &builder);
56 } else {
57 GetDoubleToStringConverter().ToShortest(number, &builder);
58 }
59 return std::string(char_buffer.data(), builder.position());
60}
61} // namespace
62
63std::u16string ASCIIToUTF16(std::string src) {
64 return std::u16string(src.begin(), src.end());
65}
66
67std::u16string UTF8ToUTF16(std::string src) {
68 return fml::Utf8ToUtf16(src);
69}
70
71std::string UTF16ToUTF8(std::u16string src) {
72 return fml::Utf16ToUtf8(src);
73}
74
75std::u16string NumberToString16(float number) {
76 return ASCIIToUTF16(NumberToString(number));
77}
78
79std::u16string NumberToString16(int32_t number) {
80 return ASCIIToUTF16(NumberToString(number));
81}
82
83std::u16string NumberToString16(unsigned int number) {
84 return ASCIIToUTF16(NumberToString(number));
85}
86
87std::u16string NumberToString16(double number) {
88 return ASCIIToUTF16(NumberToString(number));
89}
90
91std::string NumberToString(int32_t number) {
92 return std::to_string(number);
93}
94
95std::string NumberToString(unsigned int number) {
96 return std::to_string(number);
97}
98
99std::string NumberToString(float number) {
100 return NumberToStringImpl(number, true);
101}
102
103std::string NumberToString(double number) {
104 return NumberToStringImpl(number, false);
105}
106
107std::string JoinString(std::vector<std::string> tokens, std::string delimiter) {
108 std::ostringstream imploded;
109 for (size_t i = 0; i < tokens.size(); i++) {
110 if (i == tokens.size() - 1) {
111 imploded << tokens[i];
112 } else {
113 imploded << tokens[i] << delimiter;
114 }
115 }
116 return imploded.str();
117}
118
119std::u16string JoinString(std::vector<std::u16string> tokens,
120 std::u16string delimiter) {
121 std::u16string result;
122 for (size_t i = 0; i < tokens.size(); i++) {
123 if (i == tokens.size() - 1) {
124 result.append(tokens[i]);
125 } else {
126 result.append(tokens[i]);
127 result.append(delimiter);
128 }
129 }
130 return result;
131}
132
133void ReplaceChars(std::string in,
134 std::string from,
135 std::string to,
136 std::string* out) {
137 size_t pos = in.find(from);
138 while (pos != std::string::npos) {
139 in.replace(pos, from.size(), to);
140 pos = in.find(from, pos + to.size());
141 }
142 *out = in;
143}
144
145void ReplaceChars(std::u16string in,
146 std::u16string from,
147 std::u16string to,
148 std::u16string* out) {
149 size_t pos = in.find(from);
150 while (pos != std::u16string::npos) {
151 in.replace(pos, from.size(), to);
152 pos = in.find(from, pos + to.size());
153 }
154 *out = in;
155}
156
157const std::string& EmptyString() {
159 return *s;
160}
161
162std::string ToUpperASCII(std::string str) {
163 std::string ret;
164 ret.reserve(str.size());
165 for (size_t i = 0; i < str.size(); i++)
166 ret.push_back(std::toupper(str[i]));
167 return ret;
168}
169
170std::string ToLowerASCII(std::string str) {
171 std::string ret;
172 ret.reserve(str.size());
173 for (size_t i = 0; i < str.size(); i++)
174 ret.push_back(std::tolower(str[i]));
175 return ret;
176}
177
178bool LowerCaseEqualsASCII(std::string a, std::string b) {
179 std::string lower_a = ToLowerASCII(a);
180 return lower_a.compare(ToLowerASCII(b)) == 0;
181}
182
183bool ContainsOnlyChars(std::u16string str, char16_t ch) {
184 return std::find_if(str.begin(), str.end(),
185 [ch](char16_t c) { return c != ch; }) == str.end();
186}
187
188} // namespace base
SkPoint pos
static const size_t kBufferSize
Definition: SkString.cpp:27
static bool b
struct MyStruct s
struct MyStruct a[10]
GAsyncResult * result
std::string ToUpperASCII(std::string str)
std::string UTF16ToUTF8(std::u16string src)
Definition: string_utils.cc:71
void ReplaceChars(std::string in, std::string from, std::string to, std::string *out)
const std::string & EmptyString()
std::u16string UTF8ToUTF16(std::string src)
Definition: string_utils.cc:67
bool ContainsOnlyChars(std::u16string str, char16_t ch)
std::u16string ASCIIToUTF16(std::string src)
Definition: string_utils.cc:63
std::string ToLowerASCII(std::string str)
std::string JoinString(std::vector< std::string > tokens, std::string delimiter)
bool LowerCaseEqualsASCII(std::string a, std::string b)
std::string NumberToString(int32_t number)
Definition: string_utils.cc:91
std::u16string NumberToString16(float number)
Definition: string_utils.cc:75
string converter
Definition: cacheimages.py:19
static constexpr const char * kInfinitySymbol
static constexpr const char * kNaNSymbol
static constexpr char kExponentChar
std::string Utf16ToUtf8(const std::u16string_view string)
std::u16string Utf8ToUtf16(const std::string_view string)
static SkString to_string(int n)
Definition: nanobench.cpp:119