Avoid some two-arg {Byte,Wide}StringView constructors.
These will eventually be declared unsafe, but we can correct cases
where we already have a span or a container convertible to span.
Generally prefer span::first() to StringView::First(), since the
latter must be forgiving (and does needless branching). However,
there were some odd cases with make_span(array<>) that didn't
deduce properly and need investigation. Use First() for those.
Change-Id: I1833986d330d48ccc7667d144d7ac2d70973dbfa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119911
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 5647627..a714894 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -131,7 +131,7 @@
void CPDF_Font::AppendChar(ByteString* str, uint32_t charcode) const {
char buf[4];
int len = AppendChar(buf, charcode);
- *str += ByteStringView(buf, len);
+ *str += ByteStringView(pdfium::make_span(buf).first(len));
}
WideString CPDF_Font::UnicodeFromCharCode(uint32_t charcode) const {
diff --git a/core/fpdfapi/page/cpdf_streamparser.h b/core/fpdfapi/page/cpdf_streamparser.h
index c82e9e0..ad54060 100644
--- a/core/fpdfapi/page/cpdf_streamparser.h
+++ b/core/fpdfapi/page/cpdf_streamparser.h
@@ -34,7 +34,7 @@
ElementType ParseNextElement();
ByteStringView GetWord() const {
- return ByteStringView(m_WordBuffer.data(), m_WordSize);
+ return ByteStringView(m_WordBuffer).First(m_WordSize);
}
uint32_t GetPos() const { return m_Pos; }
void SetPos(uint32_t pos) { m_Pos = pos; }
diff --git a/core/fxcrt/fx_string_unittest.cpp b/core/fxcrt/fx_string_unittest.cpp
index b1227f7..d2cf265 100644
--- a/core/fxcrt/fx_string_unittest.cpp
+++ b/core/fxcrt/fx_string_unittest.cpp
@@ -440,7 +440,7 @@
TEST(fxstring, ByteStringViewSplitEfficiency) {
std::vector<char> commas(50000, ',');
- ByteStringView input(commas.data(), commas.size());
+ ByteStringView input(commas);
std::vector<ByteStringView> result;
result = fxcrt::Split(input, ',');
ASSERT_EQ(commas.size() + 1, result.size());
@@ -460,7 +460,7 @@
TEST(fxstring, WideStringViewSplitEfficiency) {
std::vector<wchar_t> commas(50000, L',');
- WideStringView input(commas.data(), commas.size());
+ WideStringView input(commas);
std::vector<WideStringView> result;
result = fxcrt::Split(input, ',');
ASSERT_EQ(commas.size() + 1, result.size());
diff --git a/core/fxcrt/string_template.h b/core/fxcrt/string_template.h
index 55e0dc7..ace34f6 100644
--- a/core/fxcrt/string_template.h
+++ b/core/fxcrt/string_template.h
@@ -54,9 +54,7 @@
// Explicit conversion to StringView.
// Note: Any subsequent modification of |this| will invalidate the result.
- StringView AsStringView() const {
- return StringView(unsigned_str(), GetLength());
- }
+ StringView AsStringView() const { return StringView(unsigned_span()); }
// Explicit conversion to C-style string. The result is never nullptr,
// and is always NUL terminated.
diff --git a/fxjs/xfa/cfxjse_formcalc_context.cpp b/fxjs/xfa/cfxjse_formcalc_context.cpp
index 3e0d4e9..d2b139f 100644
--- a/fxjs/xfa/cfxjse_formcalc_context.cpp
+++ b/fxjs/xfa/cfxjse_formcalc_context.cpp
@@ -736,7 +736,7 @@
WideString EncodeHTML(const ByteString& bsHTML) {
WideString wsHTML = WideString::FromUTF8(bsHTML.AsStringView());
- wchar_t encode_buffer[8];
+ std::array<wchar_t, 8> encode_buffer;
encode_buffer[0] = '&';
encode_buffer[1] = '#';
encode_buffer[2] = 'x';
@@ -754,7 +754,7 @@
encode_buffer[3] = kStrCode[iIndex];
encode_buffer[4] = kStrCode[ch - iIndex * 16];
encode_buffer[5] = ';';
- wsResultBuf << WideStringView(encode_buffer, 6);
+ wsResultBuf << WideStringView(encode_buffer).First(6);
} else if (ch < 65536) {
int32_t iBigByte = ch / 256;
int32_t iLittleByte = ch % 256;
@@ -763,7 +763,7 @@
encode_buffer[5] = kStrCode[iLittleByte / 16];
encode_buffer[6] = kStrCode[iLittleByte % 16];
encode_buffer[7] = ';';
- wsResultBuf << WideStringView(encode_buffer, 8);
+ wsResultBuf << WideStringView(encode_buffer).First(8);
} else {
// TODO(tsepez): Handle codepoint not in BMP.
}
@@ -774,7 +774,7 @@
WideString EncodeXML(const ByteString& bsXML) {
WideString wsXML = WideString::FromUTF8(bsXML.AsStringView());
WideTextBuffer wsResultBuf;
- wchar_t encode_buffer[8];
+ std::array<wchar_t, 8> encode_buffer;
encode_buffer[0] = '&';
encode_buffer[1] = '#';
encode_buffer[2] = 'x';
@@ -813,7 +813,7 @@
encode_buffer[3] = kStrCode[iIndex];
encode_buffer[4] = kStrCode[ch - iIndex * 16];
encode_buffer[5] = ';';
- wsResultBuf << WideStringView(encode_buffer, 6);
+ wsResultBuf << WideStringView(encode_buffer).First(6);
} else if (ch < 65536) {
int32_t iBigByte = ch / 256;
int32_t iLittleByte = ch % 256;
@@ -822,7 +822,7 @@
encode_buffer[5] = kStrCode[iLittleByte / 16];
encode_buffer[6] = kStrCode[iLittleByte % 16];
encode_buffer[7] = ';';
- wsResultBuf << WideStringView(encode_buffer, 8);
+ wsResultBuf << WideStringView(encode_buffer).First(8);
} else {
// TODO(tsepez): Handle codepoint not in BMP.
}
diff --git a/xfa/fde/cfde_texteditengine.cpp b/xfa/fde/cfde_texteditengine.cpp
index d44b3f8..c5be19f 100644
--- a/xfa/fde/cfde_texteditengine.cpp
+++ b/xfa/fde/cfde_texteditengine.cpp
@@ -285,9 +285,9 @@
if (validation_enabled_ || limit_horizontal_area_ || limit_vertical_area_) {
WideString str;
- if (gap_position_ > 0)
- str += WideStringView(content_.data(), gap_position_);
-
+ if (gap_position_ > 0) {
+ str += WideStringView(pdfium::make_span(content_).first(gap_position_));
+ }
str += text;
if (text_length_ - gap_position_ > 0) {
@@ -876,7 +876,7 @@
WideString CFDE_TextEditEngine::GetText() const {
WideString str;
if (gap_position_ > 0) {
- str += WideStringView(content_.data(), gap_position_);
+ str += WideStringView(pdfium::make_span(content_).first(gap_position_));
}
if (text_length_ - gap_position_ > 0) {
str += WideStringView(pdfium::make_span(content_).subspan(
diff --git a/xfa/fgas/crt/cfgas_stringformatter.cpp b/xfa/fgas/crt/cfgas_stringformatter.cpp
index 62e814c..ea0b6c3 100644
--- a/xfa/fgas/crt/cfgas_stringformatter.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter.cpp
@@ -440,13 +440,12 @@
WideString wsAM = pLocale->GetMeridiemName(true);
WideString wsPM = pLocale->GetMeridiemName(false);
if (*cc + wsAM.GetLength() <= spTime.size() &&
- UNSAFE_TODO(WideStringView(spTime.data() + *cc, wsAM.GetLength())) ==
- wsAM) {
+ WideStringView(spTime.subspan(*cc, wsAM.GetLength())) == wsAM) {
*cc += wsAM.GetLength();
bHasA = true;
} else if (*cc + wsPM.GetLength() <= spTime.size() &&
- UNSAFE_TODO(WideStringView(spTime.data() + *cc,
- wsPM.GetLength())) == wsPM) {
+ WideStringView(spTime.subspan(*cc, wsPM.GetLength())) ==
+ wsPM) {
*cc += wsPM.GetLength();
bHasA = true;
bPM = true;
@@ -953,8 +952,8 @@
if (m_spPattern[ccf] == '\'') {
size_t iCurChar = ccf;
GetLiteralText(m_spPattern, &ccf);
- wsPurgePattern += UNSAFE_TODO(
- WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1));
+ wsPurgePattern +=
+ WideStringView(m_spPattern.subspan(iCurChar, ccf - iCurChar + 1));
} else if (!bBrackOpen &&
!pdfium::Contains(kConstChars, m_spPattern[ccf])) {
WideString wsSearchCategory(m_spPattern[ccf]);
@@ -1004,8 +1003,8 @@
if (m_spPattern[ccf] == '\'') {
size_t iCurChar = ccf;
GetLiteralText(m_spPattern, &ccf);
- *wsPurgePattern += UNSAFE_TODO(
- WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1));
+ *wsPurgePattern +=
+ WideStringView(m_spPattern.subspan(iCurChar, ccf - iCurChar + 1));
} else if (!bBrackOpen &&
!pdfium::Contains(kConstChars, m_spPattern[ccf])) {
WideString wsCategory(m_spPattern[ccf]);
@@ -1598,8 +1597,8 @@
if (m_spPattern[ccf] == '\'') {
size_t iCurChar = ccf;
GetLiteralText(m_spPattern, &ccf);
- wsTempPattern += UNSAFE_TODO(
- WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1));
+ wsTempPattern +=
+ WideStringView(m_spPattern.subspan(iCurChar, ccf - iCurChar + 1));
} else if (!bBraceOpen && eDateTimeType != DateTimeType::kDateTime &&
!pdfium::Contains(kConstChars, m_spPattern[ccf])) {
WideString wsCategory(m_spPattern[ccf]);