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
bytestring.h
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#ifndef CORE_FXCRT_BYTESTRING_H_
8#define CORE_FXCRT_BYTESTRING_H_
9
10#include <stdarg.h>
11#include <stddef.h>
12#include <stdint.h>
13
14#include <iosfwd>
15#include <utility>
16
17#include "core/fxcrt/compiler_specific.h"
18#include "core/fxcrt/fx_string_wrappers.h"
19#include "core/fxcrt/string_template.h"
20
21namespace fxcrt {
22
23// A mutable string with shared buffers using copy-on-write semantics that
24// avoids the cost of std::string's iterator stability guarantees.
25class ByteString : public StringTemplate<char> {
26 public:
27 [[nodiscard]] static ByteString FormatInteger(int i);
28 [[nodiscard]] static ByteString Format(const char* pFormat, ...);
29 [[nodiscard]] static ByteString FormatV(const char* pFormat, va_list argList);
30
31 ByteString() = default;
32 ByteString(const ByteString& other) = default;
33
34 // Move-construct a ByteString. After construction, |other| is empty.
35 ByteString(ByteString&& other) noexcept = default;
36
37 ~ByteString() = default;
38
39 UNSAFE_BUFFER_USAGE ByteString(const char* pStr, size_t len);
40 UNSAFE_BUFFER_USAGE ByteString(const uint8_t* pStr, size_t len);
41
42 // Make a one-character string from a char.
43 explicit ByteString(char ch);
44
45 // Deliberately implicit to avoid calling on every string literal.
46 // NOLINTNEXTLINE(runtime/explicit)
47 ByteString(const char* ptr);
48
49 // No implicit conversions from wide strings.
50 // NOLINTNEXTLINE(runtime/explicit)
51 ByteString(wchar_t) = delete;
52
53 explicit ByteString(ByteStringView bstrc);
55 ByteString(const std::initializer_list<ByteStringView>& list);
56 explicit ByteString(const fxcrt::ostringstream& outStream);
57
58 int Compare(ByteStringView str) const;
59 bool EqualNoCase(ByteStringView str) const;
60
61 bool operator==(const char* ptr) const;
62 bool operator==(ByteStringView str) const;
63 bool operator==(const ByteString& other) const;
64
65 bool operator!=(const char* ptr) const { return !(*this == ptr); }
66 bool operator!=(ByteStringView str) const { return !(*this == str); }
67 bool operator!=(const ByteString& other) const { return !(*this == other); }
68
69 bool operator<(const char* ptr) const;
70 bool operator<(ByteStringView str) const;
71 bool operator<(const ByteString& other) const;
72
73 ByteString& operator=(const char* str);
75 ByteString& operator=(const ByteString& that);
76
77 // Move-assign a ByteString. After assignment, |that| is empty.
78 ByteString& operator=(ByteString&& that) noexcept;
79
80 ByteString& operator+=(char ch);
81 ByteString& operator+=(const char* str);
82 ByteString& operator+=(const ByteString& str);
84
85 ByteString Substr(size_t offset) const;
86 ByteString Substr(size_t first, size_t count) const;
87 ByteString First(size_t count) const;
88 ByteString Last(size_t count) const;
89
90 void MakeLower();
91 void MakeUpper();
92
93 // Remove a canonical set of characters from the string.
94 void TrimWhitespace();
95 void TrimWhitespaceBack();
97
98 uint32_t GetID() const { return AsStringView().GetID(); }
99
100 protected:
101 intptr_t ReferenceCountForTesting() const;
102
103 friend class ByteString_Assign_Test;
104 friend class ByteString_Concat_Test;
105 friend class ByteString_Construct_Test;
106 friend class StringPool_ByteString_Test;
107};
108
109inline bool operator==(const char* lhs, const ByteString& rhs) {
110 return rhs == lhs;
111}
112inline bool operator==(ByteStringView lhs, const ByteString& rhs) {
113 return rhs == lhs;
114}
115inline bool operator!=(const char* lhs, const ByteString& rhs) {
116 return rhs != lhs;
117}
118inline bool operator!=(ByteStringView lhs, const ByteString& rhs) {
119 return rhs != lhs;
120}
121inline bool operator<(const char* lhs, const ByteString& rhs) {
122 return rhs.Compare(lhs) > 0;
123}
124inline bool operator<(const ByteStringView& lhs, const ByteString& rhs) {
125 return rhs.Compare(lhs) > 0;
126}
127inline bool operator<(const ByteStringView& lhs, const char* rhs) {
128 return lhs < ByteStringView(rhs);
129}
130
132 return ByteString(str1, str2);
133}
134inline ByteString operator+(ByteStringView str1, const char* str2) {
135 return ByteString(str1, str2);
136}
137inline ByteString operator+(const char* str1, ByteStringView str2) {
138 return ByteString(str1, str2);
139}
140inline ByteString operator+(ByteStringView str1, char ch) {
141 return ByteString(str1, ByteStringView(ch));
142}
143inline ByteString operator+(char ch, ByteStringView str2) {
144 return ByteString(ByteStringView(ch), str2);
145}
146inline ByteString operator+(const ByteString& str1, const ByteString& str2) {
147 return ByteString(str1.AsStringView(), str2.AsStringView());
148}
149inline ByteString operator+(const ByteString& str1, char ch) {
150 return ByteString(str1.AsStringView(), ByteStringView(ch));
151}
152inline ByteString operator+(char ch, const ByteString& str2) {
153 return ByteString(ByteStringView(ch), str2.AsStringView());
154}
155inline ByteString operator+(const ByteString& str1, const char* str2) {
156 return ByteString(str1.AsStringView(), str2);
157}
158inline ByteString operator+(const char* str1, const ByteString& str2) {
159 return ByteString(str1, str2.AsStringView());
160}
161inline ByteString operator+(const ByteString& str1, ByteStringView str2) {
162 return ByteString(str1.AsStringView(), str2);
163}
164inline ByteString operator+(ByteStringView str1, const ByteString& str2) {
165 return ByteString(str1, str2.AsStringView());
166}
167
168std::ostream& operator<<(std::ostream& os, const ByteString& str);
169std::ostream& operator<<(std::ostream& os, ByteStringView str);
170
171// This is declared here for use in gtest-based tests but is defined in a test
172// support target. This should not be used in production code. Just use
173// operator<< from above instead.
174// In some cases, gtest will automatically use operator<< as well, but in this
175// case, it needs PrintTo() because ByteString looks like a container to gtest.
176void PrintTo(const ByteString& str, std::ostream* os);
177
178} // namespace fxcrt
179
181
182uint32_t FX_HashCode_GetA(ByteStringView str);
186
187namespace std {
188
189template <>
191 size_t operator()(const ByteString& str) const {
192 return FX_HashCode_GetA(str.AsStringView());
193 }
194};
195
196} // namespace std
197
198extern template struct std::hash<ByteString>;
199
200#endif // CORE_FXCRT_BYTESTRING_H_
uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str)
fxcrt::ByteString ByteString
Definition bytestring.h:180
uint32_t FX_HashCode_GetLoweredA(ByteStringView str)
uint32_t FX_HashCode_GetAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetA(ByteStringView str)
uint32_t GetID() const
Definition bytestring.h:98
ByteString(const char *ptr)
bool EqualNoCase(ByteStringView str) const
static ByteString Format(const char *pFormat,...)
ByteString & operator+=(const ByteString &str)
bool operator!=(ByteStringView str) const
Definition bytestring.h:66
ByteString Substr(size_t first, size_t count) const
bool operator==(ByteStringView str) const
ByteString()=default
intptr_t ReferenceCountForTesting() const
ByteString & operator+=(char ch)
ByteString(ByteStringView bstrc)
static ByteString FormatInteger(int i)
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
UNSAFE_BUFFER_USAGE ByteString(const char *pStr, size_t len)
bool operator<(ByteStringView str) const
ByteString(wchar_t)=delete
ByteString & operator+=(const char *str)
ByteString & operator+=(ByteStringView str)
~ByteString()=default
UNSAFE_BUFFER_USAGE ByteString(const uint8_t *pStr, size_t len)
bool operator!=(const ByteString &other) const
Definition bytestring.h:67
ByteString & operator=(ByteStringView str)
ByteString(ByteStringView str1, ByteStringView str2)
ByteString & operator=(const char *str)
static ByteString FormatV(const char *pFormat, va_list argList)
ByteString Substr(size_t offset) const
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
ByteString(ByteString &&other) noexcept=default
bool operator<(const ByteString &other) const
ByteString(const std::initializer_list< ByteStringView > &list)
bool operator!=(const char *ptr) const
Definition bytestring.h:65
int Compare(ByteStringView str) const
ByteString(const ByteString &other)=default
ByteString(const fxcrt::ostringstream &outStream)
ByteString First(size_t count) const
bool operator<(const char *ptr) const
ByteString Last(size_t count) const
#define UNSAFE_BUFFER_USAGE
CRYPT_md5_context CRYPT_MD5Start()
Definition fx_crypt.cpp:164
TEST(FXCRYPT, CryptToBase16)
TEST(FXCRYPT, MD5GenerateEmtpyData)
std::string CryptToBase16(const uint8_t *digest)
Definition hash.cpp:9
bool operator==(const char *lhs, const ByteString &rhs)
Definition bytestring.h:109
bool operator<(const ByteStringView &lhs, const char *rhs)
Definition bytestring.h:127
ByteString operator+(const ByteString &str1, const ByteString &str2)
Definition bytestring.h:146
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:134
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:124
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:149
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:155
void PrintTo(const ByteString &str, std::ostream *os)
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:152
ByteString operator+(const ByteString &str1, ByteStringView str2)
Definition bytestring.h:161
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:158
StringViewTemplate< char > ByteStringView
bool operator!=(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:118
bool operator!=(const char *lhs, const ByteString &rhs)
Definition bytestring.h:115
ByteString operator+(ByteStringView str1, ByteStringView str2)
Definition bytestring.h:131
ByteString operator+(ByteStringView str1, char ch)
Definition bytestring.h:140
bool operator<(const char *lhs, const ByteString &rhs)
Definition bytestring.h:121
ByteString operator+(char ch, ByteStringView str2)
Definition bytestring.h:143
ByteString operator+(ByteStringView str1, const ByteString &str2)
Definition bytestring.h:164
ByteString operator+(const char *str1, ByteStringView str2)
Definition bytestring.h:137
bool operator==(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:112
fxcrt::ByteStringView ByteStringView
size_t operator()(const ByteString &str) const
Definition bytestring.h:191