summaryrefslogtreecommitdiff
path: root/core/fxcrt/string_view_template.h
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-04-09 18:14:24 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-04-09 18:14:24 +0000
commitd45f9980995af5c6e4c68e32df89d3cccb0231ef (patch)
tree28ff005d364f5287b5f133b59af5607bd72f9cee /core/fxcrt/string_view_template.h
parenteb9502ffdfb27e61ea587cbfbb03092ded2b5630 (diff)
downloadpdfium-d45f9980995af5c6e4c68e32df89d3cccb0231ef.tar.xz
Make StringViewTemplate be based on pdfium::span<>.
Change-Id: I13d3e462c86d035bbb992696aab523df5642533b Reviewed-on: https://pdfium-review.googlesource.com/29811 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcrt/string_view_template.h')
-rw-r--r--core/fxcrt/string_view_template.h142
1 files changed, 69 insertions, 73 deletions
diff --git a/core/fxcrt/string_view_template.h b/core/fxcrt/string_view_template.h
index 1e436d3978..05694cf244 100644
--- a/core/fxcrt/string_view_template.h
+++ b/core/fxcrt/string_view_template.h
@@ -16,6 +16,7 @@
#include "core/fxcrt/fx_system.h"
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/base/optional.h"
+#include "third_party/base/span.h"
#include "third_party/base/stl_util.h"
namespace fxcrt {
@@ -31,63 +32,63 @@ class StringViewTemplate {
using const_iterator = const CharType*;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
- StringViewTemplate() : m_Ptr(nullptr), m_Length(0) {}
+ constexpr StringViewTemplate() noexcept = default;
+ constexpr StringViewTemplate(const StringViewTemplate& src) noexcept =
+ default;
// Deliberately implicit to avoid calling on every string literal.
// NOLINTNEXTLINE(runtime/explicit)
- StringViewTemplate(const CharType* ptr)
- : m_Ptr(reinterpret_cast<const UnsignedType*>(ptr)),
- m_Length(ptr ? FXSYS_len(ptr) : 0) {}
+ StringViewTemplate(const CharType* ptr) noexcept
+ : m_Span(reinterpret_cast<const UnsignedType*>(ptr),
+ ptr ? FXSYS_len(ptr) : 0) {}
- StringViewTemplate(const CharType* ptr, size_t len)
- : m_Ptr(reinterpret_cast<const UnsignedType*>(ptr)), m_Length(len) {}
+ constexpr StringViewTemplate(const CharType* ptr, size_t len) noexcept
+ : m_Span(reinterpret_cast<const UnsignedType*>(ptr), len) {}
+
+ explicit constexpr StringViewTemplate(
+ const pdfium::span<CharType>& other) noexcept
+ : m_Span(reinterpret_cast<const UnsignedType*>(other.data()),
+ other.size()) {}
template <typename U = UnsignedType>
- StringViewTemplate(
+ constexpr StringViewTemplate(
const UnsignedType* ptr,
size_t size,
- typename std::enable_if<!std::is_same<U, CharType>::value>::type* = 0)
- : m_Ptr(ptr), m_Length(size) {}
+ typename std::enable_if<!std::is_same<U, CharType>::value>::type* =
+ 0) noexcept
+ : m_Span(ptr, size) {}
+
+ template <typename U = UnsignedType>
+ StringViewTemplate(
+ const pdfium::span<U> other,
+ typename std::enable_if<!std::is_same<U, CharType>::value>::type* =
+ 0) noexcept
+ : m_Span(other) {}
// Deliberately implicit to avoid calling on every string literal.
// |ch| must be an lvalue that outlives the StringViewTemplate.
// NOLINTNEXTLINE(runtime/explicit)
- StringViewTemplate(CharType& ch) {
- m_Ptr = reinterpret_cast<const UnsignedType*>(&ch);
- m_Length = 1;
- }
-
- StringViewTemplate(const StringViewTemplate& src) {
- m_Ptr = src.m_Ptr;
- m_Length = src.m_Length;
- }
+ constexpr StringViewTemplate(CharType& ch) noexcept
+ : m_Span(reinterpret_cast<const UnsignedType*>(&ch), 1) {}
// Any changes to |vec| invalidate the string.
- explicit StringViewTemplate(const std::vector<UnsignedType>& vec) {
- m_Length = vec.size();
- m_Ptr = m_Length ? vec.data() : nullptr;
- }
+ explicit StringViewTemplate(const std::vector<UnsignedType>& vec) noexcept
+ : m_Span(vec.size() ? vec.data() : nullptr, vec.size()) {}
StringViewTemplate& operator=(const CharType* src) {
- m_Ptr = reinterpret_cast<const UnsignedType*>(src);
- m_Length = src ? FXSYS_len(src) : 0;
+ m_Span = pdfium::span<const UnsignedType>(
+ reinterpret_cast<const UnsignedType*>(src), src ? FXSYS_len(src) : 0);
return *this;
}
- StringViewTemplate& operator=(const StringViewTemplate& src) {
- m_Ptr = src.m_Ptr;
- m_Length = src.m_Length;
- return *this;
- }
+ StringViewTemplate& operator=(const StringViewTemplate& src) = default;
const_iterator begin() const {
- return reinterpret_cast<const CharType*>(m_Ptr.Get());
+ return reinterpret_cast<const_iterator>(m_Span.begin());
}
const_iterator end() const {
- return m_Ptr ? reinterpret_cast<const CharType*>(m_Ptr.Get()) + m_Length
- : nullptr;
+ return reinterpret_cast<const_iterator>(m_Span.end());
}
-
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
@@ -95,16 +96,12 @@ class StringViewTemplate {
return const_reverse_iterator(begin());
}
- bool operator==(const CharType* ptr) const {
- return FXSYS_len(ptr) == m_Length &&
- FXSYS_cmp(ptr, reinterpret_cast<const CharType*>(m_Ptr.Get()),
- m_Length) == 0;
- }
bool operator==(const StringViewTemplate& other) const {
- return other.m_Length == m_Length &&
- FXSYS_cmp(reinterpret_cast<const CharType*>(other.m_Ptr.Get()),
- reinterpret_cast<const CharType*>(m_Ptr.Get()),
- m_Length) == 0;
+ return m_Span == other.m_Span;
+ }
+ bool operator==(const CharType* ptr) const {
+ StringViewTemplate other(ptr);
+ return *this == other;
}
bool operator!=(const CharType* ptr) const { return !(*this == ptr); }
bool operator!=(const StringViewTemplate& other) const {
@@ -112,54 +109,52 @@ class StringViewTemplate {
}
uint32_t GetID() const {
- if (m_Length == 0)
+ if (m_Span.size() == 0)
return 0;
uint32_t strid = 0;
- size_t size = std::min(static_cast<size_t>(4), m_Length);
+ size_t size = std::min(static_cast<size_t>(4), m_Span.size());
for (size_t i = 0; i < size; i++)
- strid = strid * 256 + m_Ptr.Get()[i];
+ strid = strid * 256 + m_Span[i];
return strid << ((4 - size) * 8);
}
- const UnsignedType* raw_str() const { return m_Ptr.Get(); }
+ pdfium::span<const UnsignedType> span() const { return m_Span; }
+ const UnsignedType* raw_str() const { return m_Span.data(); }
const CharType* unterminated_c_str() const {
- return reinterpret_cast<const CharType*>(m_Ptr.Get());
+ return reinterpret_cast<const CharType*>(m_Span.data());
}
- size_t GetLength() const { return m_Length; }
- bool IsEmpty() const { return m_Length == 0; }
- bool IsValidIndex(size_t index) const { return index < GetLength(); }
- bool IsValidLength(size_t length) const { return length <= GetLength(); }
+ size_t GetLength() const { return m_Span.size(); }
+ bool IsEmpty() const { return m_Span.empty(); }
+ bool IsValidIndex(size_t index) const { return index < m_Span.size(); }
+ bool IsValidLength(size_t length) const { return length <= m_Span.size(); }
const UnsignedType& operator[](const size_t index) const {
- ASSERT(IsValidIndex(index));
- return m_Ptr.Get()[index];
+ return m_Span[index];
}
- UnsignedType First() const { return GetLength() ? (*this)[0] : 0; }
-
+ UnsignedType First() const { return m_Span.size() ? m_Span[0] : 0; }
UnsignedType Last() const {
- return GetLength() ? (*this)[GetLength() - 1] : 0;
+ return m_Span.size() ? m_Span[m_Span.size() - 1] : 0;
}
const CharType CharAt(const size_t index) const {
- ASSERT(IsValidIndex(index));
- return static_cast<CharType>(m_Ptr.Get()[index]);
+ return static_cast<CharType>(m_Span[index]);
}
Optional<size_t> Find(CharType ch) const {
const auto* found = reinterpret_cast<const UnsignedType*>(FXSYS_chr(
- reinterpret_cast<const CharType*>(m_Ptr.Get()), ch, m_Length));
+ reinterpret_cast<const CharType*>(m_Span.data()), ch, m_Span.size()));
- return found ? Optional<size_t>(found - m_Ptr.Get()) : Optional<size_t>();
+ return found ? Optional<size_t>(found - m_Span.data()) : Optional<size_t>();
}
bool Contains(CharType ch) const { return Find(ch).has_value(); }
StringViewTemplate Mid(size_t first, size_t count) const {
- if (!m_Ptr.Get())
+ if (!m_Span.data())
return StringViewTemplate();
if (!IsValidIndex(first))
@@ -171,7 +166,7 @@ class StringViewTemplate {
if (!IsValidIndex(first + count - 1))
return StringViewTemplate();
- return StringViewTemplate(m_Ptr.Get() + first, count);
+ return StringViewTemplate(m_Span.data() + first, count);
}
StringViewTemplate Left(size_t count) const {
@@ -197,26 +192,27 @@ class StringViewTemplate {
if (pos == 0)
return StringViewTemplate();
- return StringViewTemplate(m_Ptr.Get(), pos);
+ return StringViewTemplate(m_Span.data(), pos);
}
bool operator<(const StringViewTemplate& that) const {
- int result = FXSYS_cmp(reinterpret_cast<const CharType*>(m_Ptr.Get()),
- reinterpret_cast<const CharType*>(that.m_Ptr.Get()),
- std::min(m_Length, that.m_Length));
- return result < 0 || (result == 0 && m_Length < that.m_Length);
+ int result =
+ FXSYS_cmp(reinterpret_cast<const CharType*>(m_Span.data()),
+ reinterpret_cast<const CharType*>(that.m_Span.data()),
+ std::min(m_Span.size(), that.m_Span.size()));
+ return result < 0 || (result == 0 && m_Span.size() < that.m_Span.size());
}
bool operator>(const StringViewTemplate& that) const {
- int result = FXSYS_cmp(reinterpret_cast<const CharType*>(m_Ptr.Get()),
- reinterpret_cast<const CharType*>(that.m_Ptr.Get()),
- std::min(m_Length, that.m_Length));
- return result > 0 || (result == 0 && m_Length > that.m_Length);
+ int result =
+ FXSYS_cmp(reinterpret_cast<const CharType*>(m_Span.data()),
+ reinterpret_cast<const CharType*>(that.m_Span.data()),
+ std::min(m_Span.size(), that.m_Span.size()));
+ return result > 0 || (result == 0 && m_Span.size() > that.m_Span.size());
}
protected:
- UnownedPtr<const UnsignedType> m_Ptr;
- size_t m_Length;
+ pdfium::span<const UnsignedType> m_Span;
private:
void* operator new(size_t) throw() { return nullptr; }