From c2fa384d666aea1b119736f33116ac09e870fda0 Mon Sep 17 00:00:00 2001 From: trav90 Date: Sat, 6 Oct 2018 17:29:50 -0500 Subject: Add mozilla::Span --- xpcom/glue/nsTArray.h | 84 +++++++++++++++++++++++++++++++++++++++++++++ xpcom/string/nsTSubstring.h | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) (limited to 'xpcom') diff --git a/xpcom/glue/nsTArray.h b/xpcom/glue/nsTArray.h index ca74a41f76..c86772a8e9 100644 --- a/xpcom/glue/nsTArray.h +++ b/xpcom/glue/nsTArray.h @@ -19,6 +19,7 @@ #include "mozilla/Move.h" #include "mozilla/ReverseIterator.h" #include "mozilla/TypeTraits.h" +#include "mozilla/Span.h" #include @@ -1112,6 +1113,18 @@ public: const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } const_reverse_iterator crend() const { return rend(); } + // Span integration + + operator mozilla::Span() + { + return mozilla::Span(Elements(), Length()); + } + + operator mozilla::Span() const + { + return mozilla::Span(Elements(), Length()); + } + // // Search methods // @@ -1336,6 +1349,16 @@ protected: return ReplaceElementsAt( aStart, aCount, aArray.Elements(), aArray.Length()); } + + template + elem_type* ReplaceElementsAt(index_type aStart, + size_type aCount, + mozilla::Span aSpan) + { + return ReplaceElementsAt( + aStart, aCount, aSpan.Elements(), aSpan.Length()); + } + public: template @@ -1347,6 +1370,15 @@ public: return ReplaceElementsAt(aStart, aCount, aArray); } + template + MOZ_MUST_USE elem_type* ReplaceElementsAt(index_type aStart, + size_type aCount, + mozilla::Span aSpan, + const mozilla::fallible_t&) + { + return ReplaceElementsAt(aStart, aCount, aSpan); + } + // A variation on the ReplaceElementsAt method defined above. protected: template @@ -1399,6 +1431,15 @@ protected: return ReplaceElementsAt( aIndex, 0, aArray.Elements(), aArray.Length()); } + + template + elem_type* InsertElementsAt(index_type aIndex, + mozilla::Span aSpan) + { + return ReplaceElementsAt( + aIndex, 0, aSpan.Elements(), aSpan.Length()); + } + public: template @@ -1425,6 +1466,14 @@ public: return InsertElementAt(aIndex); } + template + MOZ_MUST_USE elem_type* InsertElementsAt(index_type aIndex, + mozilla::Span aSpan, + const mozilla::fallible_t&) + { + return InsertElementsAt(aIndex, aSpan); + } + // Insert a new element, move constructing if possible. protected: template @@ -1526,6 +1575,13 @@ protected: template elem_type* AppendElements(const Item* aArray, size_type aArrayLen); + template + elem_type* AppendElements(mozilla::Span aSpan) + { + return AppendElements(aSpan.Elements(), + aSpan.Length()); + } + public: template @@ -1536,6 +1592,15 @@ public: return AppendElements(aArray, aArrayLen); } + template + /* MOZ_MUST_USE */ + elem_type* AppendElements(mozilla::Span aSpan, + const mozilla::fallible_t&) + { + return AppendElements(aSpan.Elements(), + aSpan.Length()); + } + // A variation on the AppendElements method defined above. protected: template @@ -2347,6 +2412,25 @@ struct nsTArray_CopyChooser> typedef nsTArray_CopyWithConstructors> Type; }; +// Span integration +namespace mozilla { + +template +Span +MakeSpan(nsTArray_Impl& aTArray) +{ + return aTArray; +} + +template +Span +MakeSpan(const nsTArray_Impl& aTArray) +{ + return aTArray; +} + +} // namespace mozilla + // Assert that AutoTArray doesn't have any extra padding inside. // // It's important that the data stored in this auto array takes up a multiple of diff --git a/xpcom/string/nsTSubstring.h b/xpcom/string/nsTSubstring.h index a08036b1f0..53b4fb9a82 100644 --- a/xpcom/string/nsTSubstring.h +++ b/xpcom/string/nsTSubstring.h @@ -7,6 +7,8 @@ #include "mozilla/Casting.h" #include "mozilla/MemoryReporting.h" +#include "mozilla/IntegerTypeTraits.h" +#include "mozilla/Span.h" #ifndef MOZILLA_INTERNAL_API #error Cannot use internal string classes without MOZILLA_INTERNAL_API defined. Use the frozen header nsStringAPI.h instead. @@ -798,6 +800,68 @@ public: } #endif + /** + * Span integration + */ + + operator mozilla::Span() + { + return mozilla::MakeSpan(BeginWriting(), Length()); + } + + operator mozilla::Span() const + { + return mozilla::MakeSpan(BeginReading(), Length()); + } + + void Append(mozilla::Span aSpan) + { + auto len = aSpan.Length(); + MOZ_RELEASE_ASSERT(len <= mozilla::MaxValue::value); + Append(aSpan.Elements(), len); + } + + MOZ_MUST_USE bool Append(mozilla::Span aSpan, + const fallible_t& aFallible) + { + auto len = aSpan.Length(); + if (len > mozilla::MaxValue::value) { + return false; + } + return Append(aSpan.Elements(), len, aFallible); + } + +#if !defined(CharT_is_PRUnichar) + operator mozilla::Span() + { + return mozilla::MakeSpan(reinterpret_cast(BeginWriting()), + Length()); + } + + operator mozilla::Span() const + { + return mozilla::MakeSpan(reinterpret_cast(BeginReading()), + Length()); + } + + void Append(mozilla::Span aSpan) + { + auto len = aSpan.Length(); + MOZ_RELEASE_ASSERT(len <= mozilla::MaxValue::value); + Append(reinterpret_cast(aSpan.Elements()), len); + } + + MOZ_MUST_USE bool Append(mozilla::Span aSpan, + const fallible_t& aFallible) + { + auto len = aSpan.Length(); + if (len > mozilla::MaxValue::value) { + return false; + } + return Append( + reinterpret_cast(aSpan.Elements()), len, aFallible); + } +#endif /** * string data is never null, but can be marked void. if true, the @@ -1184,3 +1248,22 @@ operator>(const nsTSubstring_CharT::base_string_type& aLhs, { return Compare(aLhs, aRhs) > 0; } + +/** + * Span integration + */ +namespace mozilla { + +inline Span +MakeSpan(nsTSubstring_CharT& aString) +{ + return aString; +} + +inline Span +MakeSpan(const nsTSubstring_CharT& aString) +{ + return aString; +} + +} // namespace mozilla -- cgit v1.2.3