Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(707)

Side by Side Diff: fpdfsdk/javascript/PublicMethods.cpp

Issue 1882173002: Make CJS_PublicMethods::StrTrim() take a CFX_ByteString (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/javascript/PublicMethods.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "fpdfsdk/javascript/PublicMethods.h" 7 #include "fpdfsdk/javascript/PublicMethods.h"
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber) 45 JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
46 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate) 46 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
47 JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate) 47 JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
48 JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange) 48 JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
49 JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx) 49 JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
50 JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums) 50 JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
51 END_JS_STATIC_GLOBAL_FUN() 51 END_JS_STATIC_GLOBAL_FUN()
52 52
53 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods) 53 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
54 54
55 static const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr", 55 namespace {
56 L"May", L"Jun", L"Jul", L"Aug",
57 L"Sep", L"Oct", L"Nov", L"Dec"};
58 56
59 static const FX_WCHAR* const fullmonths[] = { 57 const FX_WCHAR* const months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
60 L"January", L"February", L"March", L"April", 58 L"May", L"Jun", L"Jul", L"Aug",
61 L"May", L"June", L"July", L"August", 59 L"Sep", L"Oct", L"Nov", L"Dec"};
62 L"September", L"October", L"November", L"December"};
63 60
64 bool CJS_PublicMethods::IsNumber(const FX_WCHAR* str) { 61 const FX_WCHAR* const fullmonths[] = {L"January", L"February", L"March",
62 L"April", L"May", L"June",
63 L"July", L"August", L"September",
64 L"October", L"November", L"December"};
65
66 CFX_ByteString StrTrim(const CFX_ByteString& pStr) {
67 CFX_ByteString result(pStr);
68 result.TrimLeft(' ');
69 result.TrimRight(' ');
70 return result;
71 }
72
73 CFX_WideString StrTrim(const CFX_WideString& pStr) {
74 CFX_WideString result(pStr);
75 result.TrimLeft(' ');
76 result.TrimRight(' ');
77 return result;
78 }
79
80 } // namespace
81
82 bool CJS_PublicMethods::IsNumber(const CFX_WideString& str) {
65 CFX_WideString sTrim = StrTrim(str); 83 CFX_WideString sTrim = StrTrim(str);
66 const FX_WCHAR* pTrim = sTrim.c_str(); 84 const FX_WCHAR* pTrim = sTrim.c_str();
67 const FX_WCHAR* p = pTrim; 85 const FX_WCHAR* p = pTrim;
68
69 bool bDot = false; 86 bool bDot = false;
70 bool bKXJS = false; 87 bool bKXJS = false;
71 88
72 wchar_t c; 89 wchar_t c;
73 while ((c = *p) != L'\0') { 90 while ((c = *p) != L'\0') {
74 if (c == L'.' || c == L',') { 91 if (c == L'.' || c == L',') {
75 if (bDot) 92 if (bDot)
76 return false; 93 return false;
77 bDot = true; 94 bDot = true;
78 } else if (c == L'-' || c == L'+') { 95 } else if (c == L'-' || c == L'+') {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 146 }
130 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) { 147 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
131 return std::min(dValue1, dValue2); 148 return std::min(dValue1, dValue2);
132 } 149 }
133 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) { 150 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
134 return std::max(dValue1, dValue2); 151 return std::max(dValue1, dValue2);
135 } 152 }
136 return dValue1; 153 return dValue1;
137 } 154 }
138 155
139 CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr) {
140 while (*pStr && *pStr == L' ')
141 pStr++;
142
143 return pStr;
144 }
145
146 CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr) {
147 const FX_WCHAR* p = pStr;
148 while (*p)
149 p++;
150 while (p > pStr && *(p - 1) == L' ')
151 p--;
152
153 return CFX_WideString(pStr, p - pStr);
154 }
155
156 CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr) {
157 return StrRTrim(StrLTrim(pStr).c_str());
158 }
159
160 CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr) {
161 while (*pStr && *pStr == ' ')
162 pStr++;
163
164 return pStr;
165 }
166
167 CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr) {
168 const FX_CHAR* p = pStr;
169 while (*p)
170 p++;
171 while (p > pStr && *(p - 1) == L' ')
172 p--;
173
174 return CFX_ByteString(pStr, p - pStr);
175 }
176
177 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
178 return StrRTrim(StrLTrim(pStr));
179 }
180
181 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime, 156 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
182 CJS_Value val) { 157 CJS_Value val) {
183 CJS_Array StrArray(pRuntime); 158 CJS_Array StrArray(pRuntime);
184 if (val.IsArrayObject()) { 159 if (val.IsArrayObject()) {
185 val.ConvertToArray(StrArray); 160 val.ConvertToArray(StrArray);
186 return StrArray; 161 return StrArray;
187 } 162 }
188 CFX_WideString wsStr = val.ToCFXWideString(); 163 CFX_WideString wsStr = val.ToCFXWideString();
189 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr); 164 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
190 const char* p = (const char*)t; 165 const char* p = (const char*)t;
191 166
192 int ch = ','; 167 int ch = ',';
193 int nIndex = 0; 168 int nIndex = 0;
194 169
195 while (*p) { 170 while (*p) {
196 const char* pTemp = strchr(p, ch); 171 const char* pTemp = strchr(p, ch);
197 if (!pTemp) { 172 if (!pTemp) {
198 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(p).c_str())); 173 StrArray.SetElement(nIndex,
174 CJS_Value(pRuntime, StrTrim(CFX_ByteString(p))));
199 break; 175 break;
200 } 176 }
201 177
202 char* pSub = new char[pTemp - p + 1]; 178 char* pSub = new char[pTemp - p + 1];
203 strncpy(pSub, p, pTemp - p); 179 strncpy(pSub, p, pTemp - p);
204 *(pSub + (pTemp - p)) = '\0'; 180 *(pSub + (pTemp - p)) = '\0';
205 181
206 StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(pSub).c_str())); 182 StrArray.SetElement(nIndex,
183 CJS_Value(pRuntime, StrTrim(CFX_ByteString(pSub))));
207 delete[] pSub; 184 delete[] pSub;
208 185
209 nIndex++; 186 nIndex++;
210 p = ++pTemp; 187 p = ++pTemp;
211 } 188 }
212 return StrArray; 189 return StrArray;
213 } 190 }
214 191
215 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str, 192 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
216 int nStart, 193 int nStart,
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 if (iSepStyle < 0 || iSepStyle > 3) 907 if (iSepStyle < 0 || iSepStyle > 3)
931 iSepStyle = 0; 908 iSepStyle = 0;
932 if (!pEvent->m_pValue) 909 if (!pEvent->m_pValue)
933 return FALSE; 910 return FALSE;
934 CFX_WideString& val = pEvent->Value(); 911 CFX_WideString& val = pEvent->Value();
935 CFX_WideString& w_strChange = pEvent->Change(); 912 CFX_WideString& w_strChange = pEvent->Change();
936 CFX_WideString w_strValue = val; 913 CFX_WideString w_strValue = val;
937 914
938 if (pEvent->WillCommit()) { 915 if (pEvent->WillCommit()) {
939 CFX_WideString wstrChange = w_strChange; 916 CFX_WideString wstrChange = w_strChange;
940 CFX_WideString wstrValue = StrLTrim(w_strValue.c_str()); 917 CFX_WideString wstrValue = StrTrim(w_strValue);
Tom Sepez 2016/04/12 19:25:33 note: trimming right is not strictly necessary but
941 if (wstrValue.IsEmpty()) 918 if (wstrValue.IsEmpty())
942 return TRUE; 919 return TRUE;
943 920
944 CFX_WideString swTemp = wstrValue; 921 CFX_WideString swTemp = wstrValue;
945 swTemp.Replace(L",", L"."); 922 swTemp.Replace(L",", L".");
946 if (!IsNumber(swTemp.c_str())) { 923 if (!IsNumber(swTemp.c_str())) {
947 pEvent->Rc() = FALSE; 924 pEvent->Rc() = FALSE;
948 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); 925 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
949 Alert(pContext, sError.c_str()); 926 Alert(pContext, sError.c_str());
950 return TRUE; 927 return TRUE;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(), 1146 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1170 sFormat.c_str()); 1147 sFormat.c_str());
1171 Alert(pContext, swMsg.c_str()); 1148 Alert(pContext, swMsg.c_str());
1172 return FALSE; 1149 return FALSE;
1173 } 1150 }
1174 1151
1175 val = MakeFormatDate(dDate, sFormat); 1152 val = MakeFormatDate(dDate, sFormat);
1176 return TRUE; 1153 return TRUE;
1177 } 1154 }
1178 1155
1179 double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) { 1156 double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) {
1180 std::vector<CFX_WideString> wsArray; 1157 std::vector<CFX_WideString> wsArray;
1181 CFX_WideString sTemp = L""; 1158 CFX_WideString sTemp = L"";
1182 for (int i = 0; i < strValue.GetLength(); ++i) { 1159 for (int i = 0; i < strValue.GetLength(); ++i) {
1183 FX_WCHAR c = strValue.GetAt(i); 1160 FX_WCHAR c = strValue.GetAt(i);
1184 if (c == L' ' || c == L':') { 1161 if (c == L' ' || c == L':') {
1185 wsArray.push_back(sTemp); 1162 wsArray.push_back(sTemp);
1186 sTemp = L""; 1163 sTemp = L"";
1187 continue; 1164 continue;
1188 } 1165 }
1189 sTemp += c; 1166 sTemp += c;
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
1867 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str())); 1844 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
1868 } 1845 }
1869 1846
1870 if (nums.GetLength() > 0) 1847 if (nums.GetLength() > 0)
1871 vRet = nums; 1848 vRet = nums;
1872 else 1849 else
1873 vRet.SetNull(); 1850 vRet.SetNull();
1874 1851
1875 return TRUE; 1852 return TRUE;
1876 } 1853 }
OLDNEW
« no previous file with comments | « fpdfsdk/javascript/PublicMethods.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698