Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_string.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxcrt/fx_string.h"
8
9#include <stdint.h>
10
11#include <array>
12#include <string>
13#include <vector>
14
15#include "build/build_config.h"
16#include "core/fxcrt/bytestring.h"
17#include "core/fxcrt/code_point_view.h"
18#include "core/fxcrt/fx_extension.h"
19#include "core/fxcrt/span.h"
20#include "core/fxcrt/utf16.h"
21#include "core/fxcrt/widestring.h"
22
23#if !defined(WCHAR_T_IS_16_BIT) && !defined(WCHAR_T_IS_32_BIT)
24#error "Unknown wchar_t size"
25#endif
26#if defined(WCHAR_T_IS_16_BIT) && defined(WCHAR_T_IS_32_BIT)
27#error "Conflicting wchar_t sizes"
28#endif
29
30namespace {
31
32// Appends a Unicode code point to a `ByteString` using UTF-8.
33//
34// TODO(crbug.com/pdfium/2041): Migrate to `ByteString`.
35void AppendCodePointToByteString(char32_t code_point, ByteString& buffer) {
37 // Invalid code point above U+10FFFF.
38 return;
39 }
40
41 if (code_point < 0x80) {
42 // 7-bit code points are unchanged in UTF-8.
43 buffer += code_point;
44 return;
45 }
46
47 int byte_size;
48 if (code_point < 0x800) {
49 byte_size = 2;
50 } else if (code_point < 0x10000) {
51 byte_size = 3;
52 } else {
53 byte_size = 4;
54 }
55
56 static constexpr std::array<uint8_t, 3> kPrefix = {{0xc0, 0xe0, 0xf0}};
57 int order = 1 << ((byte_size - 1) * 6);
58 buffer += kPrefix[byte_size - 2] | (code_point / order);
59 for (int i = 0; i < byte_size - 1; i++) {
60 code_point = code_point % order;
61 order >>= 6;
62 buffer += 0x80 | (code_point / order);
63 }
64}
65
66} // namespace
67
69 ByteString buffer;
70 for (char32_t code_point : pdfium::CodePointView(wsStr)) {
71 AppendCodePointToByteString(code_point, buffer);
72 }
73 return buffer;
74}
75
77 if (wsStr.IsEmpty()) {
78 return {};
79 }
80
81 std::u16string result;
82 result.reserve(wsStr.GetLength());
83
84 for (wchar_t c : wsStr) {
85#if defined(WCHAR_T_IS_32_BIT)
86 if (pdfium::IsSupplementary(c)) {
87 pdfium::SurrogatePair pair(c);
88 result.push_back(pair.high());
89 result.push_back(pair.low());
90 continue;
91 }
92#endif // defined(WCHAR_T_IS_32_BIT)
93 result.push_back(c);
94 }
95
96 return result;
97}
98
99namespace {
100
101constexpr float kFractionScalesFloat[] = {
102 0.1f, 0.01f, 0.001f, 0.0001f,
103 0.00001f, 0.000001f, 0.0000001f, 0.00000001f,
104 0.000000001f, 0.0000000001f, 0.00000000001f};
105
106const double kFractionScalesDouble[] = {
107 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001,
108 0.0000001, 0.00000001, 0.000000001, 0.0000000001, 0.00000000001};
109
110template <class T>
111T StringTo(ByteStringView strc, pdfium::span<const T> fractional_scales) {
112 if (strc.IsEmpty())
113 return 0;
114
115 bool bNegative = false;
116 size_t cc = 0;
117 size_t len = strc.GetLength();
118 if (strc[0] == '+') {
119 cc++;
120 } else if (strc[0] == '-') {
121 bNegative = true;
122 cc++;
123 }
124 while (cc < len) {
125 if (strc[cc] != '+' && strc[cc] != '-')
126 break;
127 cc++;
128 }
129 T value = 0;
130 while (cc < len) {
131 if (strc[cc] == '.')
132 break;
133 value = value * 10 + FXSYS_DecimalCharToInt(strc.CharAt(cc));
134 cc++;
135 }
136 size_t scale = 0;
137 if (cc < len && strc[cc] == '.') {
138 cc++;
139 while (cc < len) {
140 value +=
141 fractional_scales[scale] * FXSYS_DecimalCharToInt(strc.CharAt(cc));
142 scale++;
143 if (scale == fractional_scales.size())
144 break;
145 cc++;
146 }
147 }
148 return bNegative ? -value : value;
149}
150
151} // namespace
152
154 return StringTo<float>(strc, kFractionScalesFloat);
155}
156
158 return StringToFloat(FX_UTF8Encode(wsStr).AsStringView());
159}
160
162 return StringTo<double>(strc, kFractionScalesDouble);
163}
164
166 return StringToDouble(FX_UTF8Encode(wsStr).AsStringView());
167}
168
169namespace fxcrt {
170
175
176} // namespace fxcrt
fxcrt::ByteString ByteString
Definition bytestring.h:180
ByteString & operator+=(char ch)
double StringToDouble(WideStringView wsStr)
float StringToFloat(ByteStringView str)
ByteString FX_UTF8Encode(WideStringView wsStr)
Definition fx_string.cpp:68
std::u16string FX_UTF16Encode(WideStringView wsStr)
Definition fx_string.cpp:76
float StringToFloat(WideStringView wsStr)
double StringToDouble(ByteStringView str)
constexpr char32_t kMaximumSupplementaryCodePoint
Definition utf16.h:22
fxcrt::ByteStringView ByteStringView
fxcrt::WideStringView WideStringView