From 6e8b20ae7f53a1b957d8fa2aa1e0268ba10b9269 Mon Sep 17 00:00:00 2001 From: Ting-Yu Lin Date: Thu, 7 Oct 2021 23:01:41 +0000 Subject: [PATCH] Bug 1733876 Part 4 - Allow GetUnicharStringWidth() to support mozilla::Span substring argument. r=m_kato This is required by Part 5. Differential Revision: https://phabricator.services.mozilla.com/D127384 --- dom/serializers/nsPlainTextSerializer.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/dom/serializers/nsPlainTextSerializer.cpp b/dom/serializers/nsPlainTextSerializer.cpp index 5ec6bf27c71e..02d26dff1d15 100644 --- a/dom/serializers/nsPlainTextSerializer.cpp +++ b/dom/serializers/nsPlainTextSerializer.cpp @@ -28,6 +28,7 @@ #include "mozilla/dom/Element.h" #include "mozilla/dom/HTMLBRElement.h" #include "mozilla/dom/Text.h" +#include "mozilla/Span.h" #include "mozilla/Preferences.h" #include "mozilla/StaticPrefs_converter.h" #include "mozilla/BinarySearch.h" @@ -61,7 +62,7 @@ static const char16_t kSPACE = ' '; static int32_t HeaderLevel(const nsAtom* aTag); static int32_t GetUnicharWidth(char16_t ucs); -static int32_t GetUnicharStringWidth(const nsString& aString); +static int32_t GetUnicharStringWidth(Span aString); // Someday may want to make this non-const: static const uint32_t TagStackSize = 500; @@ -1880,18 +1881,12 @@ int32_t GetUnicharWidth(char16_t ucs) { (ucs >= 0xffe0 && ucs <= 0xffe6)); } -int32_t GetUnicharStringWidth(const nsString& aString) { - const char16_t* pwcs = aString.get(); - int32_t n = aString.Length(); - - int32_t w, width = 0; - - for (; *pwcs && n-- > 0; pwcs++) - if ((w = GetUnicharWidth(*pwcs)) < 0) - ++width; // Taking 1 as the width of non-printable character, for bug# - // 94475. - else - width += w; - +int32_t GetUnicharStringWidth(Span aString) { + int32_t width = 0; + for (char16_t c : aString) { + const int32_t w = GetUnicharWidth(c); + // Taking 1 as the width of non-printable character, for bug 94475. + width += (w < 0 ? 1 : w); + } return width; }