blob: 5a7a895dac5b08d658addb9d20d078b4ed3b1fee [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/util.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhang6b37a5f2015-12-25 00:20:59 -08009#include <time.h>
10
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <string>
12#include <vector>
13
Dan Sinclaira8a28e02016-03-23 15:41:39 -040014#include "core/fxcrt/include/fx_ext.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080015#include "fpdfsdk/include/javascript/IJavaScript.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Context.h"
17#include "fpdfsdk/javascript/JS_Define.h"
18#include "fpdfsdk/javascript/JS_EventHandler.h"
19#include "fpdfsdk/javascript/JS_Object.h"
20#include "fpdfsdk/javascript/JS_Runtime.h"
21#include "fpdfsdk/javascript/JS_Value.h"
22#include "fpdfsdk/javascript/PublicMethods.h"
23#include "fpdfsdk/javascript/resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026#include <ctype.h>
27#endif
28
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029BEGIN_JS_STATIC_CONST(CJS_Util)
30END_JS_STATIC_CONST()
31
32BEGIN_JS_STATIC_PROP(CJS_Util)
33END_JS_STATIC_PROP()
34
35BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036JS_STATIC_METHOD_ENTRY(printd)
37JS_STATIC_METHOD_ENTRY(printf)
38JS_STATIC_METHOD_ENTRY(printx)
39JS_STATIC_METHOD_ENTRY(scand)
40JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041END_JS_STATIC_METHOD()
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Dan Sinclairf766ad22016-03-14 13:51:24 -040047util::~util() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049struct stru_TbConvert {
50 const FX_WCHAR* lpszJSMark;
51 const FX_WCHAR* lpszCppMark;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052};
53
54const stru_TbConvert fcTable[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 {L"mmmm", L"%B"},
56 {L"mmm", L"%b"},
57 {L"mm", L"%m"},
Dan Sinclair50cce602016-02-24 09:51:16 -050058 // "m"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 {L"dddd", L"%A"},
60 {L"ddd", L"%a"},
61 {L"dd", L"%d"},
Dan Sinclair50cce602016-02-24 09:51:16 -050062 // "d", "%w",
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 {L"yyyy", L"%Y"},
64 {L"yy", L"%y"},
65 {L"HH", L"%H"},
Dan Sinclair50cce602016-02-24 09:51:16 -050066 // "H"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 {L"hh", L"%I"},
Dan Sinclair50cce602016-02-24 09:51:16 -050068 // "h"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 {L"MM", L"%M"},
Dan Sinclair50cce602016-02-24 09:51:16 -050070 // "M"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 {L"ss", L"%S"},
Dan Sinclair50cce602016-02-24 09:51:16 -050072 // "s
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 {L"TT", L"%p"},
Dan Sinclair50cce602016-02-24 09:51:16 -050074// "t"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 {L"tt", L"%p"},
77 {L"h", L"%#I"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 {L"tt", L"%P"},
80 {L"h", L"%l"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081#endif
82};
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084#define UTIL_INT 0
85#define UTIL_DOUBLE 1
86#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088int util::ParstDataType(std::wstring* sFormat) {
89 bool bPercent = FALSE;
90 for (size_t i = 0; i < sFormat->length(); ++i) {
91 wchar_t c = (*sFormat)[i];
92 if (c == L'%') {
93 bPercent = true;
94 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070095 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 if (bPercent) {
98 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
99 c == L'u' || c == L'x' || c == L'X') {
100 return UTIL_INT;
101 }
102 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
103 return UTIL_DOUBLE;
104 }
105 if (c == L's' || c == L'S') {
106 // Map s to S since we always deal internally
107 // with wchar_t strings.
108 (*sFormat)[i] = L'S';
109 return UTIL_STRING;
110 }
111 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
Lei Zhang9559b7a2015-12-21 11:12:20 -0800112 FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 continue;
114 }
115 break;
116 }
117 }
118
119 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Tom Sepezba038bc2015-10-08 12:03:00 -0700122FX_BOOL util::printf(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800123 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 CJS_Value& vRet,
125 CFX_WideString& sError) {
126 int iSize = params.size();
127 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700128 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
130 std::vector<std::wstring> c_strConvers;
131 int iOffset = 0;
132 int iOffend = 0;
133 c_ConvChar.insert(c_ConvChar.begin(), L'S');
134 while (iOffset != -1) {
135 iOffend = c_ConvChar.find(L"%", iOffset + 1);
136 std::wstring strSub;
137 if (iOffend == -1)
138 strSub = c_ConvChar.substr(iOffset);
139 else
140 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
141 c_strConvers.push_back(strSub);
142 iOffset = iOffend;
143 }
144
145 std::wstring c_strResult;
146
147 // for(int iIndex = 1;iIndex < params.size();iIndex++)
148 std::wstring c_strFormat;
149 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
150 c_strFormat = c_strConvers[iIndex];
151 if (iIndex == 0) {
152 c_strResult = c_strFormat;
153 continue;
154 }
155
156 CFX_WideString strSegment;
157 if (iIndex >= iSize) {
158 c_strResult += c_strFormat;
159 continue;
160 }
161
162 switch (ParstDataType(&c_strFormat)) {
163 case UTIL_INT:
164 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
165 break;
166 case UTIL_DOUBLE:
167 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
168 break;
169 case UTIL_STRING:
170 strSegment.Format(c_strFormat.c_str(),
171 params[iIndex].ToCFXWideString().c_str());
172 break;
173 default:
174 strSegment.Format(L"%S", c_strFormat.c_str());
175 break;
176 }
177 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
178 }
179
180 c_strResult.erase(c_strResult.begin());
181 vRet = c_strResult.c_str();
182 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Tom Sepezba038bc2015-10-08 12:03:00 -0700185FX_BOOL util::printd(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800186 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 CJS_Value& vRet,
188 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 int iSize = params.size();
190 if (iSize < 2)
191 return FALSE;
192
Tom Sepez67fd5df2015-10-08 12:24:19 -0700193 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
194 CJS_Value p1(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 p1 = params[0];
196
197 CJS_Value p2 = params[1];
Tom Sepez67fd5df2015-10-08 12:24:19 -0700198 CJS_Date jsDate(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 if (!p2.ConvertToDate(jsDate)) {
200 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
201 return FALSE;
202 }
203
204 if (!jsDate.IsValidDate()) {
205 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
206 return FALSE;
207 }
208
Tom Sepez39bfe122015-09-17 15:25:23 -0700209 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 int nFormat = p1.ToInt();
211 CFX_WideString swResult;
212
213 switch (nFormat) {
214 case 0:
215 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(),
216 jsDate.GetMonth() + 1, jsDate.GetDay(),
217 jsDate.GetHours(), jsDate.GetMinutes(),
218 jsDate.GetSeconds());
219 break;
220 case 1:
221 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(),
222 jsDate.GetMonth() + 1, jsDate.GetDay(),
223 jsDate.GetHours(), jsDate.GetMinutes(),
224 jsDate.GetSeconds());
225 break;
226 case 2:
227 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
228 jsDate.GetMonth() + 1, jsDate.GetDay(),
229 jsDate.GetHours(), jsDate.GetMinutes(),
230 jsDate.GetSeconds());
231 break;
232 default:
233 return FALSE;
234 }
235
236 vRet = swResult.c_str();
237 return TRUE;
238 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700239 if (p1.GetType() == CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
241
242 bool bXFAPicture = false;
243 if (iSize > 2) {
244 bXFAPicture = params[2].ToBool();
245 }
246
247 if (bXFAPicture) {
248 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700249 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250
Lei Zhangb9c31972015-08-11 14:09:35 -0700251 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 int iStart = 0;
253 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700254 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
255 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
256 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 iStart = iEnd;
258 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700259 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 int iYear, iMonth, iDay, iHour, iMin, iSec;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700262 iYear = jsDate.GetYear();
263 iMonth = jsDate.GetMonth();
264 iDay = jsDate.GetDay();
265 iHour = jsDate.GetHours();
266 iMin = jsDate.GetMinutes();
267 iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700269 struct tm time = {};
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 time.tm_year = iYear - 1900;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700271 time.tm_mon = iMonth;
272 time.tm_mday = iDay;
273 time.tm_hour = iHour;
274 time.tm_min = iMin;
275 time.tm_sec = iSec;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 struct stru_TbConvertAd {
278 const FX_WCHAR* lpszJSMark;
279 int iValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700280 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 stru_TbConvertAd cTableAd[] = {
283 {L"m", iMonth + 1}, {L"d", iDay},
284 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
285 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700286 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Lei Zhangb9c31972015-08-11 14:09:35 -0700288 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 wchar_t tszValue[10];
290 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700291 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
293 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 int iStart = 0;
296 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700297 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 if (iEnd > 0) {
299 if (cFormat[iEnd - 1] == L'%') {
300 iStart = iEnd + 1;
301 continue;
302 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700303 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700304 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 iStart = iEnd;
306 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700307 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700309 CFX_WideString strFormat;
310 wchar_t buf[64] = {};
311 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
312 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700314 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 }
316 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317}
318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319void util::printd(const std::wstring& cFormat2,
320 CJS_Date jsDate,
321 bool bXFAPicture,
322 std::wstring& cPurpose) {
323 std::wstring cFormat = cFormat2;
324
325 if (bXFAPicture) {
326 return; // currently, it doesn't support XFAPicture.
327 }
328
Lei Zhangb9c31972015-08-11 14:09:35 -0700329 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 int iStart = 0;
331 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700332 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
333 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
334 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 iStart = iEnd;
336 }
337 }
338
339 int iYear, iMonth, iDay, iHour, iMin, iSec;
340 iYear = jsDate.GetYear();
341 iMonth = jsDate.GetMonth();
342 iDay = jsDate.GetDay();
343 iHour = jsDate.GetHours();
344 iMin = jsDate.GetMinutes();
345 iSec = jsDate.GetSeconds();
346
347 struct tm time = {};
348 time.tm_year = iYear - 1900;
349 time.tm_mon = iMonth;
350 time.tm_mday = iDay;
351 time.tm_hour = iHour;
352 time.tm_min = iMin;
353 time.tm_sec = iSec;
354 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
355 // CString strFormat = cppTm.Format(cFormat.c_str());
356
357 struct stru_TbConvertAd {
358 const FX_WCHAR* lpszJSMark;
359 int iValue;
360 };
361
362 stru_TbConvertAd cTableAd[] = {
363 {L"m", iMonth + 1}, {L"d", iDay},
364 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
365 {L"M", iMin}, {L"s", iSec},
366 };
367
368 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
Lei Zhangb9c31972015-08-11 14:09:35 -0700369 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 wchar_t tszValue[10];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700372 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
374 sValue.GetLength() * sizeof(wchar_t));
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 int iStart = 0;
377 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700378 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 if (iEnd > 0) {
380 if (cFormat[iEnd - 1] == L'%') {
381 iStart = iEnd + 1;
382 continue;
383 }
384 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700385 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 iStart = iEnd;
387 }
388 }
389
390 CFX_WideString strFormat;
391 wchar_t buf[64] = {};
392 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
393 cFormat = buf;
394 cPurpose = cFormat;
395}
396
Tom Sepezba038bc2015-10-08 12:03:00 -0700397FX_BOOL util::printx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800398 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 CJS_Value& vRet,
400 CFX_WideString& sError) {
401 int iSize = params.size();
402 if (iSize < 2)
403 return FALSE;
404 CFX_WideString sFormat = params[0].ToCFXWideString();
405 CFX_WideString sSource = params[1].ToCFXWideString();
406 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
407 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
408 std::string cDest;
409 printx(cFormat, cSource, cDest);
410 vRet = cDest.c_str();
411 return TRUE;
412}
413
414void util::printx(const std::string& cFormat,
415 const std::string& cSource2,
416 std::string& cPurpose) {
417 std::string cSource(cSource2);
418 if (!cPurpose.empty())
419 // cPurpose.clear();
420 cPurpose.erase();
421 int itSource = 0;
422 int iSize = cSource.size();
423 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
424 iIndex++) {
425 char letter = cFormat[iIndex];
426 switch (letter) {
427 case '?':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 cPurpose += cSource[itSource];
429 itSource++;
430 break;
431 case 'X': {
432 while (itSource < iSize) {
Dan Sinclair10cfea12015-11-16 13:09:00 -0500433 if (std::isdigit(cSource[itSource]) ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
435 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700436 cPurpose += cSource[itSource];
437 itSource++;
438 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 }
440 itSource++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700441 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 break;
443 } break;
444 case 'A': {
445 while (itSource < iSize) {
446 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
447 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 cPurpose += cSource[itSource];
449 itSource++;
450 break;
451 }
452 itSource++;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700453 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 break;
455 } break;
456 case '9': {
457 while (itSource < iSize) {
Dan Sinclair10cfea12015-11-16 13:09:00 -0500458 if (std::isdigit(cSource[itSource])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 cPurpose += cSource[itSource];
460 itSource++;
461 break;
462 }
463 itSource++;
464 }
465 break;
466 }
467 case '*': {
468 cPurpose.append(cSource, itSource, iSize - itSource);
469 itSource = iSize - 1;
470 break;
471 }
472 case '\\':
473 break;
474 case '>': {
Lei Zhangcd2bb302015-12-22 13:49:44 -0800475 for (char& c : cSource)
476 c = toupper(c);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 break;
478 }
479 case '<': {
Lei Zhangcd2bb302015-12-22 13:49:44 -0800480 for (char& c : cSource)
481 c = tolower(c);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 break;
483 }
484 case '=':
485 break;
486 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 cPurpose += letter;
488 break;
489 }
490 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491}
492
Tom Sepezba038bc2015-10-08 12:03:00 -0700493FX_BOOL util::scand(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800494 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CJS_Value& vRet,
496 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 int iSize = params.size();
498 if (iSize < 2)
499 return FALSE;
500
501 CFX_WideString sFormat = params[0].ToCFXWideString();
502 CFX_WideString sDate = params[1].ToCFXWideString();
503 double dDate = JS_GetDateTime();
504 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800505 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 }
507
508 if (!JS_PortIsNan(dDate)) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700509 vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 } else {
511 vRet.SetNull();
512 }
513
514 return TRUE;
515}
516
Tom Sepezba038bc2015-10-08 12:03:00 -0700517FX_BOOL util::byteToChar(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800518 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700519 CJS_Value& vRet,
520 CFX_WideString& sError) {
521 int iSize = params.size();
522 if (iSize == 0)
523 return FALSE;
524 int nByte = params[0].ToInt();
525 unsigned char cByte = (unsigned char)nByte;
526 CFX_WideString csValue;
527 csValue.Format(L"%c", cByte);
528 vRet = csValue.c_str();
529 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700530}