Bug 1772006 - Part 2: Support converting nsTStringRepr<T> to std::basic_string_view<T>, r=xpcom-reviewers,barret

This type was introduced in c++17, and can be used as a convenient standard
medium for passing around borrowed substring references. It can be implicitly
converted to from string literals and `const char_type*`, meaning that after
this change it can be used as a convenient catch-all type to replace seperate
overloads for `const self_type&`, `const char_type*` and `const
char_type(&)[N]`.

std::basic_string_view also provides standard implementations of some
algorithms which will be convenient for code cleanup in later parts of this
bug.

Differential Revision: https://phabricator.services.mozilla.com/D148297
This commit is contained in:
Nika Layzell 2022-07-30 00:12:47 +00:00
Родитель f84e572177
Коммит b41825fb93
6 изменённых файлов: 17 добавлений и 1 удалений

Просмотреть файл

@ -46,6 +46,7 @@ raw-lines = [
hide-types = [
".*char_traits",
".*incompatible_char_type",
".*string_view",
# https://github.com/rust-lang/rust-bindgen/issues/1503
"mozilla::StyleTimingFunction.*",
# https://github.com/rust-lang/rust-bindgen/issues/1559

Просмотреть файл

@ -43,6 +43,8 @@ class nsTDependentString : public nsTString<T> {
typedef typename base_string_type::const_char_iterator const_char_iterator;
typedef typename base_string_type::string_view string_view;
typedef typename base_string_type::index_type index_type;
typedef typename base_string_type::size_type size_type;

Просмотреть файл

@ -44,6 +44,8 @@ class nsTDependentSubstring : public nsTSubstring<T> {
typedef typename substring_type::const_char_iterator const_char_iterator;
typedef typename substring_type::string_view string_view;
typedef typename substring_type::index_type index_type;
typedef typename substring_type::size_type size_type;

Просмотреть файл

@ -54,6 +54,8 @@ class nsTString : public nsTSubstring<T> {
typedef typename substring_type::const_char_iterator const_char_iterator;
typedef typename substring_type::string_view string_view;
typedef typename substring_type::index_type index_type;
typedef typename substring_type::size_type size_type;

Просмотреть файл

@ -8,6 +8,7 @@
#define nsTStringRepr_h
#include <limits>
#include <string_view>
#include <type_traits> // std::enable_if
#include "mozilla/Char16.h"
@ -131,6 +132,8 @@ class nsTStringRepr {
typedef const char_type* const_char_iterator;
typedef std::basic_string_view<char_type> string_view;
typedef size_t index_type;
typedef size_t size_type;
@ -179,10 +182,14 @@ class nsTStringRepr {
#endif
// Returns pointer to string data (not necessarily null-terminated)
constexpr const typename raw_type<T, int>::type Data() const { return mData; }
constexpr typename raw_type<T, int>::type Data() const { return mData; }
constexpr size_type Length() const { return static_cast<size_type>(mLength); }
constexpr string_view View() const { return string_view(Data(), Length()); }
constexpr operator string_view() const { return View(); }
constexpr DataFlags GetDataFlags() const { return mDataFlags; }
constexpr bool IsEmpty() const { return mLength == 0; }

Просмотреть файл

@ -312,6 +312,8 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr<T> {
typedef typename base_string_type::const_char_iterator const_char_iterator;
typedef typename base_string_type::string_view string_view;
typedef typename base_string_type::index_type index_type;
typedef typename base_string_type::size_type size_type;