From 4064812605d903d2817201848f47139d70c4b1f2 Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Tue, 4 Apr 2017 14:04:14 +0300 Subject: [PATCH] Bug 1353324 - Add const char16_t variant of MakeCStringSpan() and rename both to MakeStringSpan(). r=froydnj. MozReview-Commit-ID: E6LEZpe5H4w --HG-- extra : rebase_source : dd6fe66be289e94751ecdf34113d79a091c9c8f8 --- mfbt/Span.h | 31 ++++++++++++++++++++++++------- mfbt/tests/gtest/TestSpan.cpp | 11 ++++++++++- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/mfbt/Span.h b/mfbt/Span.h index 5acaafcbd73c..13e6f5bf10c3 100644 --- a/mfbt/Span.h +++ b/mfbt/Span.h @@ -87,6 +87,14 @@ class Span; // implementation details namespace span_details { +inline size_t strlen16(const char16_t* aZeroTerminated) { + size_t len = 0; + while (*(aZeroTerminated++)) { + len++; + } + return len; +} + // C++14 types that we don't have because we build as C++11. template using remove_cv_t = typename mozilla::RemoveCV::Type; @@ -391,11 +399,11 @@ private: * (via constructor or MakeSpan()) from a pointer and a length or a pointer * and another pointer pointing just past the last element. * - * A Span can be obtained for const char* pointing to a - * zero-terminated C string using the MakeCStringSpan() function. A - * corresponding implicit constructor does not exist in order to avoid - * accidental construction in cases where const char* does not point to a - * zero-terminated C string. + * A Span or Span can be obtained for const char* + * or const char16_t pointing to a zero-terminated string using the + * MakeStringSpan() function. Corresponding implicit constructor does not exist + * in order to avoid accidental construction in cases where const char* or + * const char16_t* do not point to a zero-terminated string. * * Span has methods that follow the Mozilla naming style and methods that * don't. The methods that follow the Mozilla naming style are meant to be @@ -1015,9 +1023,18 @@ MakeSpan(Ptr& aPtr, size_t aLength) * Create span from C string. */ inline Span -MakeCStringSpan(const char* aStr) +MakeStringSpan(const char* aZeroTerminated) { - return Span(aStr, std::strlen(aStr)); + return Span(aZeroTerminated, std::strlen(aZeroTerminated)); +} + +/** + * Create span from UTF-16 C string. + */ +inline Span +MakeStringSpan(const char16_t* aZeroTerminated) +{ + return Span(aZeroTerminated, span_details::strlen16(aZeroTerminated)); } } // namespace mozilla diff --git a/mfbt/tests/gtest/TestSpan.cpp b/mfbt/tests/gtest/TestSpan.cpp index f3aa000a438e..e2f01784bf59 100644 --- a/mfbt/tests/gtest/TestSpan.cpp +++ b/mfbt/tests/gtest/TestSpan.cpp @@ -1177,7 +1177,16 @@ SPAN_TEST(from_cstring) { const char* str = "abc"; - auto cs = MakeCStringSpan(str); + auto cs = MakeStringSpan(str); + ASSERT_EQ(cs.size(), 3U); + ASSERT_EQ(cs.data(), str); + ASSERT_EQ(cs[2], 'c'); + } + { + char16_t arr[4] = {'a', 'b', 'c', 0}; + const char16_t* str = arr; + + auto cs = MakeStringSpan(str); ASSERT_EQ(cs.size(), 3U); ASSERT_EQ(cs.data(), str); ASSERT_EQ(cs[2], 'c');