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#include <string.h>
14
15#include <functional>
16#include <iosfwd>
17#include <iterator>
18#include <utility>
19
20#include "core/fxcrt/fx_string_wrappers.h"
21#include "core/fxcrt/retain_ptr.h"
22#include "core/fxcrt/string_data_template.h"
23#include "core/fxcrt/string_view_template.h"
24#include "third_party/abseil-cpp/absl/types/optional.h"
25#include "third_party/base/check.h"
26#include "third_party/base/containers/span.h"
27
28namespace fxcrt {
29
30// A mutable string with shared buffers using copy-on-write semantics that
31// avoids the cost of std::string's iterator stability guarantees.
33 public:
34 using CharType = char;
35 using const_iterator = const CharType*;
36 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
37
38 [[nodiscard]] static ByteString FormatInteger(int i);
39 [[nodiscard]] static ByteString FormatFloat(float f);
40 [[nodiscard]] static ByteString Format(const char* pFormat, ...);
41 [[nodiscard]] static ByteString FormatV(const char* pFormat, va_list argList);
42
44 ByteString(const ByteString& other);
45
46 // Move-construct a ByteString. After construction, |other| is empty.
47 ByteString(ByteString&& other) noexcept;
48
49 // Make a one-character string from a char.
50 explicit ByteString(char ch);
51
52 // Deliberately implicit to avoid calling on every string literal.
53 // NOLINTNEXTLINE(runtime/explicit)
54 ByteString(const char* ptr);
55
56 // No implicit conversions from wide strings.
57 // NOLINTNEXTLINE(runtime/explicit)
58 ByteString(wchar_t) = delete;
59
60 ByteString(const char* pStr, size_t len);
61 ByteString(const uint8_t* pStr, size_t len);
62
63 explicit ByteString(ByteStringView bstrc);
64 ByteString(ByteStringView str1, ByteStringView str2);
65 ByteString(const std::initializer_list<ByteStringView>& list);
66 explicit ByteString(const fxcrt::ostringstream& outStream);
67
69
70 // Holds on to buffer if possible for later re-use. Assign ByteString()
71 // to force immediate release if desired.
72 void clear();
73
74 // Explicit conversion to C-style string.
75 // Note: Any subsequent modification of |this| will invalidate the result.
76 const char* c_str() const { return m_pData ? m_pData->m_String : ""; }
77
78 // Explicit conversion to uint8_t*.
79 // Note: Any subsequent modification of |this| will invalidate the result.
80 const uint8_t* raw_str() const {
81 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String)
82 : nullptr;
83 }
84
85 // Explicit conversion to ByteStringView.
86 // Note: Any subsequent modification of |this| will invalidate the result.
87 ByteStringView AsStringView() const {
88 return ByteStringView(raw_str(), GetLength());
89 }
90
91 // Explicit conversion to span.
92 // Note: Any subsequent modification of |this| will invalidate the result.
93 pdfium::span<const char> span() const {
94 return pdfium::make_span(m_pData ? m_pData->m_String : nullptr,
95 GetLength());
96 }
97 pdfium::span<const uint8_t> raw_span() const {
98 return pdfium::make_span(raw_str(), GetLength());
99 }
100
101 // Note: Any subsequent modification of |this| will invalidate iterators.
102 const_iterator begin() const { return m_pData ? m_pData->m_String : nullptr; }
103 const_iterator end() const {
104 return m_pData ? m_pData->m_String + m_pData->m_nDataLength : nullptr;
105 }
106
107 // Note: Any subsequent modification of |this| will invalidate iterators.
108 const_reverse_iterator rbegin() const {
109 return const_reverse_iterator(end());
110 }
111 const_reverse_iterator rend() const {
112 return const_reverse_iterator(begin());
113 }
114
115 size_t GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; }
117 return m_pData ? strlen(m_pData->m_String) : 0;
118 }
119 bool IsEmpty() const { return !GetLength(); }
120 bool IsValidIndex(size_t index) const { return index < GetLength(); }
121 bool IsValidLength(size_t length) const { return length <= GetLength(); }
122
123 int Compare(ByteStringView str) const;
124 bool EqualNoCase(ByteStringView str) const;
125
126 bool operator==(const char* ptr) const;
127 bool operator==(ByteStringView str) const;
128 bool operator==(const ByteString& other) const;
129
130 bool operator!=(const char* ptr) const { return !(*this == ptr); }
131 bool operator!=(ByteStringView str) const { return !(*this == str); }
132 bool operator!=(const ByteString& other) const { return !(*this == other); }
133
134 bool operator<(const char* ptr) const;
135 bool operator<(ByteStringView str) const;
136 bool operator<(const ByteString& other) const;
137
138 ByteString& operator=(const char* str);
139 ByteString& operator=(ByteStringView str);
140 ByteString& operator=(const ByteString& that);
141
142 // Move-assign a ByteString. After assignment, |that| is empty.
143 ByteString& operator=(ByteString&& that) noexcept;
144
145 ByteString& operator+=(char ch);
146 ByteString& operator+=(const char* str);
147 ByteString& operator+=(const ByteString& str);
148 ByteString& operator+=(ByteStringView str);
149
150 CharType operator[](const size_t index) const {
151 CHECK(IsValidIndex(index));
152 return m_pData->m_String[index];
153 }
154
155 CharType Front() const { return GetLength() ? (*this)[0] : 0; }
156 CharType Back() const { return GetLength() ? (*this)[GetLength() - 1] : 0; }
157
158 void SetAt(size_t index, char c);
159
160 size_t Insert(size_t index, char ch);
161 size_t InsertAtFront(char ch) { return Insert(0, ch); }
162 size_t InsertAtBack(char ch) { return Insert(GetLength(), ch); }
163 size_t Delete(size_t index, size_t count = 1);
164
165 void Reserve(size_t len);
166
167 // Increase the backing store of the string so that it is capable of storing
168 // at least `nMinBufLength` chars. Returns a span to the entire buffer,
169 // which may be larger than `nMinBufLength` due to rounding by allocators.
170 // Note: any modification of the string (including ReleaseBuffer()) may
171 // invalidate the span, which must not outlive its buffer.
172 pdfium::span<char> GetBuffer(size_t nMinBufLength);
173
174 // Sets the size of the string to `nNewLength` chars. Call this after a call
175 // to GetBuffer(), to indicate how much of the buffer was actually used.
176 void ReleaseBuffer(size_t nNewLength);
177
178 ByteString Substr(size_t offset) const;
179 ByteString Substr(size_t first, size_t count) const;
180 ByteString First(size_t count) const;
181 ByteString Last(size_t count) const;
182
183 absl::optional<size_t> Find(ByteStringView subStr, size_t start = 0) const;
184 absl::optional<size_t> Find(char ch, size_t start = 0) const;
185 absl::optional<size_t> ReverseFind(char ch) const;
186
187 bool Contains(ByteStringView lpszSub, size_t start = 0) const {
188 return Find(lpszSub, start).has_value();
189 }
190
191 bool Contains(char ch, size_t start = 0) const {
192 return Find(ch, start).has_value();
193 }
194
195 void MakeLower();
196 void MakeUpper();
197
198 void Trim();
199 void Trim(char target);
200 void Trim(ByteStringView targets);
201
202 void TrimLeft();
203 void TrimLeft(char target);
204 void TrimLeft(ByteStringView targets);
205
206 void TrimRight();
207 void TrimRight(char target);
208 void TrimRight(ByteStringView targets);
209
210 size_t Replace(ByteStringView pOld, ByteStringView pNew);
211 size_t Remove(char ch);
212
213 uint32_t GetID() const { return AsStringView().GetID(); }
214
215 protected:
216 using StringData = StringDataTemplate<char>;
217
218 void ReallocBeforeWrite(size_t nNewLen);
219 void AllocBeforeWrite(size_t nNewLen);
220 void AllocCopy(ByteString& dest, size_t nCopyLen, size_t nCopyIndex) const;
221 void AssignCopy(const char* pSrcData, size_t nSrcLen);
222 void Concat(const char* pSrcData, size_t nSrcLen);
223 intptr_t ReferenceCountForTesting() const;
224
225 RetainPtr<StringData> m_pData;
226
227 friend class ByteString_Assign_Test;
228 friend class ByteString_Concat_Test;
229 friend class ByteString_Construct_Test;
230 friend class StringPool_ByteString_Test;
231};
232
233inline bool operator==(const char* lhs, const ByteString& rhs) {
234 return rhs == lhs;
235}
236inline bool operator==(ByteStringView lhs, const ByteString& rhs) {
237 return rhs == lhs;
238}
239inline bool operator!=(const char* lhs, const ByteString& rhs) {
240 return rhs != lhs;
241}
242inline bool operator!=(ByteStringView lhs, const ByteString& rhs) {
243 return rhs != lhs;
244}
245inline bool operator<(const char* lhs, const ByteString& rhs) {
246 return rhs.Compare(lhs) > 0;
247}
248inline bool operator<(const ByteStringView& lhs, const ByteString& rhs) {
249 return rhs.Compare(lhs) > 0;
250}
251inline bool operator<(const ByteStringView& lhs, const char* rhs) {
252 return lhs < ByteStringView(rhs);
253}
254
255inline ByteString operator+(ByteStringView str1, ByteStringView str2) {
256 return ByteString(str1, str2);
257}
258inline ByteString operator+(ByteStringView str1, const char* str2) {
259 return ByteString(str1, str2);
260}
261inline ByteString operator+(const char* str1, ByteStringView str2) {
262 return ByteString(str1, str2);
263}
264inline ByteString operator+(ByteStringView str1, char ch) {
265 return ByteString(str1, ByteStringView(ch));
266}
267inline ByteString operator+(char ch, ByteStringView str2) {
268 return ByteString(ByteStringView(ch), str2);
269}
270inline ByteString operator+(const ByteString& str1, const ByteString& str2) {
271 return ByteString(str1.AsStringView(), str2.AsStringView());
272}
273inline ByteString operator+(const ByteString& str1, char ch) {
274 return ByteString(str1.AsStringView(), ByteStringView(ch));
275}
276inline ByteString operator+(char ch, const ByteString& str2) {
277 return ByteString(ByteStringView(ch), str2.AsStringView());
278}
279inline ByteString operator+(const ByteString& str1, const char* str2) {
280 return ByteString(str1.AsStringView(), str2);
281}
282inline ByteString operator+(const char* str1, const ByteString& str2) {
283 return ByteString(str1, str2.AsStringView());
284}
285inline ByteString operator+(const ByteString& str1, ByteStringView str2) {
286 return ByteString(str1.AsStringView(), str2);
287}
288inline ByteString operator+(ByteStringView str1, const ByteString& str2) {
289 return ByteString(str1, str2.AsStringView());
290}
291
292std::ostream& operator<<(std::ostream& os, const ByteString& str);
293std::ostream& operator<<(std::ostream& os, ByteStringView str);
294
295// This is declared here for use in gtest-based tests but is defined in a test
296// support target. This should not be used in production code. Just use
297// operator<< from above instead.
298// In some cases, gtest will automatically use operator<< as well, but in this
299// case, it needs PrintTo() because ByteString looks like a container to gtest.
300void PrintTo(const ByteString& str, std::ostream* os);
301
302} // namespace fxcrt
303
304using ByteString = fxcrt::ByteString;
305
306uint32_t FX_HashCode_GetA(ByteStringView str);
307uint32_t FX_HashCode_GetLoweredA(ByteStringView str);
308uint32_t FX_HashCode_GetAsIfW(ByteStringView str);
309uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str);
310
311namespace std {
312
313template <>
314struct hash<ByteString> {
315 size_t operator()(const ByteString& str) const {
316 return FX_HashCode_GetA(str.AsStringView());
317 }
318};
319
320} // namespace std
321
322extern template struct std::hash<ByteString>;
323
324#endif // CORE_FXCRT_BYTESTRING_H_
uint32_t FX_HashCode_GetLoweredAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetLoweredA(ByteStringView str)
uint32_t FX_HashCode_GetAsIfW(ByteStringView str)
uint32_t FX_HashCode_GetA(ByteStringView str)
CPDF_Creator(CPDF_Document *pDoc, RetainPtr< IFX_RetainableWriteStream > archive)
bool SetFileVersion(int32_t fileVersion)
void RemoveSecurity()
bool Create(uint32_t flags)
virtual FX_FILESIZE CurrentOffset() const =0
static RetainPtr< IFX_SeekableReadStream > CreateFromFilename(const char *filename)
Definition fx_stream.cpp:68
virtual size_t ReadBlock(pdfium::span< uint8_t > buffer)
Definition fx_stream.cpp:88
virtual FX_FILESIZE GetPosition()
Definition fx_stream.cpp:84
virtual bool ReadBlockAtOffset(pdfium::span< uint8_t > buffer, FX_FILESIZE offset)=0
virtual bool IsEOF()
Definition fx_stream.cpp:80
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:92
virtual bool WriteBlockAtOffset(pdfium::span< const uint8_t > data, FX_FILESIZE offset)=0
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
Definition fx_stream.cpp:76
virtual bool Flush()=0
virtual FX_FILESIZE GetSize()=0
bool WriteFilesize(FX_FILESIZE size)
Definition fx_stream.cpp:61
bool WriteByte(uint8_t byte)
Definition fx_stream.cpp:51
bool WriteString(ByteStringView str)
Definition fx_stream.cpp:47
virtual ~IFX_WriteStream()=default
bool WriteDWord(uint32_t i)
Definition fx_stream.cpp:55
virtual bool WriteBlock(pdfium::span< const uint8_t > data)=0
void TrimLeft(ByteStringView targets)
bool IsValidLength(size_t length) const
Definition bytestring.h:121
bool Contains(ByteStringView lpszSub, size_t start=0) const
Definition bytestring.h:187
const_iterator begin() const
Definition bytestring.h:102
bool IsValidIndex(size_t index) const
Definition bytestring.h:120
static ByteString FormatFloat(float f)
ByteStringView AsStringView() const
Definition bytestring.h:87
uint32_t GetID() const
Definition bytestring.h:213
void Reserve(size_t len)
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:131
size_t GetLength() const
Definition bytestring.h:115
ByteString Substr(size_t first, size_t count) const
size_t GetStringLength() const
Definition bytestring.h:116
size_t InsertAtBack(char ch)
Definition bytestring.h:162
bool operator==(ByteStringView str) const
pdfium::span< const uint8_t > raw_span() const
Definition bytestring.h:97
void AllocCopy(ByteString &dest, size_t nCopyLen, size_t nCopyIndex) const
void AssignCopy(const char *pSrcData, size_t nSrcLen)
bool Contains(char ch, size_t start=0) const
Definition bytestring.h:191
absl::optional< size_t > ReverseFind(char ch) const
intptr_t ReferenceCountForTesting() const
size_t Delete(size_t index, size_t count=1)
const_reverse_iterator rend() const
Definition bytestring.h:111
ByteString & operator+=(char ch)
const uint8_t * raw_str() const
Definition bytestring.h:80
void ReallocBeforeWrite(size_t nNewLen)
ByteString(ByteStringView bstrc)
RetainPtr< StringData > m_pData
Definition bytestring.h:225
static ByteString FormatInteger(int i)
pdfium::span< char > GetBuffer(size_t nMinBufLength)
void Trim(char target)
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
const_iterator end() const
Definition bytestring.h:103
ByteString(const ByteString &other)
size_t Remove(char ch)
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(const uint8_t *pStr, size_t len)
size_t Replace(ByteStringView pOld, ByteStringView pNew)
absl::optional< size_t > Find(ByteStringView subStr, size_t start=0) const
bool operator!=(const ByteString &other) const
Definition bytestring.h:132
void SetAt(size_t index, char c)
ByteString & operator=(ByteStringView str)
ByteString(ByteStringView str1, ByteStringView str2)
void Trim(ByteStringView targets)
ByteString & operator=(const char *str)
static ByteString FormatV(const char *pFormat, va_list argList)
void TrimRight(char target)
const char * c_str() const
Definition bytestring.h:76
ByteString Substr(size_t offset) const
ByteString & operator=(const ByteString &that)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
void TrimRight(ByteStringView targets)
bool operator<(const ByteString &other) const
ByteString(const std::initializer_list< ByteStringView > &list)
CharType operator[](const size_t index) const
Definition bytestring.h:150
const_reverse_iterator rbegin() const
Definition bytestring.h:108
CharType Back() const
Definition bytestring.h:156
CharType Front() const
Definition bytestring.h:155
bool operator!=(const char *ptr) const
Definition bytestring.h:130
int Compare(ByteStringView str) const
size_t Insert(size_t index, char ch)
void ReleaseBuffer(size_t nNewLength)
ByteString(const fxcrt::ostringstream &outStream)
void TrimLeft(char target)
size_t InsertAtFront(char ch)
Definition bytestring.h:161
ByteString First(size_t count) const
void AllocBeforeWrite(size_t nNewLen)
bool operator<(const char *ptr) const
void Concat(const char *pSrcData, size_t nSrcLen)
absl::optional< size_t > Find(char ch, size_t start=0) const
ByteString(ByteString &&other) noexcept
ByteString Last(size_t count) const
#define FPDFCREATE_INCREMENTAL
#define FPDFCREATE_NO_ORIGINAL
void * FX_Random_MT_Start(uint32_t dwSeed)
Definition fx_random.cpp:85
uint32_t FX_Random_MT_Generate(void *pContext)
Definition fx_random.cpp:96
void FX_Random_MT_Close(void *pContext)
#define FX_FILESIZE
Definition fx_types.h:19
bool operator==(const char *lhs, const ByteString &rhs)
Definition bytestring.h:233
bool operator<(const ByteStringView &lhs, const char *rhs)
Definition bytestring.h:251
ByteString operator+(const ByteString &str1, const ByteString &str2)
Definition bytestring.h:270
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:258
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:248
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:273
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:279
void PrintTo(const ByteString &str, std::ostream *os)
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:276
ByteString operator+(const ByteString &str1, ByteStringView str2)
Definition bytestring.h:285
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:282
bool operator!=(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:242
bool operator!=(const char *lhs, const ByteString &rhs)
Definition bytestring.h:239
ByteString operator+(ByteStringView str1, ByteStringView str2)
Definition bytestring.h:255
ByteString operator+(ByteStringView str1, char ch)
Definition bytestring.h:264
bool operator<(const char *lhs, const ByteString &rhs)
Definition bytestring.h:245
ByteString operator+(char ch, ByteStringView str2)
Definition bytestring.h:267
ByteString operator+(ByteStringView str1, const ByteString &str2)
Definition bytestring.h:288
ByteString operator+(const char *str1, ByteStringView str2)
Definition bytestring.h:261
bool operator==(ByteStringView lhs, const ByteString &rhs)
Definition bytestring.h:236
#define CHECK(cvref)
size_t operator()(const ByteString &str) const
Definition bytestring.h:315