aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2024-05-23 16:25:42 +0000
committerPdfium LUCI CQ <pdfium-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-05-23 16:25:42 +0000
commitecb79e63534311e38951c65bf807bade4872fa3d (patch)
tree50ac5fd287ecdfa6cb0c75a628286858556a9064
parent59f088efc51f9cb831e8278263c90dfe3935207d (diff)
downloadpdfium-ecb79e63534311e38951c65bf807bade4872fa3d.tar.gz
Take std::to_array<>() equivalent from chromium base.
to_array<>() helps clean up std::array<> initialization, however it can not be used until C++20, and we are holding the line at C++17. The code differs from base in that we need to suppress both UNSAFE_BUFFER usage and narowing via a static_cast<> to run under stricter compiler flags. -- Demonstrate its use in one place. Change-Id: Iddaf1fa936f107562049a54733c657c2184a872b Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119512 Reviewed-by: Lei Zhang <thestig@chromium.org> Reviewed-by: Thomas Sepez <tsepez@google.com> Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r--core/fpdfdoc/cpdf_dest.cpp11
-rw-r--r--core/fxcrt/stl_util.h24
2 files changed, 30 insertions, 5 deletions
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp
index e89ea7abe..8026a75d5 100644
--- a/core/fpdfdoc/cpdf_dest.cpp
+++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -15,17 +15,18 @@
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_number.h"
#include "core/fpdfdoc/cpdf_nametree.h"
+#include "core/fxcrt/stl_util.h"
namespace {
// These arrays are indexed by the PDFDEST_VIEW_* constants.
-constexpr std::array<const char*, 9> kZoomModes = {{"Unknown", "XYZ", "Fit",
- "FitH", "FitV", "FitR",
- "FitB", "FitBH", "FitBV"}};
+constexpr auto kZoomModes =
+ fxcrt::ToArray<const char*>({"Unknown", "XYZ", "Fit", "FitH", "FitV",
+ "FitR", "FitB", "FitBH", "FitBV"});
-constexpr std::array<uint8_t, 9> kZoomModeMaxParamCount = {
- {0, 3, 0, 1, 1, 4, 0, 1, 1}};
+constexpr auto kZoomModeMaxParamCount =
+ fxcrt::ToArray<const uint8_t>({0, 3, 0, 1, 1, 4, 0, 1, 1});
} // namespace
diff --git a/core/fxcrt/stl_util.h b/core/fxcrt/stl_util.h
index 8fe163914..363f36bc4 100644
--- a/core/fxcrt/stl_util.h
+++ b/core/fxcrt/stl_util.h
@@ -8,6 +8,7 @@
#include <algorithm>
#include <memory>
+#include "core/fxcrt/compiler_specific.h"
#include "core/fxcrt/numerics/safe_conversions.h"
namespace fxcrt {
@@ -47,6 +48,29 @@ void Fill(T& container, const V& value) {
std::fill(std::begin(container), std::end(container), value);
}
+// ToArray<>() implementation as taken from chromium /base. Replace with
+// std::to_array<>() when C++20 becomes available.
+//
+// Helper inspired by C++20's std::to_array to convert a C-style array to a
+// std::array. As opposed to the C++20 version this implementation does not
+// provide an overload for rvalues and does not strip cv qualifers from the
+// returned std::array::value_type. The returned value_type needs to be
+// specified explicitly, allowing the construction of std::arrays with const
+// elements.
+//
+// Reference: https://en.cppreference.com/w/cpp/container/array/to_array
+template <typename U, typename T, size_t N, size_t... I>
+constexpr std::array<U, N> ToArrayImpl(const T (&data)[N],
+ std::index_sequence<I...>) {
+ // SAFETY: compiler-deduced size `N`.
+ return UNSAFE_BUFFERS({{static_cast<U>(data[I])...}});
+}
+
+template <typename U, typename T, size_t N>
+constexpr std::array<U, N> ToArray(const T (&data)[N]) {
+ return ToArrayImpl<U>(data, std::make_index_sequence<N>());
+}
+
} // namespace fxcrt
#endif // CORE_FXCRT_STL_UTIL_H_