summaryrefslogtreecommitdiff
path: root/fpdfsdk/javascript/util.cpp
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-03-28 14:13:16 -0700
committerCommit bot <commit-bot@chromium.org>2016-03-28 14:13:16 -0700
commit4f1f41f338ce0899e48605c82375a72883f4eb7c (patch)
treea89cc3f1d8b981fa12d7fd423ef6883c7a502798 /fpdfsdk/javascript/util.cpp
parent4678e283c5b48a8954bd0db3565b1376bd9c6b27 (diff)
downloadpdfium-4f1f41f338ce0899e48605c82375a72883f4eb7c.tar.xz
Added test for util.printx() and make it match spec.
R=dsinclair@chromium.org Review URL: https://codereview.chromium.org/1837543002
Diffstat (limited to 'fpdfsdk/javascript/util.cpp')
-rw-r--r--fpdfsdk/javascript/util.cpp161
1 files changed, 89 insertions, 72 deletions
diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp
index 30656cd766..f7b2d7fbfa 100644
--- a/fpdfsdk/javascript/util.cpp
+++ b/fpdfsdk/javascript/util.cpp
@@ -309,96 +309,113 @@ FX_BOOL util::printx(IJS_Context* cc,
const std::vector<CJS_Value>& params,
CJS_Value& vRet,
CFX_WideString& sError) {
- int iSize = params.size();
- if (iSize < 2)
+ if (params.size() < 2) {
+ sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR);
return FALSE;
- CFX_WideString sFormat = params[0].ToCFXWideString();
- CFX_WideString sSource = params[1].ToCFXWideString();
- std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
- std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
- std::string cDest;
- printx(cFormat, cSource, cDest);
- vRet = cDest.c_str();
+ }
+ vRet = printx(params[0].ToCFXWideString(), params[1].ToCFXWideString());
return TRUE;
}
-void util::printx(const std::string& cFormat,
- const std::string& cSource2,
- std::string& cPurpose) {
- std::string cSource(cSource2);
- if (!cPurpose.empty())
- // cPurpose.clear();
- cPurpose.erase();
- int itSource = 0;
- int iSize = cSource.size();
- for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
- iIndex++) {
- char letter = cFormat[iIndex];
- switch (letter) {
- case '?':
- cPurpose += cSource[itSource];
- itSource++;
- break;
+enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
+
+static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) {
+ if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
+ return input | 0x20;
+ if (eMode == kUpperCase && input >= 'a' && input <= 'z')
+ return input & ~0x20;
+ return input;
+}
+
+CFX_WideString util::printx(const CFX_WideString& wsFormat,
+ const CFX_WideString& wsSource) {
+ CFX_WideString wsResult;
+ FX_STRSIZE iSourceIdx = 0;
+ FX_STRSIZE iFormatIdx = 0;
+ CaseMode eCaseMode = kPreserveCase;
+ bool bEscaped = false;
+ while (iFormatIdx < wsFormat.GetLength()) {
+ if (bEscaped) {
+ bEscaped = false;
+ wsResult += wsFormat[iFormatIdx];
+ ++iFormatIdx;
+ continue;
+ }
+ switch (wsFormat[iFormatIdx]) {
+ case '\\': {
+ bEscaped = true;
+ ++iFormatIdx;
+ } break;
+ case '<': {
+ eCaseMode = kLowerCase;
+ ++iFormatIdx;
+ } break;
+ case '>': {
+ eCaseMode = kUpperCase;
+ ++iFormatIdx;
+ } break;
+ case '=': {
+ eCaseMode = kPreserveCase;
+ ++iFormatIdx;
+ } break;
+ case '?': {
+ if (iSourceIdx < wsSource.GetLength()) {
+ wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
+ ++iSourceIdx;
+ }
+ ++iFormatIdx;
+ } break;
case 'X': {
- while (itSource < iSize) {
- if (std::isdigit(cSource[itSource]) ||
- (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
- (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
- cPurpose += cSource[itSource];
- itSource++;
- break;
+ if (iSourceIdx < wsSource.GetLength()) {
+ if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
+ (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
+ (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
+ wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
+ ++iFormatIdx;
}
- itSource++;
+ ++iSourceIdx;
+ } else {
+ ++iFormatIdx;
}
- break;
} break;
case 'A': {
- while (itSource < iSize) {
- if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
- (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
- cPurpose += cSource[itSource];
- itSource++;
- break;
+ if (iSourceIdx < wsSource.GetLength()) {
+ if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
+ (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
+ wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
+ ++iFormatIdx;
}
- itSource++;
+ ++iSourceIdx;
+ } else {
+ ++iFormatIdx;
}
- break;
} break;
case '9': {
- while (itSource < iSize) {
- if (std::isdigit(cSource[itSource])) {
- cPurpose += cSource[itSource];
- itSource++;
- break;
+ if (iSourceIdx < wsSource.GetLength()) {
+ if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
+ wsResult += wsSource[iSourceIdx];
+ ++iFormatIdx;
}
- itSource++;
+ ++iSourceIdx;
+ } else {
+ ++iFormatIdx;
}
- break;
- }
+ } break;
case '*': {
- cPurpose.append(cSource, itSource, iSize - itSource);
- itSource = iSize - 1;
- break;
- }
- case '\\':
- break;
- case '>': {
- for (char& c : cSource)
- c = toupper(c);
- break;
- }
- case '<': {
- for (char& c : cSource)
- c = tolower(c);
- break;
- }
- case '=':
- break;
- default:
- cPurpose += letter;
- break;
+ if (iSourceIdx < wsSource.GetLength()) {
+ wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
+ ++iSourceIdx;
+ } else {
+ ++iFormatIdx;
+ }
+ } break;
+ default: {
+ wsResult += wsFormat[iFormatIdx];
+ ++iFormatIdx;
+ } break;
}
}
+ return wsResult;
}
FX_BOOL util::scand(IJS_Context* cc,