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
string_template.h
Go to the documentation of this file.
1// Copyright 2024 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_STRING_TEMPLATE_H_
8#define CORE_FXCRT_STRING_TEMPLATE_H_
9
10#include <stddef.h>
11
12#include <type_traits>
13
14#include "core/fxcrt/compiler_specific.h"
15#include "core/fxcrt/retain_ptr.h"
16#include "core/fxcrt/span.h"
17#include "core/fxcrt/string_data_template.h"
18#include "core/fxcrt/string_view_template.h"
19
20namespace fxcrt {
21
22inline constexpr const char* EmptyString(char*) {
23 return "";
24}
25inline constexpr const wchar_t* EmptyString(wchar_t*) {
26 return L"";
27}
28
29// Base class for a mutable string with shared buffers using copy-on-write
30// semantics that avoids std::string's iterator stability guarantees.
31template <typename T>
33 public:
34 using CharType = T;
35 using UnsignedType = typename std::make_unsigned<CharType>::type;
37 using const_iterator = T*;
38 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
39
40 bool IsEmpty() const { return !GetLength(); }
41 size_t GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; }
42
43 // Return length as determined by the position of the first NUL.
45 return m_pData ? m_pData->GetStringLength() : 0;
46 }
47
48 // Explicit conversion to UnsignedType*. May return nullptr.
49 // Note: Any subsequent modification of |this| will invalidate the result.
50 const UnsignedType* unsigned_str() const {
51 return m_pData ? reinterpret_cast<const UnsignedType*>(m_pData->m_String)
52 : nullptr;
53 }
54
55 // Explicit conversion to StringView.
56 // Note: Any subsequent modification of |this| will invalidate the result.
57 StringView AsStringView() const { return StringView(unsigned_span()); }
58
59 // Explicit conversion to C-style string. The result is never nullptr,
60 // and is always NUL terminated.
61 // Note: Any subsequent modification of |this| will invalidate the result.
62 const CharType* c_str() const {
63 return m_pData ? m_pData->m_String
64 : EmptyString(static_cast<CharType*>(nullptr));
65 }
66
67 // Explicit conversion to span.
68 // Note: Any subsequent modification of |this| will invalidate the result.
69 pdfium::span<const CharType> span() const {
70 return m_pData ? m_pData->span() : pdfium::span<const CharType>();
71 }
72
73 // Explicit conversion to spans of unsigned types.
74 // Note: Any subsequent modification of |this| will invalidate the result.
76 return reinterpret_span<const UnsignedType>(span());
77 }
78
79 // Explicit conversion to spans including the NUL terminator. The result is
80 // never an empty span. Only a const-form is provided to preclude modifying
81 // the terminator. Usage should be rare and carefully considered.
82 // Note: Any subsequent modification of |this| will invalidate the result.
84 // SAFETY: EmptyString() returns one NUL byte.
85 return m_pData ? m_pData->span_with_terminator()
86 : UNSAFE_BUFFERS(pdfium::make_span(
87 EmptyString(static_cast<CharType*>(nullptr)), 1u));
88 }
89
90 // Explicit conversion to spans of unsigned types including the NUL
91 // terminator. The result is never an empty span. Only a const-form is
92 // provided to preclude modifying the terminator. Usage should be rare
93 // and carefully considered.
94 // Note: Any subsequent modification of |this| will invalidate the result.
96 return reinterpret_span<const UnsignedType>(span_with_terminator());
97 }
98
99 // Note: Any subsequent modification of |this| will invalidate iterators.
101 return m_pData ? m_pData->span().begin() : nullptr;
102 }
104 return m_pData ? m_pData->span().end() : nullptr;
105 }
106
107 // Note: Any subsequent modification of |this| will invalidate iterators.
114
115 bool IsValidIndex(size_t index) const { return index < GetLength(); }
116 bool IsValidLength(size_t length) const { return length <= GetLength(); }
117
118 // CHECK() if index is out of range (via span's operator[]).
119 CharType operator[](const size_t index) const {
120 CHECK(m_pData);
121 return m_pData->span()[index];
122 }
123
124 // Unlike std::wstring::front(), this is always safe and returns a
125 // NUL char when the string is empty.
126 CharType Front() const { return m_pData ? m_pData->Front() : 0; }
127
128 // Unlike std::wstring::back(), this is always safe and returns a
129 // NUL char when the string is empty.
130 CharType Back() const { return m_pData ? m_pData->Back() : 0; }
131
132 // Holds on to buffer if possible for later re-use. Use assignment
133 // to force immediate release if desired.
134 void clear();
135
136 // Increase the backing store of the string so that it is capable of storing
137 // at least `nMinBufLength` chars. Returns a span to the entire buffer,
138 // which may be larger than `nMinBufLength` due to rounding by allocators.
139 // Note: any modification of the string (including ReleaseBuffer()) may
140 // invalidate the span, which must not outlive its buffer.
141 pdfium::span<T> GetBuffer(size_t nMinBufLength);
142
143 // Sets the size of the string to `nNewLength` chars. Call this after a call
144 // to GetBuffer(), to indicate how much of the buffer was actually used.
145 void ReleaseBuffer(size_t nNewLength);
146
147 // Returns size of string following insertion.
148 size_t Insert(size_t index, T ch);
149 size_t InsertAtFront(T ch) { return Insert(0, ch); }
150 size_t InsertAtBack(T ch) { return Insert(GetLength(), ch); }
151
152 // Returns number of instances of `ch` removed.
153 size_t Remove(T ch);
154
155 // Returns size of the string following deletion.
156 size_t Delete(size_t index, size_t count = 1);
157
158 // Returns the index within the string when found.
159 std::optional<size_t> Find(StringView str, size_t start = 0) const;
160 std::optional<size_t> Find(T ch, size_t start = 0) const;
161 std::optional<size_t> ReverseFind(T ch) const;
162
163 bool Contains(StringView str, size_t start = 0) const {
164 return Find(str, start).has_value();
165 }
166 bool Contains(T ch, size_t start = 0) const {
167 return Find(ch, start).has_value();
168 }
169
170 // Replace all occurences of `oldstr' with `newstr'.
171 size_t Replace(StringView oldstr, StringView newstr);
172
173 // Overwrite character at `index`.
174 void SetAt(size_t index, T ch);
175
176 void Reserve(size_t len) { GetBuffer(len); }
177
178 // Remove character `ch` from both/front/back of string.
179 void Trim(T ch);
180 void TrimFront(T ch);
181 void TrimBack(T ch);
182
183 // Remove all characters in `targets` from both/front/back of string.
184 void Trim(StringView targets);
185 void TrimFront(StringView targets);
186 void TrimBack(StringView targets);
187
188 protected:
190
191 StringTemplate() = default;
192 StringTemplate(const StringTemplate& other) = default;
193
194 // Move-construct a StringTemplate. After construction, |other| is empty.
195 StringTemplate(StringTemplate&& other) noexcept = default;
196
197 ~StringTemplate() = default;
198
199 void ReallocBeforeWrite(size_t nNewLen);
200 void AllocBeforeWrite(size_t nNewLen);
201 void AssignCopy(const T* pSrcData, size_t nSrcLen);
202 void Concat(const T* pSrcData, size_t nSrcLen);
203
205};
206
207extern template class StringTemplate<char>;
208extern template class StringTemplate<wchar_t>;
209
210} // namespace fxcrt
211
212#endif // CORE_FXCRT_STRING_TEMPLATE_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
pdfium::span< T > GetBuffer(size_t nMinBufLength)
size_t Delete(size_t index, size_t count=1)
const_iterator begin() const
void AllocBeforeWrite(size_t nNewLen)
pdfium::span< const UnsignedType > unsigned_span_with_terminator() const
size_t Replace(StringView oldstr, StringView newstr)
StringTemplate(const StringTemplate &other)=default
pdfium::span< const CharType > span_with_terminator() const
StringView AsStringView() const
void ReleaseBuffer(size_t nNewLength)
void Reserve(size_t len)
CharType operator[](const size_t index) const
CharType Front() const
pdfium::span< const UnsignedType > unsigned_span() const
StringDataTemplate< T > StringData
std::optional< size_t > ReverseFind(T ch) const
StringViewTemplate< T > StringView
void AssignCopy(const T *pSrcData, size_t nSrcLen)
RetainPtr< StringData > m_pData
std::optional< size_t > Find(StringView str, size_t start=0) const
void SetAt(size_t index, T ch)
const_reverse_iterator rbegin() const
const_iterator end() const
bool IsValidIndex(size_t index) const
bool Contains(T ch, size_t start=0) const
typename std::make_unsigned< CharType >::type UnsignedType
void TrimBack(StringView targets)
void Trim(StringView targets)
StringTemplate(StringTemplate &&other) noexcept=default
std::reverse_iterator< const_iterator > const_reverse_iterator
bool Contains(StringView str, size_t start=0) const
void Concat(const T *pSrcData, size_t nSrcLen)
CharType Back() const
std::optional< size_t > Find(T ch, size_t start=0) const
size_t GetLength() const
pdfium::span< const CharType > span() const
const UnsignedType * unsigned_str() const
bool IsValidLength(size_t length) const
size_t GetStringLength() const
void ReallocBeforeWrite(size_t nNewLen)
void TrimFront(StringView targets)
size_t Insert(size_t index, T ch)
const_reverse_iterator rend() const
const CharType * c_str() const
#define UNSAFE_BUFFERS(...)
#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
constexpr const wchar_t * EmptyString(wchar_t *)
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
constexpr const char * EmptyString(char *)
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
#define CHECK(cvref)
fxcrt::ByteStringView ByteStringView
size_t operator()(const ByteString &str) const
Definition bytestring.h:191