summaryrefslogtreecommitdiff
path: root/fpdfsdk/javascript/PublicMethods.cpp
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-04-12 16:46:34 -0700
committerCommit bot <commit-bot@chromium.org>2016-04-12 16:46:34 -0700
commit745611b68cdfcbfb50d792694ed2dc7be9f4f936 (patch)
tree5c848f1d3cd53dcd5ee8fb35707b7320e85cb28c /fpdfsdk/javascript/PublicMethods.cpp
parent461129efe889fa7d653c8c4620a12179eeddd5ec (diff)
downloadpdfium-745611b68cdfcbfb50d792694ed2dc7be9f4f936.tar.xz
Make CJS_PublicMethods::StrTrim() take a CFX_ByteString
Similar to https://codereview.chromium.org/1876203002 but in a different file. The result is still not as efficient as possible owing to the conversions back and forth to C types. Convert these methods to ordinary functions in anon namespace while we're at it. Review URL: https://codereview.chromium.org/1882173002
Diffstat (limited to 'fpdfsdk/javascript/PublicMethods.cpp')
-rw-r--r--fpdfsdk/javascript/PublicMethods.cpp87
1 files changed, 32 insertions, 55 deletions
diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp
index eb0bd1b306..5a694c742a 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -52,20 +52,37 @@ END_JS_STATIC_GLOBAL_FUN()
IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
-static const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
- L"May", L"Jun", L"Jul", L"Aug",
- L"Sep", L"Oct", L"Nov", L"Dec"};
+namespace {
+
+const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
+ L"May", L"Jun", L"Jul", L"Aug",
+ L"Sep", L"Oct", L"Nov", L"Dec"};
+
+const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
+ L"April", L"May", L"June",
+ L"July", L"August", L"September",
+ L"October", L"November", L"December"};
+
+CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
+ CFX_ByteString result(pStr);
+ result.TrimLeft(' ');
+ result.TrimRight(' ');
+ return result;
+}
+
+CFX_WideString StrTrim(const CFX_WideString& pStr) {
+ CFX_WideString result(pStr);
+ result.TrimLeft(' ');
+ result.TrimRight(' ');
+ return result;
+}
-static const FX_WCHAR* const fullmonths[] = {
- L"January", L"February", L"March", L"April",
- L"May", L"June", L"July", L"August",
- L"September", L"October", L"November", L"December"};
+} // namespace
-bool CJS_PublicMethods::IsNumber(const FX_WCHAR* str) {
+bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
CFX_WideString sTrim = StrTrim(str);
const FX_WCHAR* pTrim = sTrim.c_str();
const FX_WCHAR* p = pTrim;
-
bool bDot = false;
bool bKXJS = false;
@@ -136,48 +153,6 @@ double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
return dValue1;
}
-CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr) {
- while (*pStr && *pStr == L' ')
- pStr++;
-
- return pStr;
-}
-
-CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr) {
- const FX_WCHAR* p = pStr;
- while (*p)
- p++;
- while (p > pStr && *(p - 1) == L' ')
- p--;
-
- return CFX_WideString(pStr, p - pStr);
-}
-
-CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr) {
- return StrRTrim(StrLTrim(pStr).c_str());
-}
-
-CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr) {
- while (*pStr && *pStr == ' ')
- pStr++;
-
- return pStr;
-}
-
-CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr) {
- const FX_CHAR* p = pStr;
- while (*p)
- p++;
- while (p > pStr && *(p - 1) == L' ')
- p--;
-
- return CFX_ByteString(pStr, p - pStr);
-}
-
-CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
- return StrRTrim(StrLTrim(pStr));
-}
-
CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
CJS_Value val) {
CJS_Array StrArray(pRuntime);
@@ -195,7 +170,8 @@ CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
while (*p) {
const char* pTemp = strchr(p, ch);
if (!pTemp) {
- StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(p).c_str()));
+ StrArray.SetElement(nIndex,
+ CJS_Value(pRuntime, StrTrim(CFX_ByteString(p))));
break;
}
@@ -203,7 +179,8 @@ CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
strncpy(pSub, p, pTemp - p);
*(pSub + (pTemp - p)) = '\0';
- StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(pSub).c_str()));
+ StrArray.SetElement(nIndex,
+ CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub))));
delete[] pSub;
nIndex++;
@@ -937,7 +914,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(
if (pEvent->WillCommit()) {
CFX_WideString wstrChange = w_strChange;
- CFX_WideString wstrValue = StrLTrim(w_strValue.c_str());
+ CFX_WideString wstrValue = StrTrim(w_strValue);
if (wstrValue.IsEmpty())
return TRUE;
@@ -1176,7 +1153,7 @@ FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
return TRUE;
}
-double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) {
+double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
std::vector<CFX_WideString> wsArray;
CFX_WideString sTemp = L"";
for (int i = 0; i < strValue.GetLength(); ++i) {