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