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
span_util.h
Go to the documentation of this file.
1// Copyright 2021 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#ifndef CORE_FXCRT_SPAN_UTIL_H_
6#define CORE_FXCRT_SPAN_UTIL_H_
7
8#include <stdint.h>
9
10#include <optional>
11#include <type_traits>
12
13#include "core/fxcrt/check_op.h"
14#include "core/fxcrt/fx_memcpy_wrappers.h"
15#include "core/fxcrt/span.h"
16
17namespace fxcrt {
18
19// Bounds-checked byte-for-byte copies from spans into spans. Returns a
20// span describing the remaining portion of the destination span.
21template <typename T1,
22 typename T2,
23 size_t N1,
24 size_t N2,
25 typename P1,
26 typename P2,
27 typename = std::enable_if_t<sizeof(T1) == sizeof(T2) &&
30inline pdfium::span<T1> spancpy(pdfium::span<T1, N1, P1> dst,
31 pdfium::span<T2, N2, P2> src) {
32 CHECK_GE(dst.size(), src.size());
33 // SAFETY: SFINAE ensures `sizeof(T1)` equals `sizeof(T2)`, so comparing
34 // `size()` for equality ensures `size_bytes()` are equal, and `size_bytes()`
35 // accurately describes `data()`.
36 UNSAFE_BUFFERS(FXSYS_memcpy(dst.data(), src.data(), src.size_bytes()));
37 return dst.subspan(src.size());
38}
39
40// Bounds-checked byte-for-byte moves from spans into spans. Returns a
41// span describing the remaining portion of the destination span.
42template <typename T1,
43 typename T2,
44 size_t N1,
45 size_t N2,
46 typename P1,
47 typename P2,
48 typename = std::enable_if_t<sizeof(T1) == sizeof(T2) &&
51inline pdfium::span<T1> spanmove(pdfium::span<T1, N1, P1> dst,
52 pdfium::span<T2, N2, P2> src) {
53 CHECK_GE(dst.size(), src.size());
54 // SAFETY: SFINAE ensures `sizeof(T1)` equals `sizeof(T2)`, so comparing
55 // `size()` for equality ensures `size_bytes()` are equal, and `size_bytes()`
56 // accurately describes `data()`.
57 UNSAFE_BUFFERS(FXSYS_memmove(dst.data(), src.data(), src.size_bytes()));
58 return dst.subspan(src.size());
59}
60
61// Bounds-checked byte-for-byte copies from spans into spans. Performs the
62// copy if there is room, and returns true. Otherwise does not copy anything
63// and returns false.
64template <typename T1,
65 typename T2,
66 size_t N1,
67 size_t N2,
68 typename P1,
69 typename P2,
70 typename = std::enable_if_t<sizeof(T1) == sizeof(T2) &&
73inline bool try_spancpy(pdfium::span<T1, N1, P1> dst,
74 pdfium::span<T2, N2, P2> src) {
75 if (dst.size() < src.size()) {
76 return false;
77 }
78 // SAFETY: SFINAE ensures `sizeof(T1)` equals `sizeof(T2)`, the test above
79 // ensures `src.size()` <= `dst.size()` which implies `src.size_bytes()`
80 // <= `dst.size_bytes()`, and `dst.size_bytes()` describes `dst.data()`.
81 UNSAFE_BUFFERS(FXSYS_memcpy(dst.data(), src.data(), src.size_bytes()));
82 return true;
83}
84
85// Bounds-checked byte-for-byte moves from spans into spans. Peforms the
86// move if there is room, and returns true. Otherwise does not move anything
87// and returns false.
88template <typename T1,
89 typename T2,
90 size_t N1,
91 size_t N2,
92 typename P1,
93 typename P2,
94 typename = std::enable_if_t<sizeof(T1) == sizeof(T2) &&
97inline bool try_spanmove(pdfium::span<T1, N1, P1> dst,
98 pdfium::span<T2, N2, P2> src) {
99 if (dst.size() < src.size()) {
100 return false;
101 }
102 // SAFETY: SFINAE ensures `sizeof(T1)` equals `sizeof(T2)`, the test above
103 // ensures `src.size()` <= `dst.size()` which implies `src.size_bytes()`
104 // <= `dst.size_bytes()`, and `dst.size_bytes()` describes `dst.data()`.
105 UNSAFE_BUFFERS(FXSYS_memmove(dst.data(), src.data(), src.size_bytes()));
106 return true;
107}
108
109// Bounds-checked byte-for-byte equality of same-sized spans. This is
110// helpful because span does not (yet) have an operator==().
111template <typename T1,
112 typename T2,
113 size_t N1,
114 size_t N2,
115 typename P1,
116 typename P2,
117 typename = std::enable_if_t<sizeof(T1) == sizeof(T2) &&
120bool span_equals(pdfium::span<T1, N1, P1> s1, pdfium::span<T2, N2, P2> s2) {
121 // SAFETY: For both `s1` and `s2`, there are `size_bytes()` valid bytes at
122 // the corresponding `data()`, and the sizes are the same.
123 return s1.size_bytes() == s2.size_bytes() &&
124 UNSAFE_BUFFERS(FXSYS_memcmp(s1.data(), s2.data(), s1.size_bytes())) ==
125 0;
126}
127
128// Returns the first position where `needle` occurs in `haystack`.
129template <typename T,
130 typename U,
131 typename = std::enable_if_t<sizeof(T) == sizeof(U) &&
134std::optional<size_t> spanpos(pdfium::span<T> haystack,
135 pdfium::span<U> needle) {
136 if (needle.empty() || needle.size() > haystack.size()) {
137 return std::nullopt;
138 }
139 // After this `end_pos`, not enough characters remain in `haystack` for
140 // a full match to occur.
141 size_t end_pos = haystack.size() - needle.size();
142 for (size_t haystack_pos = 0; haystack_pos <= end_pos; ++haystack_pos) {
143 auto candidate = haystack.subspan(haystack_pos, needle.size());
144 if (fxcrt::span_equals(candidate, needle)) {
145 return haystack_pos;
146 }
147 }
148 return std::nullopt;
149}
150
151template <typename T,
152 typename U,
153 typename = typename std::enable_if_t<std::is_const_v<T> ||
154 !std::is_const_v<U>>>
155inline pdfium::span<T> reinterpret_span(pdfium::span<U> s) noexcept {
156 CHECK(alignof(T) == alignof(U) ||
157 reinterpret_cast<uintptr_t>(s.data()) % alignof(T) == 0u);
158 // SAFETY: relies on correct conversion of size_bytes() result.
159 return UNSAFE_BUFFERS(pdfium::make_span(reinterpret_cast<T*>(s.data()),
160 s.size_bytes() / sizeof(T)));
161}
162
163} // namespace fxcrt
164
165#endif // CORE_FXCRT_SPAN_UTIL_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)
#define CHECK_GE(x, y)
Definition check_op.h:15
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
bool operator>(const StringViewTemplate &that) const
StringViewTemplate(const CharType *ptr) noexcept
bool operator<(const StringViewTemplate &that) const
bool IsValidLength(size_t length) const
bool operator!=(const CharType *ptr) const
typename std::make_unsigned< CharType >::type UnsignedType
StringViewTemplate TrimmedRight(CharType ch) const
pdfium::span< const UnsignedType > m_Span
bool EqualsASCII(const StringViewTemplate< char > &that) const
std::optional< size_t > Find(CharType ch) const
const CharType * unterminated_c_str() const
bool operator==(const CharType *ptr) const
bool Contains(CharType ch) const
bool EqualsASCIINoCase(const StringViewTemplate< char > &that) const
StringViewTemplate Substr(size_t first, size_t count) const
std::reverse_iterator< const_iterator > const_reverse_iterator
UNSAFE_BUFFER_USAGE constexpr StringViewTemplate(const CharType *ptr, size_t size) noexcept
bool operator==(const StringViewTemplate &other) const
const UnsignedType & operator[](const size_t index) const
StringViewTemplate Last(size_t count) const
const UnsignedType * unterminated_unsigned_str() const
CharType CharAt(const size_t index) const
bool operator!=(const StringViewTemplate &other) const
StringViewTemplate First(size_t count) const
constexpr StringViewTemplate(const pdfium::span< const CharType > &other) noexcept
constexpr StringViewTemplate(const StringViewTemplate &src) noexcept=default
const_iterator begin() const
StringViewTemplate & operator=(const StringViewTemplate &src)
const_reverse_iterator rend() const
StringViewTemplate & operator=(const CharType *src)
StringViewTemplate Substr(size_t offset) const
constexpr StringViewTemplate() noexcept=default
constexpr StringViewTemplate(const CharType &ch) noexcept
UNSAFE_BUFFER_USAGE constexpr StringViewTemplate(const UnsignedType *ptr, size_t size) noexcept
pdfium::span< const CharType > span() const
const_reverse_iterator rbegin() const
bool IsValidIndex(size_t index) const
constexpr StringViewTemplate(const pdfium::span< const UnsignedType > &other) noexcept
pdfium::span< const UnsignedType > unsigned_span() 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)
int32_t FXSYS_atoi(const char *str)
uint32_t FXSYS_atoui(const char *str)
int FXSYS_wcsicmp(const wchar_t *str1, const wchar_t *str2)
int32_t FXSYS_wtoi(const wchar_t *str)
int FXSYS_stricmp(const char *str1, const char *str2)
int64_t FXSYS_atoi64(const char *str)
char * FXSYS_strlwr(char *str)
wchar_t * FXSYS_wcsupr(wchar_t *str)
char * FXSYS_strupr(char *str)
wchar_t * FXSYS_wcslwr(wchar_t *str)
#define FXSYS_IsFloatZero(f)
Definition fx_system.h:36
uint32_t FXSYS_GetLastError()
const char * FXSYS_i64toa(int64_t value, char *str, int radix)
int FXSYS_roundf(float f)
int FXSYS_round(double d)
char * FXSYS_itoa(int value, char *str, int radix)
void FXSYS_SetLastError(uint32_t err)
float FXSYS_sqrt2(float a, float b)
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
bool span_equals(pdfium::span< T1, N1, P1 > s1, pdfium::span< T2, N2, P2 > s2)
Definition span_util.h:120
ByteString operator+(ByteStringView str1, const char *str2)
Definition bytestring.h:134
bool operator<(const ByteStringView &lhs, const ByteString &rhs)
Definition bytestring.h:124
StringViewTemplate< wchar_t > WideStringView
pdfium::span< T > reinterpret_span(pdfium::span< U > s) noexcept
Definition span_util.h:155
bool operator==(const T *lhs, const StringViewTemplate< T > &rhs)
bool operator<(const T *lhs, const StringViewTemplate< T > &rhs)
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:149
ByteString operator+(const ByteString &str1, const char *str2)
Definition bytestring.h:155
std::optional< size_t > spanpos(pdfium::span< T > haystack, pdfium::span< U > needle)
Definition span_util.h:134
void PrintTo(const ByteString &str, std::ostream *os)
bool operator!=(const T *lhs, const StringViewTemplate< T > &rhs)
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
pdfium::span< T1 > spanmove(pdfium::span< T1, N1, P1 > dst, pdfium::span< T2, N2, P2 > src)
Definition span_util.h:51
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
bool try_spanmove(pdfium::span< T1, N1, P1 > dst, pdfium::span< T2, N2, P2 > src)
Definition span_util.h:97
pdfium::span< T1 > spancpy(pdfium::span< T1, N1, P1 > dst, pdfium::span< T2, N2, P2 > src)
Definition span_util.h:30
bool try_spancpy(pdfium::span< T1, N1, P1 > dst, pdfium::span< T2, N2, P2 > src)
Definition span_util.h:73
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
fxcrt::WideStringView WideStringView
void operator()(T *ptr) const
Definition retain_ptr.h:23
size_t operator()(const ByteString &str) const
Definition bytestring.h:191