Add WideString::FormatInteger().
Make more consistent with ByteString static methods.
Change-Id: I8a4b415115b6ae5f0b6f74daa929f43acb1ad99f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93030
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 8f58a2c..a30ac62 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -288,6 +288,13 @@
"Strings must not require more space than pointers");
// static
+WideString WideString::FormatInteger(int i) {
+ wchar_t wbuf[32];
+ swprintf(wbuf, std::size(wbuf), L"%d", i);
+ return WideString(wbuf);
+}
+
+// static
WideString WideString::FormatV(const wchar_t* format, va_list argList) {
va_list argListCopy;
va_copy(argListCopy, argList);
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index 24774bd..7fc26aa 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -36,6 +36,7 @@
using const_iterator = const CharType*;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
+ [[nodiscard]] static WideString FormatInteger(int i);
[[nodiscard]] static WideString Format(const wchar_t* pFormat, ...);
[[nodiscard]] static WideString FormatV(const wchar_t* lpszFormat,
va_list argList);
diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp
index 8632280..640755d 100644
--- a/core/fxcrt/widestring_unittest.cpp
+++ b/core/fxcrt/widestring_unittest.cpp
@@ -2056,6 +2056,21 @@
}
}
+TEST(WideString, FormatInteger) {
+ // Base case of 0.
+ EXPECT_EQ(L"0", WideString::FormatInteger(0));
+
+ // Positive ordinary number.
+ EXPECT_EQ(L"123456", WideString::FormatInteger(123456));
+
+ // Negative ordinary number.
+ EXPECT_EQ(L"-123456", WideString::FormatInteger(-123456));
+
+ // int limits.
+ EXPECT_EQ(L"2147483647", WideString::FormatInteger(INT_MAX));
+ EXPECT_EQ(L"-2147483648", WideString::FormatInteger(INT_MIN));
+}
+
TEST(WideString, FX_HashCode_Wide) {
EXPECT_EQ(0u, FX_HashCode_GetW(L""));
EXPECT_EQ(65u, FX_HashCode_GetW(L"A"));