Pass spans to ToString<T>().
Avoid some pointer arithmetic.
Then adjust callers to pass the same, until we reach a point where
c-style char[] arrays are being passed. These now silently convert
to spans rather than decaying to char* ptrs.
Change-Id: I1243a3f2ad398eeb65dfcf6643eb6f549e1d80b6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101490
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/fx_string.cpp b/core/fxcrt/fx_string.cpp
index 1c497ed..e661363 100644
--- a/core/fxcrt/fx_string.cpp
+++ b/core/fxcrt/fx_string.cpp
@@ -11,6 +11,7 @@
#include "core/fxcrt/cfx_utf8decoder.h"
#include "core/fxcrt/cfx_utf8encoder.h"
#include "core/fxcrt/fx_extension.h"
+#include "core/fxcrt/span_util.h"
#include "third_party/base/compiler_specific.h"
#include "third_party/base/span.h"
@@ -86,7 +87,7 @@
}
template <class T>
-size_t ToString(T value, int (*round_func)(T), char* buf) {
+size_t ToString(T value, int (*round_func)(T), pdfium::span<char> buf) {
buf[0] = '0';
buf[1] = '\0';
if (value == 0) {
@@ -117,7 +118,7 @@
int i = scaled / scale;
FXSYS_itoa(i, buf2, 10);
size_t len = strlen(buf2);
- memcpy(buf + buf_size, buf2, len);
+ fxcrt::spancpy(buf.subspan(buf_size), pdfium::make_span(buf2).first(len));
buf_size += len;
int fraction = scaled % scale;
if (fraction == 0) {
@@ -143,7 +144,7 @@
return StringToFloat(FX_UTF8Encode(wsStr).AsStringView());
}
-size_t FloatToString(float f, char* buf) {
+size_t FloatToString(float f, pdfium::span<char> buf) {
return ToString<float>(f, FXSYS_roundf, buf);
}
@@ -155,7 +156,7 @@
return StringToDouble(FX_UTF8Encode(wsStr).AsStringView());
}
-size_t DoubleToString(double d, char* buf) {
+size_t DoubleToString(double d, pdfium::span<char> buf) {
return ToString<double>(d, FXSYS_round, buf);
}
diff --git a/core/fxcrt/fx_string.h b/core/fxcrt/fx_string.h
index b43b899..41d73e4 100644
--- a/core/fxcrt/fx_string.h
+++ b/core/fxcrt/fx_string.h
@@ -13,6 +13,7 @@
#include "core/fxcrt/bytestring.h"
#include "core/fxcrt/widestring.h"
+#include "third_party/base/span.h"
constexpr uint32_t FXBSTR_ID(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4) {
return static_cast<uint32_t>(c1) << 24 | static_cast<uint32_t>(c2) << 16 |
@@ -24,11 +25,11 @@
float StringToFloat(ByteStringView str);
float StringToFloat(WideStringView wsStr);
-size_t FloatToString(float f, char* buf);
+size_t FloatToString(float f, pdfium::span<char> buf);
double StringToDouble(ByteStringView str);
double StringToDouble(WideStringView wsStr);
-size_t DoubleToString(double d, char* buf);
+size_t DoubleToString(double d, pdfium::span<char> buf);
namespace fxcrt {
diff --git a/core/fxcrt/fx_string_unittest.cpp b/core/fxcrt/fx_string_unittest.cpp
index 87891c1..2619864 100644
--- a/core/fxcrt/fx_string_unittest.cpp
+++ b/core/fxcrt/fx_string_unittest.cpp
@@ -6,17 +6,18 @@
#include "core/fxcrt/fx_string.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/base/span.h"
-char* TerminatedFloatToString(float value, char* buf) {
+char* TerminatedFloatToString(float value, pdfium::span<char> buf) {
size_t buflen = FloatToString(value, buf);
buf[buflen] = '\0';
- return buf;
+ return buf.data();
}
-char* TerminatedDoubleToString(double value, char* buf) {
+char* TerminatedDoubleToString(double value, pdfium::span<char> buf) {
size_t buflen = DoubleToString(value, buf);
buf[buflen] = '\0';
- return buf;
+ return buf.data();
}
TEST(fxstring, FX_UTF8Encode) {