7#include "core/fxcrt/fx_string.h"
13#include "build/build_config.h"
14#include "core/fxcrt/bytestring.h"
15#include "core/fxcrt/code_point_view.h"
16#include "core/fxcrt/fx_extension.h"
17#include "core/fxcrt/span_util.h"
18#include "core/fxcrt/string_view_template.h"
19#include "core/fxcrt/utf16.h"
20#include "core/fxcrt/widestring.h"
21#include "third_party/base/compiler_specific.h"
22#include "third_party/base/containers/span.h"
24#if !defined(WCHAR_T_IS_16_BIT) && !defined(WCHAR_T_IS_32_BIT)
25#error "Unknown wchar_t size"
27#if defined(WCHAR_T_IS_16_BIT) && defined(WCHAR_T_IS_32_BIT)
28#error "Conflicting wchar_t sizes"
36void AppendCodePointToByteString(
char32_t code_point, ByteString& buffer) {
42 if (code_point < 0x80) {
49 if (code_point < 0x800) {
51 }
else if (code_point < 0x10000) {
57 static constexpr uint8_t kPrefix[] = {0xc0, 0xe0, 0xf0};
58 int order = 1 << ((byte_size - 1) * 6);
59 buffer
+= kPrefix[byte_size - 2] | (code_point / order);
60 for (
int i = 0; i < byte_size - 1; i++) {
61 code_point = code_point % order;
63 buffer
+= 0x80 | (code_point / order);
71 for (
char32_t code_point : pdfium::CodePointView(wsStr)) {
72 AppendCodePointToByteString(code_point, buffer);
78 if (wsStr.IsEmpty()) {
82 std::u16string result;
83 result.reserve(wsStr.GetLength());
85 for (
wchar_t c : wsStr) {
86#if defined(WCHAR_T_IS_32_BIT)
87 if (pdfium::IsSupplementary(c)) {
88 pdfium::SurrogatePair pair(c);
89 result.push_back(pair.high());
90 result.push_back(pair.low());
102constexpr float kFractionScalesFloat[] = {
103 0.1f, 0.01f, 0.001f, 0.0001f,
104 0.00001f, 0.000001f, 0.0000001f, 0.00000001f,
105 0.000000001f, 0.0000000001f, 0.00000000001f};
107const double kFractionScalesDouble[] = {
108 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001,
109 0.0000001, 0.00000001, 0.000000001, 0.0000000001, 0.00000000001};
112T StringTo(ByteStringView strc, pdfium::span<
const T> fractional_scales) {
116 bool bNegative =
false;
118 size_t len = strc.GetLength();
119 if (strc[0] ==
'+') {
121 }
else if (strc[0] ==
'-') {
126 if (strc[cc] !=
'+' && strc[cc] !=
'-')
134 value = value * 10 + FXSYS_DecimalCharToInt(strc.CharAt(cc));
138 if (cc < len && strc[cc] ==
'.') {
142 fractional_scales[scale] * FXSYS_DecimalCharToInt(strc.CharAt(cc));
144 if (scale == fractional_scales.size())
149 return bNegative ? -value : value;
153size_t ToString(T value,
int (*round_func)(T), pdfium::span<
char> buf) {
159 bool bNegative =
false;
165 int scaled = round_func(value);
166 while (scaled < 100000) {
167 if (scale == 1000000) {
171 scaled = round_func(value * scale);
179 buf[buf_size++] =
'-';
181 int i = scaled / scale;
183 size_t len = strlen(buf2);
184 fxcrt::spancpy(buf.subspan(buf_size), pdfium::make_span(buf2).first(len));
186 int fraction = scaled % scale;
190 buf[buf_size++] =
'.';
193 buf[buf_size++] =
'0' + fraction / scale;
203 return StringTo<
float>(strc, kFractionScalesFloat);
207 return StringToFloat(FX_UTF8Encode(wsStr).AsStringView());
215 return StringTo<
double>(strc, kFractionScalesDouble);
219 return StringToDouble(FX_UTF8Encode(wsStr).AsStringView());
ByteString & operator+=(char ch)
size_t FloatToString(float f, pdfium::span< char > buf)
double StringToDouble(WideStringView wsStr)
float StringToFloat(ByteStringView str)
ByteString FX_UTF8Encode(WideStringView wsStr)
std::u16string FX_UTF16Encode(WideStringView wsStr)
size_t DoubleToString(double d, pdfium::span< char > buf)
float StringToFloat(WideStringView wsStr)
double StringToDouble(ByteStringView str)
int FXSYS_roundf(float f)
int FXSYS_round(double d)
char * FXSYS_itoa(int value, char *str, int radix)
constexpr char32_t kMaximumSupplementaryCodePoint