blob: 3221cfb0cbb6d2b8828dc8bfeb7e14c6eff16f7e [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
tsepez86a61dc2016-03-25 10:00:11 -070011#include <algorithm>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012#include <string>
13#include <vector>
14
dsinclaira52ab742016-09-29 13:59:29 -070015#include "core/fxcrt/fx_ext.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040016#include "fpdfsdk/javascript/JS_Define.h"
17#include "fpdfsdk/javascript/JS_EventHandler.h"
18#include "fpdfsdk/javascript/JS_Object.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040019#include "fpdfsdk/javascript/JS_Value.h"
20#include "fpdfsdk/javascript/PublicMethods.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080021#include "fpdfsdk/javascript/cjs_event_context.h"
dsinclair64376be2016-03-31 20:03:24 -070022#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040023#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
Tom Sepez04557b82017-02-16 09:43:10 -080029JSConstSpec CJS_Util::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Tom Sepez04557b82017-02-16 09:43:10 -080031JSPropertySpec CJS_Util::PropertySpecs[] = {{0, 0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Tom Sepez04557b82017-02-16 09:43:10 -080033JSMethodSpec CJS_Util::MethodSpecs[] = {
Tom Sepez9b99b632017-02-21 15:05:57 -080034 {"printd", printd_static}, {"printf", printf_static},
35 {"printx", printx_static}, {"scand", scand_static},
36 {"byteToChar", byteToChar_static}, {0, 0}};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040#define UTIL_INT 0
41#define UTIL_DOUBLE 1
42#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
tsepez86a61dc2016-03-25 10:00:11 -070044namespace {
45
46// Map PDF-style directives to equivalent wcsftime directives. Not
47// all have direct equivalents, though.
48struct TbConvert {
49 const FX_WCHAR* lpszJSMark;
50 const FX_WCHAR* lpszCppMark;
51};
52
53// Map PDF-style directives lacking direct wcsftime directives to
54// the value with which they will be replaced.
55struct TbConvertAdditional {
56 const FX_WCHAR* lpszJSMark;
57 int iValue;
58};
59
60const TbConvert TbConvertTable[] = {
61 {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"},
62 {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"},
63 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"},
64 {L"TT", L"%p"},
65#if defined(_WIN32)
66 {L"tt", L"%p"}, {L"h", L"%#I"},
67#else
68 {L"tt", L"%P"}, {L"h", L"%l"},
69#endif
70};
71
72int ParseDataType(std::wstring* sFormat) {
tsepez4cf55152016-11-02 14:37:54 -070073 bool bPercent = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 for (size_t i = 0; i < sFormat->length(); ++i) {
75 wchar_t c = (*sFormat)[i];
76 if (c == L'%') {
77 bPercent = true;
78 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070079 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (bPercent) {
82 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
83 c == L'u' || c == L'x' || c == L'X') {
84 return UTIL_INT;
85 }
86 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
87 return UTIL_DOUBLE;
88 }
89 if (c == L's' || c == L'S') {
90 // Map s to S since we always deal internally
91 // with wchar_t strings.
92 (*sFormat)[i] = L'S';
93 return UTIL_STRING;
94 }
95 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
Lei Zhang9559b7a2015-12-21 11:12:20 -080096 FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 continue;
98 }
99 break;
100 }
101 }
102
103 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
tsepez86a61dc2016-03-25 10:00:11 -0700106} // namespace
107
108util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
109
110util::~util() {}
111
Tom Sepezb1670b52017-02-16 17:01:00 -0800112bool util::printf(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700113 const std::vector<CJS_Value>& params,
114 CJS_Value& vRet,
115 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 int iSize = params.size();
117 if (iSize < 1)
tsepez4cf55152016-11-02 14:37:54 -0700118 return false;
tsepezb4694242016-08-15 16:44:55 -0700119 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 std::vector<std::wstring> c_strConvers;
121 int iOffset = 0;
122 int iOffend = 0;
123 c_ConvChar.insert(c_ConvChar.begin(), L'S');
124 while (iOffset != -1) {
125 iOffend = c_ConvChar.find(L"%", iOffset + 1);
126 std::wstring strSub;
127 if (iOffend == -1)
128 strSub = c_ConvChar.substr(iOffset);
129 else
130 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
131 c_strConvers.push_back(strSub);
132 iOffset = iOffend;
133 }
134
135 std::wstring c_strResult;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 std::wstring c_strFormat;
137 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
138 c_strFormat = c_strConvers[iIndex];
139 if (iIndex == 0) {
140 c_strResult = c_strFormat;
141 continue;
142 }
143
144 CFX_WideString strSegment;
145 if (iIndex >= iSize) {
146 c_strResult += c_strFormat;
147 continue;
148 }
149
tsepez86a61dc2016-03-25 10:00:11 -0700150 switch (ParseDataType(&c_strFormat)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 case UTIL_INT:
tsepezb4694242016-08-15 16:44:55 -0700152 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 break;
154 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700155 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700156 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 break;
158 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700159 strSegment.Format(c_strFormat.c_str(),
160 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 break;
162 default:
163 strSegment.Format(L"%S", c_strFormat.c_str());
164 break;
165 }
166 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
167 }
168
169 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700170 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700171 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Tom Sepezb1670b52017-02-16 17:01:00 -0800174bool util::printd(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700175 const std::vector<CJS_Value>& params,
176 CJS_Value& vRet,
177 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 int iSize = params.size();
179 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700180 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181
tsepez86a61dc2016-03-25 10:00:11 -0700182 CJS_Value p1 = params[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CJS_Value p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700184 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700185 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700186 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700187 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 }
189
tsepezb4694242016-08-15 16:44:55 -0700190 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700191 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700192 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 }
194
Tom Sepez39bfe122015-09-17 15:25:23 -0700195 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 CFX_WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700197 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 case 0:
tsepezb4694242016-08-15 16:44:55 -0700199 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
200 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
201 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
202 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 break;
204 case 1:
tsepezf3c88322016-08-09 07:30:38 -0700205 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700206 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
207 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
208 jsDate.GetMinutes(pRuntime),
209 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 break;
211 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700212 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700213 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
214 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
215 jsDate.GetMinutes(pRuntime),
216 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 break;
218 default:
tsepezcd5dc852016-09-08 11:23:24 -0700219 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700220 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
222
tsepezf3dc8c62016-08-10 06:29:29 -0700223 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700224 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 }
tsepez86a61dc2016-03-25 10:00:11 -0700226
Tom Sepez39bfe122015-09-17 15:25:23 -0700227 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700228 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700229 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700230 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700231 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232
tsepez86a61dc2016-03-25 10:00:11 -0700233 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
234 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700235 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700236 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
237 cFormat.end());
238
239 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 int iStart = 0;
241 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700242 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
243 -1) {
244 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
245 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 iStart = iEnd;
247 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700248 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249
tsepezb4694242016-08-15 16:44:55 -0700250 int iYear = jsDate.GetYear(pRuntime);
251 int iMonth = jsDate.GetMonth(pRuntime);
252 int iDay = jsDate.GetDay(pRuntime);
253 int iHour = jsDate.GetHours(pRuntime);
254 int iMin = jsDate.GetMinutes(pRuntime);
255 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
tsepez86a61dc2016-03-25 10:00:11 -0700257 TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 {L"m", iMonth + 1}, {L"d", iDay},
259 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
260 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700261 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
tsepez86a61dc2016-03-25 10:00:11 -0700263 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
264 wchar_t tszValue[16];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700266 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
268 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 int iStart = 0;
271 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700272 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (iEnd > 0) {
274 if (cFormat[iEnd - 1] == L'%') {
275 iStart = iEnd + 1;
276 continue;
277 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700278 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700279 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 iStart = iEnd;
281 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700282 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
tsepez86a61dc2016-03-25 10:00:11 -0700284 struct tm time = {};
285 time.tm_year = iYear - 1900;
286 time.tm_mon = iMonth;
287 time.tm_mday = iDay;
288 time.tm_hour = iHour;
289 time.tm_min = iMin;
290 time.tm_sec = iSec;
291
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700292 wchar_t buf[64] = {};
tsepez86a61dc2016-03-25 10:00:11 -0700293 wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700294 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700295 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700296 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 }
tsepez86a61dc2016-03-25 10:00:11 -0700298
tsepezcd5dc852016-09-08 11:23:24 -0700299 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700300 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301}
302
Tom Sepezb1670b52017-02-16 17:01:00 -0800303bool util::printx(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700304 const std::vector<CJS_Value>& params,
305 CJS_Value& vRet,
306 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700307 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700308 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700309 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700310 }
tsepezf3dc8c62016-08-10 06:29:29 -0700311
tsepezb4694242016-08-15 16:44:55 -0700312 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
313 params[1].ToCFXWideString(pRuntime))
314 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700315
tsepez4cf55152016-11-02 14:37:54 -0700316 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317}
318
tsepez4f1f41f2016-03-28 14:13:16 -0700319enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
320
321static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) {
322 if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
323 return input | 0x20;
324 if (eMode == kUpperCase && input >= 'a' && input <= 'z')
325 return input & ~0x20;
326 return input;
327}
328
329CFX_WideString util::printx(const CFX_WideString& wsFormat,
330 const CFX_WideString& wsSource) {
331 CFX_WideString wsResult;
332 FX_STRSIZE iSourceIdx = 0;
333 FX_STRSIZE iFormatIdx = 0;
334 CaseMode eCaseMode = kPreserveCase;
335 bool bEscaped = false;
336 while (iFormatIdx < wsFormat.GetLength()) {
337 if (bEscaped) {
338 bEscaped = false;
339 wsResult += wsFormat[iFormatIdx];
340 ++iFormatIdx;
341 continue;
342 }
343 switch (wsFormat[iFormatIdx]) {
344 case '\\': {
345 bEscaped = true;
346 ++iFormatIdx;
347 } break;
348 case '<': {
349 eCaseMode = kLowerCase;
350 ++iFormatIdx;
351 } break;
352 case '>': {
353 eCaseMode = kUpperCase;
354 ++iFormatIdx;
355 } break;
356 case '=': {
357 eCaseMode = kPreserveCase;
358 ++iFormatIdx;
359 } break;
360 case '?': {
361 if (iSourceIdx < wsSource.GetLength()) {
362 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
363 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700364 }
tsepez4f1f41f2016-03-28 14:13:16 -0700365 ++iFormatIdx;
366 } break;
367 case 'X': {
368 if (iSourceIdx < wsSource.GetLength()) {
369 if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
370 (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
371 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
372 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
373 ++iFormatIdx;
374 }
375 ++iSourceIdx;
376 } else {
377 ++iFormatIdx;
378 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 } break;
380 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700381 if (iSourceIdx < wsSource.GetLength()) {
382 if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
383 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
384 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
385 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 }
tsepez4f1f41f2016-03-28 14:13:16 -0700387 ++iSourceIdx;
388 } else {
389 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 } break;
392 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700393 if (iSourceIdx < wsSource.GetLength()) {
394 if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
395 wsResult += wsSource[iSourceIdx];
396 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 }
tsepez4f1f41f2016-03-28 14:13:16 -0700398 ++iSourceIdx;
399 } else {
400 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 }
tsepez4f1f41f2016-03-28 14:13:16 -0700402 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700404 if (iSourceIdx < wsSource.GetLength()) {
405 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
406 ++iSourceIdx;
407 } else {
408 ++iFormatIdx;
409 }
410 } break;
411 default: {
412 wsResult += wsFormat[iFormatIdx];
413 ++iFormatIdx;
414 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 }
416 }
tsepez4f1f41f2016-03-28 14:13:16 -0700417 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418}
419
Tom Sepezb1670b52017-02-16 17:01:00 -0800420bool util::scand(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700421 const std::vector<CJS_Value>& params,
422 CJS_Value& vRet,
423 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 int iSize = params.size();
425 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700426 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427
tsepezb4694242016-08-15 16:44:55 -0700428 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
429 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 double dDate = JS_GetDateTime();
431 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800432 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 }
434
435 if (!JS_PortIsNan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700436 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700438 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 }
440
tsepez4cf55152016-11-02 14:37:54 -0700441 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442}
443
Tom Sepezb1670b52017-02-16 17:01:00 -0800444bool util::byteToChar(CJS_Runtime* pRuntime,
tsepez4cf55152016-11-02 14:37:54 -0700445 const std::vector<CJS_Value>& params,
446 CJS_Value& vRet,
447 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700448 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700449 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700450 return false;
tsepez90d87792016-03-29 09:21:54 -0700451 }
tsepezf3dc8c62016-08-10 06:29:29 -0700452
tsepezb4694242016-08-15 16:44:55 -0700453 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700454 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700455 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700456 return false;
tsepez90d87792016-03-29 09:21:54 -0700457 }
tsepezf3dc8c62016-08-10 06:29:29 -0700458
tsepez90d87792016-03-29 09:21:54 -0700459 CFX_WideString wStr(static_cast<FX_WCHAR>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700460 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700461 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462}