Replace UTIL_* definitions with DataType enum class
Bug: pdfium:1085
Change-Id: Iccf276068dacae939c88f0fe508cc88d6b4a0a31
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/80572
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/cjs_util.cpp b/fxjs/cjs_util.cpp
index 9d11c7a..81820fe 100644
--- a/fxjs/cjs_util.cpp
+++ b/fxjs/cjs_util.cpp
@@ -135,14 +135,14 @@
WideString segment;
switch (ParseDataType(&fmt)) {
- case UTIL_INT:
+ case DataType::kInt:
segment = WideString::Format(fmt.c_str(), pRuntime->ToInt32(params[i]));
break;
- case UTIL_DOUBLE:
+ case DataType::kDouble:
segment =
WideString::Format(fmt.c_str(), pRuntime->ToDouble(params[i]));
break;
- case UTIL_STRING:
+ case DataType::kString:
segment = WideString::Format(fmt.c_str(),
pRuntime->ToWideString(params[i]).c_str());
break;
@@ -398,10 +398,10 @@
}
// static
-int CJS_Util::ParseDataType(WideString* sFormat) {
+CJS_Util::DataType CJS_Util::ParseDataType(WideString* sFormat) {
enum State { BEFORE, FLAGS, WIDTH, PRECISION, SPECIFIER, AFTER };
- int result = -1;
+ DataType result = DataType::kInvalid;
State state = BEFORE;
size_t precision_digits = 0;
size_t i = 0;
@@ -422,7 +422,7 @@
break;
case WIDTH:
if (c == L'*')
- return -1;
+ return DataType::kInvalid;
if (FXSYS_IsDecimalDigit(c)) {
// Stay in same state.
} else if (c == L'.') {
@@ -434,7 +434,7 @@
break;
case PRECISION:
if (c == L'*')
- return -1;
+ return DataType::kInvalid;
if (FXSYS_IsDecimalDigit(c)) {
// Stay in same state.
++precision_digits;
@@ -446,32 +446,32 @@
case SPECIFIER:
if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
c == L'u' || c == L'x' || c == L'X') {
- result = UTIL_INT;
+ result = DataType::kInt;
} else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' ||
c == L'G') {
- result = UTIL_DOUBLE;
+ result = DataType::kDouble;
} else if (c == L's' || c == L'S') {
// Map s to S since we always deal internally with wchar_t strings.
// TODO(tsepez): Probably 100% borked. %S is not a standard
// conversion.
sFormat->SetAt(i, L'S');
- result = UTIL_STRING;
+ result = DataType::kString;
} else {
- return -1;
+ return DataType::kInvalid;
}
state = AFTER;
break;
case AFTER:
if (c == L'%')
- return -1;
+ return DataType::kInvalid;
// Stay in same state until string exhausted.
break;
}
++i;
}
// See https://crbug.com/740166
- if (result == UTIL_INT && precision_digits > 2)
- return -1;
+ if (result == DataType::kInt && precision_digits > 2)
+ return DataType::kInvalid;
return result;
}
diff --git a/fxjs/cjs_util.h b/fxjs/cjs_util.h
index e565520..2ad4d27 100644
--- a/fxjs/cjs_util.h
+++ b/fxjs/cjs_util.h
@@ -13,13 +13,15 @@
#include "fxjs/cjs_object.h"
#include "fxjs/js_define.h"
-// Return values for ParseDataType() below.
-#define UTIL_INT 0
-#define UTIL_DOUBLE 1
-#define UTIL_STRING 2
-
class CJS_Util final : public CJS_Object {
public:
+ enum class DataType {
+ kInvalid = -1,
+ kInt = 0,
+ kDouble = 1,
+ kString = 2,
+ };
+
static uint32_t GetObjDefnID();
static void DefineJSObjects(CFXJS_Engine* pEngine);
@@ -33,7 +35,7 @@
// byte-by-byte.
//
// Exposed for testing.
- static int ParseDataType(WideString* sFormat);
+ static DataType ParseDataType(WideString* sFormat);
// Exposed for testing.
static WideString StringPrintx(const WideString& cFormat,
diff --git a/fxjs/cjs_util_unittest.cpp b/fxjs/cjs_util_unittest.cpp
index b4c07f7..77fa270 100644
--- a/fxjs/cjs_util_unittest.cpp
+++ b/fxjs/cjs_util_unittest.cpp
@@ -10,99 +10,99 @@
TEST(CJS_Util, ParseDataType) {
struct ParseDataTypeCase {
const wchar_t* const input_string;
- const int expected;
+ const CJS_Util::DataType expected;
};
// Commented out tests follow the spec but are not passing.
const ParseDataTypeCase cases[] = {
// Not conversions
- {L"", -1},
- {L"d", -1},
+ {L"", CJS_Util::DataType::kInvalid},
+ {L"d", CJS_Util::DataType::kInvalid},
// Simple cases
- {L"%d", UTIL_INT},
- {L"%x", UTIL_INT},
- {L"%f", UTIL_DOUBLE},
- {L"%s", UTIL_STRING},
+ {L"%d", CJS_Util::DataType::kInt},
+ {L"%x", CJS_Util::DataType::kInt},
+ {L"%f", CJS_Util::DataType::kDouble},
+ {L"%s", CJS_Util::DataType::kString},
// nDecSep Not implemented
- // {L"%,0d", UTIL_INT},
- // {L"%,1d", UTIL_INT},
- // {L"%,2d", UTIL_INT},
- // {L"%,3d", UTIL_INT},
+ // {L"%,0d", CJS_Util::DataType::kInt},
+ // {L"%,1d", CJS_Util::DataType::kInt},
+ // {L"%,2d", CJS_Util::DataType::kInt},
+ // {L"%,3d", CJS_Util::DataType::kInt},
// {L"%,4d", -1},
// {L"%,d", -1},
// cFlags("+ 0#"") are only valid for numeric conversions.
- {L"%+d", UTIL_INT},
- {L"%+x", UTIL_INT},
- {L"%+f", UTIL_DOUBLE},
+ {L"%+d", CJS_Util::DataType::kInt},
+ {L"%+x", CJS_Util::DataType::kInt},
+ {L"%+f", CJS_Util::DataType::kDouble},
// {L"%+s", -1},
- {L"% d", UTIL_INT},
- {L"% x", UTIL_INT},
- {L"% f", UTIL_DOUBLE},
+ {L"% d", CJS_Util::DataType::kInt},
+ {L"% x", CJS_Util::DataType::kInt},
+ {L"% f", CJS_Util::DataType::kDouble},
// {L"% s", -1},
- {L"%0d", UTIL_INT},
- {L"%0x", UTIL_INT},
- {L"%0f", UTIL_DOUBLE},
+ {L"%0d", CJS_Util::DataType::kInt},
+ {L"%0x", CJS_Util::DataType::kInt},
+ {L"%0f", CJS_Util::DataType::kDouble},
// {L"%0s", -1},
- {L"%#d", UTIL_INT},
- {L"%#x", UTIL_INT},
- {L"%#f", UTIL_DOUBLE},
+ {L"%#d", CJS_Util::DataType::kInt},
+ {L"%#x", CJS_Util::DataType::kInt},
+ {L"%#f", CJS_Util::DataType::kDouble},
// {L"%#s", -1},
// nWidth should work. for all conversions, can be combined with cFlags=0
// for numbers.
- {L"%5d", UTIL_INT},
- {L"%05d", UTIL_INT},
- {L"%5x", UTIL_INT},
- {L"%05x", UTIL_INT},
- {L"%5f", UTIL_DOUBLE},
- {L"%05f", UTIL_DOUBLE},
- {L"%5s", UTIL_STRING},
+ {L"%5d", CJS_Util::DataType::kInt},
+ {L"%05d", CJS_Util::DataType::kInt},
+ {L"%5x", CJS_Util::DataType::kInt},
+ {L"%05x", CJS_Util::DataType::kInt},
+ {L"%5f", CJS_Util::DataType::kDouble},
+ {L"%05f", CJS_Util::DataType::kDouble},
+ {L"%5s", CJS_Util::DataType::kString},
// {L"%05s", -1},
// nPrecision should only work for float
// {L"%.5d", -1},
// {L"%.5x", -1},
- {L"%.5f", UTIL_DOUBLE},
+ {L"%.5f", CJS_Util::DataType::kDouble},
// {L"%.5s", -1},
// {L"%.14d", -1},
// {L"%.14x", -1},
- {L"%.14f", UTIL_DOUBLE},
+ {L"%.14f", CJS_Util::DataType::kDouble},
// {L"%.14s", -1},
// {L"%.f", -1},
// See https://crbug.com/740166
// nPrecision too large (> 260) causes crashes in Windows.
// Avoid this by limiting to two digits
- {L"%.1d", UTIL_INT},
- {L"%.10d", UTIL_INT},
- {L"%.100d", -1},
+ {L"%.1d", CJS_Util::DataType::kInt},
+ {L"%.10d", CJS_Util::DataType::kInt},
+ {L"%.100d", CJS_Util::DataType::kInvalid},
// Unexpected characters
- {L"%ad", -1},
- {L"%bx", -1},
- // {L"%cf", -1},
- // {L"%es", -1},
- // {L"%gd", -1},
- {L"%hx", -1},
- // {L"%if", -1},
- {L"%js", -1},
- {L"%@d", -1},
- {L"%~x", -1},
- {L"%[f", -1},
- {L"%\0s", -1},
- {L"%\nd", -1},
- {L"%\rx", -1},
- // {L"%%f", -1},
- // {L"% s", -1},
+ {L"%ad", CJS_Util::DataType::kInvalid},
+ {L"%bx", CJS_Util::DataType::kInvalid},
+ // {L"%cf", CJS_Util::DataType::kInvalid},
+ // {L"%es", CJS_Util::DataType::kInvalid},
+ // {L"%gd", CJS_Util::DataType::kInvalid},
+ {L"%hx", CJS_Util::DataType::kInvalid},
+ // {L"%if", CJS_Util::DataType::kInvalid},
+ {L"%js", CJS_Util::DataType::kInvalid},
+ {L"%@d", CJS_Util::DataType::kInvalid},
+ {L"%~x", CJS_Util::DataType::kInvalid},
+ {L"%[f", CJS_Util::DataType::kInvalid},
+ {L"%\0s", CJS_Util::DataType::kInvalid},
+ {L"%\nd", CJS_Util::DataType::kInvalid},
+ {L"%\rx", CJS_Util::DataType::kInvalid},
+ // {L"%%f", CJS_Util::DataType::kInvalid},
+ // {L"% s", CJS_Util::DataType::kInvalid},
// Combine multiple valid components
- {L"%+6d", UTIL_INT},
- {L"% 7x", UTIL_INT},
- {L"%#9.3f", UTIL_DOUBLE},
- {L"%10s", UTIL_STRING},
+ {L"%+6d", CJS_Util::DataType::kInt},
+ {L"% 7x", CJS_Util::DataType::kInt},
+ {L"%#9.3f", CJS_Util::DataType::kDouble},
+ {L"%10s", CJS_Util::DataType::kString},
};
for (size_t i = 0; i < pdfium::size(cases); i++) {