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_data_template.h
Go to the documentation of this file.
1// Copyright 2016 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_DATA_TEMPLATE_H_
8#define CORE_FXCRT_STRING_DATA_TEMPLATE_H_
9
10#include <stddef.h>
11#include <stdint.h>
12
13#include <string>
14
15#include "core/fxcrt/retain_ptr.h"
16#include "core/fxcrt/span.h"
17
18namespace fxcrt {
19
20template <typename CharType>
22 public:
23 static RetainPtr<StringDataTemplate> Create(size_t nLen);
24 static RetainPtr<StringDataTemplate> Create(pdfium::span<const CharType> str);
25
26 void Retain() { ++m_nRefs; }
27 void Release();
28
29 bool CanOperateInPlace(size_t nTotalLen) const {
30 return m_nRefs <= 1 && nTotalLen <= m_nAllocLength;
31 }
32
33 void CopyContents(const StringDataTemplate& other);
34 void CopyContents(pdfium::span<const CharType> str);
35 void CopyContentsAt(size_t offset, pdfium::span<const CharType> str);
36
38 // SAFETY: m_nDataLength is within m_String.
39 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nDataLength));
40 }
41 pdfium::span<const CharType> span() const {
42 // SAFETY: m_nDataLength is within m_String.
43 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nDataLength));
44 }
45
46 // Only a const-form is provided to preclude modifying the terminator.
48 // SAFETY: m_nDataLength is within m_String and there is always a
49 // terminator character following it.
50 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nDataLength + 1));
51 }
52
54 // SAFETY: m_nAllocLength is within m_String.
55 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nAllocLength));
56 }
57 pdfium::span<const CharType> alloc_span() const {
58 // SAFETY: m_nAllocLength is within m_String.
59 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nAllocLength));
60 }
61
62 // Includes the terminating NUL not included in lengths.
64 // SAFETY: m_nAllocLength + 1 is within m_String.
65 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nAllocLength + 1));
66 }
68 // SAFETY: m_nAllocLength + 1 is within m_String.
69 return UNSAFE_BUFFERS(pdfium::make_span(m_String, m_nAllocLength + 1));
70 }
71
72 // Return length as determined by the location of the first embedded NUL.
74 return std::char_traits<CharType>::length(m_String);
75 }
76
77 // Unlike std::string::front(), this is always safe and returns a
78 // NUL char when the string is empty.
79 CharType Front() const { return !span().empty() ? span().front() : 0; }
80
81 // Unlike std::string::back(), this is always safe and returns a
82 // NUL char when the string is empty.
83 CharType Back() const { return !span().empty() ? span().back() : 0; }
84
85 // To ensure ref counts do not overflow, consider the worst possible case:
86 // the entire address space contains nothing but pointers to this object.
87 // Since the count increments with each new pointer, the largest value is
88 // the number of pointers that can fit into the address space. The size of
89 // the address space itself is a good upper bound on it.
90 intptr_t m_nRefs = 0;
91
92 // These lengths are in terms of number of characters, not bytes, and do not
93 // include the terminating NUL character, but the underlying buffer is sized
94 // to be capable of holding it.
97
98 // Not really 1, variable size.
99 CharType m_String[1];
100
101 private:
102 StringDataTemplate(size_t dataLen, size_t allocLen);
103 ~StringDataTemplate() = delete;
104};
105
106extern template class StringDataTemplate<char>;
107extern template class StringDataTemplate<wchar_t>;
108
109} // namespace fxcrt
110
111using fxcrt::StringDataTemplate;
112
113#endif // CORE_FXCRT_STRING_DATA_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
U * AsRaw() const
Definition retain_ptr.h:106
RetainPtr & operator=(const RetainPtr< U > &that)
Definition retain_ptr.h:88
operator bool() const
Definition retain_ptr.h:147
void Unleak(T *ptr)
Definition retain_ptr.h:128
RetainPtr(std::nullptr_t ptr)
Definition retain_ptr.h:34
void Reset(T *obj=nullptr)
Definition retain_ptr.h:115
bool operator!=(const RetainPtr &that) const
Definition retain_ptr.h:131
void Swap(RetainPtr &that)
Definition retain_ptr.h:124
RetainPtr() noexcept=default
RetainPtr(RetainPtr &&that) noexcept
Definition retain_ptr.h:47
bool operator!=(const U &that) const
Definition retain_ptr.h:139
RetainPtr & operator=(const RetainPtr &that)
Definition retain_ptr.h:71
bool operator==(const RetainPtr &that) const
Definition retain_ptr.h:130
T & operator*() const
Definition retain_ptr.h:148
bool operator==(const U &that) const
Definition retain_ptr.h:134
RetainPtr & operator=(RetainPtr &&that) noexcept
Definition retain_ptr.h:79
T * operator->() const
Definition retain_ptr.h:149
RetainPtr & operator=(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:98
RetainPtr(const RetainPtr< U > &that)
Definition retain_ptr.h:53
T * Get() const noexcept
Definition retain_ptr.h:122
~RetainPtr()=default
bool operator<(const RetainPtr &that) const
Definition retain_ptr.h:143
RetainPtr(const RetainPtr &that) noexcept
Definition retain_ptr.h:43
operator T*() const noexcept
Definition retain_ptr.h:121
RetainPtr & operator=(std::nullptr_t) noexcept
Definition retain_ptr.h:64
RetainPtr(RetainPtr< U > &&that) noexcept
Definition retain_ptr.h:59
RetainPtr< U > As() const
Definition retain_ptr.h:111
RetainPtr(T *pObj) noexcept
Definition retain_ptr.h:36
bool HasOneRef() const
Definition retain_ptr.h:160
Retainable()=default
virtual ~Retainable()=default
pdfium::span< CharType > alloc_span()
pdfium::span< CharType > span()
pdfium::span< const CharType > span_with_terminator() const
pdfium::span< const CharType > span() const
pdfium::span< CharType > capacity_span()
void CopyContents(const StringDataTemplate &other)
static RetainPtr< StringDataTemplate > Create(size_t nLen)
void CopyContentsAt(size_t offset, pdfium::span< const CharType > str)
void CopyContents(pdfium::span< const CharType > str)
bool CanOperateInPlace(size_t nTotalLen) const
pdfium::span< const CharType > capacity_span() const
pdfium::span< const CharType > alloc_span() 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 TRIVIAL_ABI
#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
RetainPtr< T > MakeRetain(Args &&... args)
Definition retain_ptr.h:207
RetainPtr< T > WrapRetain(T *that)
Definition retain_ptr.h:214
#define CHECK(cvref)
fxcrt::ByteStringView ByteStringView
void operator()(T *ptr) const
Definition retain_ptr.h:23
size_t operator()(const ByteString &str) const
Definition bytestring.h:191