1 // Copyright 2014 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 #ifndef CORE_FXCRT_FX_STRING_H_
8 #define CORE_FXCRT_FX_STRING_H_
9
10 #include <stdint.h>
11
12 #include <vector>
13
14 #include "core/fxcrt/bytestring.h"
15 #include "core/fxcrt/widestring.h"
16 #include "third_party/base/span.h"
17
FXBSTR_ID(uint8_t c1,uint8_t c2,uint8_t c3,uint8_t c4)18 constexpr uint32_t FXBSTR_ID(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4) {
19 return static_cast<uint32_t>(c1) << 24 | static_cast<uint32_t>(c2) << 16 |
20 static_cast<uint32_t>(c3) << 8 | static_cast<uint32_t>(c4);
21 }
22
23 ByteString FX_UTF8Encode(WideStringView wsStr);
24 WideString FX_UTF8Decode(ByteStringView bsStr);
25
26 float StringToFloat(ByteStringView str);
27 float StringToFloat(WideStringView wsStr);
28 size_t FloatToString(float f, pdfium::span<char> buf);
29
30 double StringToDouble(ByteStringView str);
31 double StringToDouble(WideStringView wsStr);
32 size_t DoubleToString(double d, pdfium::span<char> buf);
33
34 namespace fxcrt {
35
36 template <typename StrType>
Split(const StrType & that,typename StrType::CharType ch)37 std::vector<StrType> Split(const StrType& that, typename StrType::CharType ch) {
38 std::vector<StrType> result;
39 StringViewTemplate<typename StrType::CharType> remaining(that.span());
40 while (true) {
41 absl::optional<size_t> index = remaining.Find(ch);
42 if (!index.has_value())
43 break;
44 result.emplace_back(remaining.First(index.value()));
45 remaining = remaining.Last(remaining.GetLength() - index.value() - 1);
46 }
47 result.emplace_back(remaining);
48 return result;
49 }
50
51 extern template std::vector<ByteString> Split<ByteString>(
52 const ByteString& that,
53 ByteString::CharType ch);
54 extern template std::vector<WideString> Split<WideString>(
55 const WideString& that,
56 WideString::CharType ch);
57
58 } // namespace fxcrt
59
60 #endif // CORE_FXCRT_FX_STRING_H_
61