Retire fxcrt::spanset() and fxcrt::spanclr(). Convert to fxcrt::Fill(), which is more in-line with what we expect to be the future C++ vocabulary used in chromium (i.e. ranges::fill). This has the nice property that we can fill various containers without first having to make a span from them. Change-Id: I23018969b8a47f2207adb8e40b6feb36b19ecd72 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/120772 Reviewed-by: Thomas Sepez <tsepez@google.com> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp index b33f04f..bca3800 100644 --- a/core/fpdfapi/page/cpdf_dib.cpp +++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -40,6 +40,7 @@ #include "core/fxcrt/fx_memcpy_wrappers.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/calculate_pitch.h" #include "core/fxge/dib/cfx_dibitmap.h" @@ -1190,7 +1191,7 @@ if (pSrcLine.empty()) { pdfium::span<uint8_t> result = !m_MaskBuf.empty() ? m_MaskBuf : m_LineBuf; - fxcrt::spanset(result, 0); + fxcrt::Fill(result, 0); return result; } if (m_bpc * m_nComponents == 1) { @@ -1275,7 +1276,7 @@ } }); } else { - fxcrt::spanset(pdfium::make_span(m_MaskBuf), 0xFF); + fxcrt::Fill(m_MaskBuf, 0xFF); } } if (m_pColorSpace) {
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 6bcf80a..4d0cb88 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -31,7 +31,7 @@ #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/numerics/safe_conversions.h" #include "core/fxcrt/span.h" -#include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxcrt/utf16.h" namespace { @@ -159,7 +159,7 @@ } if (ch == 'z') { - fxcrt::spanset(dest_span.first(4), 0); + fxcrt::Fill(dest_span.first(4), 0); dest_span = dest_span.subspan(4); state = 0; res = 0; @@ -286,7 +286,7 @@ if (buf_left < copy_len) { uint32_t delta = copy_len - buf_left; copy_len = buf_left; - fxcrt::spanclr(dest_span.subspan(dest_count + copy_len, delta)); + fxcrt::Fill(dest_span.subspan(dest_count + copy_len, delta), 0); } auto copy_span = src_span.subspan(i + 1, copy_len); fxcrt::spancpy(dest_span.subspan(dest_count), copy_span); @@ -295,7 +295,7 @@ } else { const uint8_t fill = i + 1 < src_span.size() ? src_span[i + 1] : 0; const size_t fill_size = 257 - src_span[i]; - fxcrt::spanset(dest_span.subspan(dest_count, fill_size), fill); + fxcrt::Fill(dest_span.subspan(dest_count, fill_size), fill); dest_count += fill_size; i += 2; }
diff --git a/core/fxcodec/basic/basicmodule.cpp b/core/fxcodec/basic/basicmodule.cpp index 6e47caa..5099727 100644 --- a/core/fxcodec/basic/basicmodule.cpp +++ b/core/fxcodec/basic/basicmodule.cpp
@@ -17,6 +17,7 @@ #include "core/fxcrt/numerics/safe_conversions.h" #include "core/fxcrt/raw_span.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" namespace fxcodec { @@ -116,7 +117,7 @@ } bool RLScanlineDecoder::Rewind() { - fxcrt::spanclr(pdfium::make_span(m_Scanline)); + fxcrt::Fill(m_Scanline, 0); m_SrcOffset = 0; m_bEOD = false; m_Operator = 0; @@ -129,10 +130,10 @@ } else if (m_bEOD) { return pdfium::span<uint8_t>(); } + fxcrt::Fill(m_Scanline, 0); uint32_t col_pos = 0; bool eol = false; auto scan_span = pdfium::make_span(m_Scanline); - fxcrt::spanclr(scan_span); while (m_SrcOffset < m_SrcBuf.size() && !eol) { if (m_Operator < 128) { uint32_t copy_len = m_Operator + 1; @@ -159,7 +160,7 @@ duplicate_len = pdfium::checked_cast<uint32_t>(m_dwLineBytes - col_pos); eol = true; } - fxcrt::spanset(scan_span.subspan(col_pos, duplicate_len), fill); + fxcrt::Fill(scan_span.subspan(col_pos, duplicate_len), fill); col_pos += duplicate_len; UpdateOperator((uint8_t)duplicate_len); } else {
diff --git a/core/fxcodec/bmp/cfx_bmpdecompressor.cpp b/core/fxcodec/bmp/cfx_bmpdecompressor.cpp index 437168e..dc3d064 100644 --- a/core/fxcodec/bmp/cfx_bmpdecompressor.cpp +++ b/core/fxcodec/bmp/cfx_bmpdecompressor.cpp
@@ -20,6 +20,7 @@ #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/numerics/safe_math.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/calculate_pitch.h" namespace fxcodec { @@ -469,7 +470,7 @@ ReadNextScanline(); col_num_ = 0; - fxcrt::spanset(pdfium::make_span(out_row_buffer_), 0); + fxcrt::Fill(out_row_buffer_, 0); SaveDecodingStatus(DecodeStatus::kData); continue; } @@ -490,7 +491,7 @@ return BmpDecoder::Status::kFail; while (row_num_ < bmp_row_num__next) { - fxcrt::spanset(pdfium::make_span(out_row_buffer_), 0); + fxcrt::Fill(out_row_buffer_, 0); ReadNextScanline(); } break; @@ -530,7 +531,7 @@ return BmpDecoder::Status::kContinue; } - fxcrt::spanset( + fxcrt::Fill( pdfium::make_span(out_row_buffer_).subspan(col_num_, first_part), second_part); @@ -565,7 +566,7 @@ ReadNextScanline(); col_num_ = 0; - fxcrt::spanset(pdfium::make_span(out_row_buffer_), 0); + fxcrt::Fill(out_row_buffer_, 0); SaveDecodingStatus(DecodeStatus::kData); continue; } @@ -586,7 +587,7 @@ return BmpDecoder::Status::kFail; while (row_num_ < bmp_row_num__next) { - fxcrt::spanset(pdfium::make_span(out_row_buffer_), 0); + fxcrt::Fill(out_row_buffer_, 0); ReadNextScanline(); } break;
diff --git a/core/fxcodec/fax/faxmodule.cpp b/core/fxcodec/fax/faxmodule.cpp index 9a54082..9a0bdda 100644 --- a/core/fxcodec/fax/faxmodule.cpp +++ b/core/fxcodec/fax/faxmodule.cpp
@@ -822,7 +822,7 @@ uint8_t last_byte = 0; for (int i = 0; i < m_Rows; ++i) { pdfium::span<uint8_t> buf_span = pdfium::make_span(m_LineBuf); - fxcrt::spanset(buf_span, 0); + fxcrt::Fill(buf_span, 0); buf_span[0] = last_byte; pdfium::span<const uint8_t> scan_line = m_Src->GetScanline(i); FaxEncode2DLine(scan_line);
diff --git a/core/fxcodec/flate/flatemodule.cpp b/core/fxcodec/flate/flatemodule.cpp index 55444a2..a11b7e2 100644 --- a/core/fxcodec/flate/flatemodule.cpp +++ b/core/fxcodec/flate/flatemodule.cpp
@@ -29,6 +29,7 @@ #include "core/fxcrt/raw_span.h" #include "core/fxcrt/span.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/calculate_pitch.h" #if defined(USE_SYSTEM_ZLIB) @@ -98,7 +99,7 @@ uint32_t post_pos = FlateGetPossiblyTruncatedTotalOut(context); CHECK_GE(post_pos, pre_pos); - fxcrt::spanclr(dest_span.subspan(post_pos - pre_pos)); + fxcrt::Fill(dest_span.subspan(post_pos - pre_pos), 0); return ret; }
diff --git a/core/fxcodec/jbig2/jbig2_decoder.cpp b/core/fxcodec/jbig2/jbig2_decoder.cpp index 073f2b7..5867ca1 100644 --- a/core/fxcodec/jbig2/jbig2_decoder.cpp +++ b/core/fxcodec/jbig2/jbig2_decoder.cpp
@@ -10,7 +10,7 @@ #include "core/fxcodec/jbig2/JBig2_DocumentContext.h" #include "core/fxcrt/compiler_specific.h" #include "core/fxcrt/fx_2d_size.h" -#include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" namespace fxcodec { @@ -60,7 +60,7 @@ pJbig2Context->m_nGlobalKey = global_key; pJbig2Context->m_dest_buf = dest_buf.data(); pJbig2Context->m_dest_pitch = dest_pitch; - fxcrt::spanset(dest_buf.first(Fx2DSizeOrDie(height, dest_pitch)), 0); + fxcrt::Fill(dest_buf.first(Fx2DSizeOrDie(height, dest_pitch)), 0); pJbig2Context->m_pContext = CJBig2_Context::Create(global_span, global_key, src_span, src_key, pJBig2DocumentContext->GetSymbolDictCache());
diff --git a/core/fxcodec/jpx/cjpx_decoder.cpp b/core/fxcodec/jpx/cjpx_decoder.cpp index b1ca859..7a84964 100644 --- a/core/fxcodec/jpx/cjpx_decoder.cpp +++ b/core/fxcodec/jpx/cjpx_decoder.cpp
@@ -15,11 +15,12 @@ #include <vector> #include "core/fxcodec/jpx/jpx_decode_utils.h" +#include "core/fxcrt/check_op.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/numerics/safe_conversions.h" #include "core/fxcrt/ptr_util.h" #include "core/fxcrt/span.h" -#include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/calculate_pitch.h" #if !defined(USE_SYSTEM_LIBOPENJPEG2) @@ -558,7 +559,7 @@ // the color component data if `m_Image->numcomps` > `component_count`. // Currently only the color component data is used for rendering. // TODO(crbug.com/pdfium/1747): Make full use of the component information. - fxcrt::spanset(dest_buf.first(m_Image->comps[0].h * pitch), 0xff); + fxcrt::Fill(dest_buf.first(m_Image->comps[0].h * pitch), 0xff); std::vector<uint8_t*> channel_bufs(m_Image->numcomps); std::vector<int> adjust_comps(m_Image->numcomps); const pdfium::span<opj_image_comp_t> components =
diff --git a/core/fxcodec/progressive_decoder.cpp b/core/fxcodec/progressive_decoder.cpp index 76b7791..c15c4ec 100644 --- a/core/fxcodec/progressive_decoder.cpp +++ b/core/fxcodec/progressive_decoder.cpp
@@ -24,6 +24,7 @@ #include "core/fxcrt/notreached.h" #include "core/fxcrt/numerics/safe_conversions.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/dib/cfx_cmyk_to_srgb.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/dib/fx_dib.h" @@ -481,7 +482,7 @@ } const int32_t left = m_GifFrameRect.left; const pdfium::span<uint8_t> decode_span = m_DecodeBuf; - fxcrt::spanset(decode_span.first(m_SrcWidth), pal_index); + fxcrt::Fill(decode_span.first(m_SrcWidth), pal_index); fxcrt::spancpy(decode_span.subspan(left), row_buf.first(img_width)); bool bLastPass = (row_num % 2) == 1;
diff --git a/core/fxcrt/span_util.h b/core/fxcrt/span_util.h index c6611ad..5f5d592 100644 --- a/core/fxcrt/span_util.h +++ b/core/fxcrt/span_util.h
@@ -106,28 +106,6 @@ return true; } -// Bounds-checked sets into spans. -template <typename T, - size_t N, - typename P, - typename = std::enable_if_t<std::is_trivially_constructible_v<T> && - std::is_trivially_destructible_v<T>>> -void spanset(pdfium::span<T, N, P> dst, uint8_t val) { - // SAFETY: `dst.size_bytes()` accurately describes `dst.data()`. - UNSAFE_BUFFERS(FXSYS_memset(dst.data(), val, dst.size_bytes())); -} - -// Bounds-checked zeroing of spans. -template <typename T, - size_t N, - typename P, - typename = std::enable_if_t<std::is_trivially_constructible_v<T> && - std::is_trivially_destructible_v<T>>> -void spanclr(pdfium::span<T, N, P> dst) { - // SAFETY: `dst.size_bytes()` accurately describes `dst.data()`. - UNSAFE_BUFFERS(FXSYS_memset(dst.data(), 0, dst.size_bytes())); -} - // Bounds-checked byte-for-byte equality of same-sized spans. This is // helpful because span does not (yet) have an operator==(). template <typename T1,
diff --git a/core/fxcrt/span_util_unittest.cpp b/core/fxcrt/span_util_unittest.cpp index 825bf2b..7ce7b38 100644 --- a/core/fxcrt/span_util_unittest.cpp +++ b/core/fxcrt/span_util_unittest.cpp
@@ -9,24 +9,6 @@ #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" -TEST(Spanset, Fits) { - std::vector<char> dst(4, 'B'); - fxcrt::spanset(pdfium::make_span(dst).first(2), 'A'); - EXPECT_EQ(dst[0], 'A'); - EXPECT_EQ(dst[1], 'A'); - EXPECT_EQ(dst[2], 'B'); - EXPECT_EQ(dst[3], 'B'); -} - -TEST(Spanset, Empty) { - std::vector<char> dst(4, 'B'); - fxcrt::spanset(pdfium::make_span(dst).subspan(4), 'A'); - EXPECT_EQ(dst[0], 'B'); - EXPECT_EQ(dst[1], 'B'); - EXPECT_EQ(dst[2], 'B'); - EXPECT_EQ(dst[3], 'B'); -} - TEST(Spancpy, FitsEntirely) { std::vector<char> src(4, 'A'); std::vector<char> dst(4, 'B');
diff --git a/core/fxge/dib/cfx_bitmapcomposer.cpp b/core/fxge/dib/cfx_bitmapcomposer.cpp index 37a5cfe..5665ecf 100644 --- a/core/fxge/dib/cfx_bitmapcomposer.cpp +++ b/core/fxge/dib/cfx_bitmapcomposer.cpp
@@ -14,7 +14,7 @@ #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/fx_system.h" -#include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/cfx_cliprgn.h" #include "core/fxge/dib/cfx_dibitmap.h" @@ -83,8 +83,8 @@ m_pAddClipScan[i] = clip_scan[i] * m_Alpha; } } else { - fxcrt::spanset(pdfium::make_span(m_pAddClipScan).first(dest_width), - FXSYS_roundf(m_Alpha * 255)); + fxcrt::Fill(pdfium::make_span(m_pAddClipScan).first(dest_width), + FXSYS_roundf(m_Alpha * 255)); } clip_scan = m_pAddClipScan; }
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp index 94f956e..196c5da 100644 --- a/core/fxge/dib/cfx_dibbase.cpp +++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -21,6 +21,7 @@ #include "core/fxcrt/notreached.h" #include "core/fxcrt/span.h" #include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" #include "core/fxge/calculate_pitch.h" #include "core/fxge/cfx_cliprgn.h" #include "core/fxge/dib/cfx_bitmapstorer.h" @@ -147,7 +148,7 @@ dest_buf.subspan(Fx2DSizeOrDie(row, dest_pitch)); pdfium::span<const uint8_t> src_span = pSrcBitmap->GetScanline(src_top + row); - fxcrt::spanset(dest_span.first(width), kResetGray); + fxcrt::Fill(dest_span.first(width), kResetGray); uint8_t* dest_scan = dest_span.data(); const uint8_t* src_scan = src_span.data(); UNSAFE_TODO({ @@ -195,7 +196,7 @@ for (int row = 0; row < height; ++row) { pdfium::span<uint8_t> dest_span = dest_buf.subspan(Fx2DSizeOrDie(row, dest_pitch)); - fxcrt::spanset(dest_span.first(width), gray0); + fxcrt::Fill(dest_span.first(width), gray0); uint8_t* dest_scan = dest_span.data(); const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row).data(); UNSAFE_TODO({ @@ -270,7 +271,7 @@ pdfium::span<uint8_t> dest_span = dest_buf.subspan(Fx2DSizeOrDie(row, dest_pitch)); // Set all destination pixels to be white initially. - fxcrt::spanset(dest_span.first(width), 255); + fxcrt::Fill(dest_span.first(width), 255); uint8_t* dest_scan = dest_span.data(); const uint8_t* src_scan = pSrcBitmap->GetScanline(src_top + row).data(); UNSAFE_TODO({ @@ -1020,7 +1021,7 @@ const int col_end = bYFlip ? m_Width - dest_clip.top : dest_clip.bottom; UNSAFE_TODO({ if (GetBPP() == 1) { - fxcrt::spanset(dest_span, 0xff); + fxcrt::Fill(dest_span, 0xff); if (bYFlip) { dest_span = dest_span.subspan(dest_last_row_offset); }
diff --git a/fpdfsdk/cpdfsdk_helpers_unittest.cpp b/fpdfsdk/cpdfsdk_helpers_unittest.cpp index 0dcff4d..4648272 100644 --- a/fpdfsdk/cpdfsdk_helpers_unittest.cpp +++ b/fpdfsdk/cpdfsdk_helpers_unittest.cpp
@@ -6,6 +6,7 @@ #include "core/fxcrt/compiler_specific.h" #include "core/fxcrt/fx_memcpy_wrappers.h" +#include "core/fxcrt/stl_util.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -23,7 +24,7 @@ // Buffer should not change if declared length is too short. char buf[kExpectedToBeCopiedLen + 1]; - fxcrt::spanset(pdfium::make_span(buf), 0x42); + fxcrt::Fill(buf, 0x42); ASSERT_EQ(kExpectedToBeCopiedLen + 1, NulTerminateMaybeCopyAndReturnLength( to_be_copied,
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp index 459a63f..f14219a 100644 --- a/fpdfsdk/fpdf_view.cpp +++ b/fpdfsdk/fpdf_view.cpp
@@ -642,9 +642,9 @@ if (win_dc.GetDeviceType() == DeviceType::kPrinter) { auto dest_bitmap = pdfium::MakeRetain<CFX_DIBitmap>(); if (dest_bitmap->Create(size_x, size_y, FXDIB_Format::kRgb32)) { - fxcrt::spanset(dest_bitmap->GetWritableBuffer().first( - pBitmap->GetPitch() * size_y), - -1); + fxcrt::Fill(dest_bitmap->GetWritableBuffer().first(pBitmap->GetPitch() * + size_y), + -1); dest_bitmap->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, BlendMode::kNormal, nullptr, false); win_dc.StretchDIBits(std::move(dest_bitmap), 0, 0, size_x, size_y);
diff --git a/fxbarcode/pdf417/BC_PDF417BarcodeRow.cpp b/fxbarcode/pdf417/BC_PDF417BarcodeRow.cpp index a11f211..77e24bd 100644 --- a/fxbarcode/pdf417/BC_PDF417BarcodeRow.cpp +++ b/fxbarcode/pdf417/BC_PDF417BarcodeRow.cpp
@@ -23,7 +23,7 @@ #include "fxbarcode/pdf417/BC_PDF417BarcodeRow.h" #include "core/fxcrt/check_op.h" -#include "core/fxcrt/span_util.h" +#include "core/fxcrt/stl_util.h" CBC_BarcodeRow::CBC_BarcodeRow(size_t width) : row_(FixedSizeDataVector<uint8_t>::Zeroed(width)) {} @@ -33,6 +33,6 @@ void CBC_BarcodeRow::AddBar(bool black, size_t width) { pdfium::span<uint8_t> available = row_.subspan(offset_); CHECK_LE(width, available.size()); - fxcrt::spanset(available.first(width), black ? 1 : 0); + fxcrt::Fill(available.first(width), black ? 1 : 0); offset_ += width; }
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index e41f651..083bb4c 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -752,7 +752,7 @@ if (unicode_range.has_value()) { fxcrt::spancpy(usb_span, pdfium::make_span(unicode_range.value())); } else { - fxcrt::spanclr(usb_span); + fxcrt::Fill(usb_span, 0); } std::optional<std::array<uint32_t, 2>> code_page_range = @@ -761,7 +761,7 @@ if (code_page_range.has_value()) { fxcrt::spancpy(csb_span, pdfium::make_span(code_page_range.value())); } else { - fxcrt::spanclr(csb_span); + fxcrt::Fill(csb_span, 0); } static constexpr uint32_t kNameTag =