blob: 3967ceb0cb28d2b1fc6a10e06beae27361e2678e [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"
dsinclair64376be2016-03-31 20:03:24 -070021#include "fpdfsdk/javascript/cjs_context.h"
22#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
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 -070045#define UTIL_INT 0
46#define UTIL_DOUBLE 1
47#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
tsepez86a61dc2016-03-25 10:00:11 -070049namespace {
50
51// Map PDF-style directives to equivalent wcsftime directives. Not
52// all have direct equivalents, though.
53struct TbConvert {
54 const FX_WCHAR* lpszJSMark;
55 const FX_WCHAR* lpszCppMark;
56};
57
58// Map PDF-style directives lacking direct wcsftime directives to
59// the value with which they will be replaced.
60struct TbConvertAdditional {
61 const FX_WCHAR* lpszJSMark;
62 int iValue;
63};
64
65const TbConvert TbConvertTable[] = {
66 {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"},
67 {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"},
68 {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"},
69 {L"TT", L"%p"},
70#if defined(_WIN32)
71 {L"tt", L"%p"}, {L"h", L"%#I"},
72#else
73 {L"tt", L"%P"}, {L"h", L"%l"},
74#endif
75};
76
77int ParseDataType(std::wstring* sFormat) {
tsepez4cf55152016-11-02 14:37:54 -070078 bool bPercent = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 for (size_t i = 0; i < sFormat->length(); ++i) {
80 wchar_t c = (*sFormat)[i];
81 if (c == L'%') {
82 bPercent = true;
83 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070084 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 if (bPercent) {
87 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
88 c == L'u' || c == L'x' || c == L'X') {
89 return UTIL_INT;
90 }
91 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
92 return UTIL_DOUBLE;
93 }
94 if (c == L's' || c == L'S') {
95 // Map s to S since we always deal internally
96 // with wchar_t strings.
97 (*sFormat)[i] = L'S';
98 return UTIL_STRING;
99 }
100 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
Lei Zhang9559b7a2015-12-21 11:12:20 -0800101 FXSYS_iswdigit(c)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 continue;
103 }
104 break;
105 }
106 }
107
108 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
110
tsepez86a61dc2016-03-25 10:00:11 -0700111} // namespace
112
113util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
114
115util::~util() {}
116
tsepez4cf55152016-11-02 14:37:54 -0700117bool util::printf(IJS_Context* cc,
118 const std::vector<CJS_Value>& params,
119 CJS_Value& vRet,
120 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700121 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 int iSize = params.size();
123 if (iSize < 1)
tsepez4cf55152016-11-02 14:37:54 -0700124 return false;
tsepezb4694242016-08-15 16:44:55 -0700125 std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 std::vector<std::wstring> c_strConvers;
127 int iOffset = 0;
128 int iOffend = 0;
129 c_ConvChar.insert(c_ConvChar.begin(), L'S');
130 while (iOffset != -1) {
131 iOffend = c_ConvChar.find(L"%", iOffset + 1);
132 std::wstring strSub;
133 if (iOffend == -1)
134 strSub = c_ConvChar.substr(iOffset);
135 else
136 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
137 c_strConvers.push_back(strSub);
138 iOffset = iOffend;
139 }
140
141 std::wstring c_strResult;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 std::wstring c_strFormat;
143 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
144 c_strFormat = c_strConvers[iIndex];
145 if (iIndex == 0) {
146 c_strResult = c_strFormat;
147 continue;
148 }
149
150 CFX_WideString strSegment;
151 if (iIndex >= iSize) {
152 c_strResult += c_strFormat;
153 continue;
154 }
155
tsepez86a61dc2016-03-25 10:00:11 -0700156 switch (ParseDataType(&c_strFormat)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 case UTIL_INT:
tsepezb4694242016-08-15 16:44:55 -0700158 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 break;
160 case UTIL_DOUBLE:
tsepezf3dc8c62016-08-10 06:29:29 -0700161 strSegment.Format(c_strFormat.c_str(),
tsepezb4694242016-08-15 16:44:55 -0700162 params[iIndex].ToDouble(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 break;
164 case UTIL_STRING:
tsepezb4694242016-08-15 16:44:55 -0700165 strSegment.Format(c_strFormat.c_str(),
166 params[iIndex].ToCFXWideString(pRuntime).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 break;
168 default:
169 strSegment.Format(L"%S", c_strFormat.c_str());
170 break;
171 }
172 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
173 }
174
175 c_strResult.erase(c_strResult.begin());
tsepezf3dc8c62016-08-10 06:29:29 -0700176 vRet = CJS_Value(pRuntime, c_strResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700177 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
tsepez4cf55152016-11-02 14:37:54 -0700180bool util::printd(IJS_Context* cc,
181 const std::vector<CJS_Value>& params,
182 CJS_Value& vRet,
183 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 int iSize = params.size();
185 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700186 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187
Tom Sepez67fd5df2015-10-08 12:24:19 -0700188 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepez86a61dc2016-03-25 10:00:11 -0700189 CJS_Value p1 = params[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 CJS_Value p2 = params[1];
tsepezf3c88322016-08-09 07:30:38 -0700191 CJS_Date jsDate;
tsepezb4694242016-08-15 16:44:55 -0700192 if (!p2.ConvertToDate(pRuntime, jsDate)) {
tsepezcd5dc852016-09-08 11:23:24 -0700193 sError = JSGetStringFromID(IDS_STRING_JSPRINT1);
tsepez4cf55152016-11-02 14:37:54 -0700194 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 }
196
tsepezb4694242016-08-15 16:44:55 -0700197 if (!jsDate.IsValidDate(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700198 sError = JSGetStringFromID(IDS_STRING_JSPRINT2);
tsepez4cf55152016-11-02 14:37:54 -0700199 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 }
201
Tom Sepez39bfe122015-09-17 15:25:23 -0700202 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 CFX_WideString swResult;
tsepezb4694242016-08-15 16:44:55 -0700204 switch (p1.ToInt(pRuntime)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 case 0:
tsepezb4694242016-08-15 16:44:55 -0700206 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime),
207 jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime),
208 jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime),
209 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 break;
211 case 1:
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 case 2:
tsepezf3c88322016-08-09 07:30:38 -0700219 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
tsepezb4694242016-08-15 16:44:55 -0700220 jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1,
221 jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime),
222 jsDate.GetMinutes(pRuntime),
223 jsDate.GetSeconds(pRuntime));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 break;
225 default:
tsepezcd5dc852016-09-08 11:23:24 -0700226 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700227 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 }
229
tsepezf3dc8c62016-08-10 06:29:29 -0700230 vRet = CJS_Value(pRuntime, swResult.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700231 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 }
tsepez86a61dc2016-03-25 10:00:11 -0700233
Tom Sepez39bfe122015-09-17 15:25:23 -0700234 if (p1.GetType() == CJS_Value::VT_string) {
tsepezb4694242016-08-15 16:44:55 -0700235 if (iSize > 2 && params[2].ToBool(pRuntime)) {
tsepezcd5dc852016-09-08 11:23:24 -0700236 sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT);
tsepez4cf55152016-11-02 14:37:54 -0700237 return false; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700238 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239
tsepez86a61dc2016-03-25 10:00:11 -0700240 // Convert PDF-style format specifiers to wcsftime specifiers. Remove any
241 // pre-existing %-directives before inserting our own.
tsepezb4694242016-08-15 16:44:55 -0700242 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str();
tsepez86a61dc2016-03-25 10:00:11 -0700243 cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'),
244 cFormat.end());
245
246 for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 int iStart = 0;
248 int iEnd;
tsepez86a61dc2016-03-25 10:00:11 -0700249 while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) !=
250 -1) {
251 cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark),
252 TbConvertTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 iStart = iEnd;
254 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700255 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
tsepezb4694242016-08-15 16:44:55 -0700257 int iYear = jsDate.GetYear(pRuntime);
258 int iMonth = jsDate.GetMonth(pRuntime);
259 int iDay = jsDate.GetDay(pRuntime);
260 int iHour = jsDate.GetHours(pRuntime);
261 int iMin = jsDate.GetMinutes(pRuntime);
262 int iSec = jsDate.GetSeconds(pRuntime);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
tsepez86a61dc2016-03-25 10:00:11 -0700264 TbConvertAdditional cTableAd[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 {L"m", iMonth + 1}, {L"d", iDay},
266 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
267 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700268 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
tsepez86a61dc2016-03-25 10:00:11 -0700270 for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) {
271 wchar_t tszValue[16];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700273 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
275 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 int iStart = 0;
278 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700279 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (iEnd > 0) {
281 if (cFormat[iEnd - 1] == L'%') {
282 iStart = iEnd + 1;
283 continue;
284 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700285 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700286 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 iStart = iEnd;
288 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700289 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
tsepez86a61dc2016-03-25 10:00:11 -0700291 struct tm time = {};
292 time.tm_year = iYear - 1900;
293 time.tm_mon = iMonth;
294 time.tm_mday = iDay;
295 time.tm_hour = iHour;
296 time.tm_min = iMin;
297 time.tm_sec = iSec;
298
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700299 wchar_t buf[64] = {};
tsepez86a61dc2016-03-25 10:00:11 -0700300 wcsftime(buf, 64, cFormat.c_str(), &time);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700301 cFormat = buf;
tsepezf3dc8c62016-08-10 06:29:29 -0700302 vRet = CJS_Value(pRuntime, cFormat.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700303 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 }
tsepez86a61dc2016-03-25 10:00:11 -0700305
tsepezcd5dc852016-09-08 11:23:24 -0700306 sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700307 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
tsepez4cf55152016-11-02 14:37:54 -0700310bool util::printx(IJS_Context* cc,
311 const std::vector<CJS_Value>& params,
312 CJS_Value& vRet,
313 CFX_WideString& sError) {
tsepez4f1f41f2016-03-28 14:13:16 -0700314 if (params.size() < 2) {
tsepezcd5dc852016-09-08 11:23:24 -0700315 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700316 return false;
tsepez4f1f41f2016-03-28 14:13:16 -0700317 }
tsepezf3dc8c62016-08-10 06:29:29 -0700318
tsepezcd5dc852016-09-08 11:23:24 -0700319 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700320 vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime),
321 params[1].ToCFXWideString(pRuntime))
322 .c_str());
tsepezf3dc8c62016-08-10 06:29:29 -0700323
tsepez4cf55152016-11-02 14:37:54 -0700324 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325}
326
tsepez4f1f41f2016-03-28 14:13:16 -0700327enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
328
329static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) {
330 if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
331 return input | 0x20;
332 if (eMode == kUpperCase && input >= 'a' && input <= 'z')
333 return input & ~0x20;
334 return input;
335}
336
337CFX_WideString util::printx(const CFX_WideString& wsFormat,
338 const CFX_WideString& wsSource) {
339 CFX_WideString wsResult;
340 FX_STRSIZE iSourceIdx = 0;
341 FX_STRSIZE iFormatIdx = 0;
342 CaseMode eCaseMode = kPreserveCase;
343 bool bEscaped = false;
344 while (iFormatIdx < wsFormat.GetLength()) {
345 if (bEscaped) {
346 bEscaped = false;
347 wsResult += wsFormat[iFormatIdx];
348 ++iFormatIdx;
349 continue;
350 }
351 switch (wsFormat[iFormatIdx]) {
352 case '\\': {
353 bEscaped = true;
354 ++iFormatIdx;
355 } break;
356 case '<': {
357 eCaseMode = kLowerCase;
358 ++iFormatIdx;
359 } break;
360 case '>': {
361 eCaseMode = kUpperCase;
362 ++iFormatIdx;
363 } break;
364 case '=': {
365 eCaseMode = kPreserveCase;
366 ++iFormatIdx;
367 } break;
368 case '?': {
369 if (iSourceIdx < wsSource.GetLength()) {
370 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
371 ++iSourceIdx;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700372 }
tsepez4f1f41f2016-03-28 14:13:16 -0700373 ++iFormatIdx;
374 } break;
375 case 'X': {
376 if (iSourceIdx < wsSource.GetLength()) {
377 if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
378 (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
379 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
380 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
381 ++iFormatIdx;
382 }
383 ++iSourceIdx;
384 } else {
385 ++iFormatIdx;
386 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 } break;
388 case 'A': {
tsepez4f1f41f2016-03-28 14:13:16 -0700389 if (iSourceIdx < wsSource.GetLength()) {
390 if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
391 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
392 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
393 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 }
tsepez4f1f41f2016-03-28 14:13:16 -0700395 ++iSourceIdx;
396 } else {
397 ++iFormatIdx;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 } break;
400 case '9': {
tsepez4f1f41f2016-03-28 14:13:16 -0700401 if (iSourceIdx < wsSource.GetLength()) {
402 if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
403 wsResult += wsSource[iSourceIdx];
404 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 }
tsepez4f1f41f2016-03-28 14:13:16 -0700406 ++iSourceIdx;
407 } else {
408 ++iFormatIdx;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 }
tsepez4f1f41f2016-03-28 14:13:16 -0700410 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 case '*': {
tsepez4f1f41f2016-03-28 14:13:16 -0700412 if (iSourceIdx < wsSource.GetLength()) {
413 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
414 ++iSourceIdx;
415 } else {
416 ++iFormatIdx;
417 }
418 } break;
419 default: {
420 wsResult += wsFormat[iFormatIdx];
421 ++iFormatIdx;
422 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 }
424 }
tsepez4f1f41f2016-03-28 14:13:16 -0700425 return wsResult;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426}
427
tsepez4cf55152016-11-02 14:37:54 -0700428bool util::scand(IJS_Context* cc,
429 const std::vector<CJS_Value>& params,
430 CJS_Value& vRet,
431 CFX_WideString& sError) {
tsepezf3dc8c62016-08-10 06:29:29 -0700432 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 int iSize = params.size();
434 if (iSize < 2)
tsepez4cf55152016-11-02 14:37:54 -0700435 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436
tsepezb4694242016-08-15 16:44:55 -0700437 CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime);
438 CFX_WideString sDate = params[1].ToCFXWideString(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 double dDate = JS_GetDateTime();
440 if (sDate.GetLength() > 0) {
Lei Zhang9559b7a2015-12-21 11:12:20 -0800441 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 }
443
444 if (!JS_PortIsNan(dDate)) {
tsepezb4694242016-08-15 16:44:55 -0700445 vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 } else {
tsepezf3dc8c62016-08-10 06:29:29 -0700447 vRet.SetNull(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 }
449
tsepez4cf55152016-11-02 14:37:54 -0700450 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451}
452
tsepez4cf55152016-11-02 14:37:54 -0700453bool util::byteToChar(IJS_Context* cc,
454 const std::vector<CJS_Value>& params,
455 CJS_Value& vRet,
456 CFX_WideString& sError) {
tsepez90d87792016-03-29 09:21:54 -0700457 if (params.size() < 1) {
tsepezcd5dc852016-09-08 11:23:24 -0700458 sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
tsepez4cf55152016-11-02 14:37:54 -0700459 return false;
tsepez90d87792016-03-29 09:21:54 -0700460 }
tsepezf3dc8c62016-08-10 06:29:29 -0700461
tsepezcd5dc852016-09-08 11:23:24 -0700462 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
tsepezb4694242016-08-15 16:44:55 -0700463 int arg = params[0].ToInt(pRuntime);
tsepez90d87792016-03-29 09:21:54 -0700464 if (arg < 0 || arg > 255) {
tsepezcd5dc852016-09-08 11:23:24 -0700465 sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
tsepez4cf55152016-11-02 14:37:54 -0700466 return false;
tsepez90d87792016-03-29 09:21:54 -0700467 }
tsepezf3dc8c62016-08-10 06:29:29 -0700468
tsepez90d87792016-03-29 09:21:54 -0700469 CFX_WideString wStr(static_cast<FX_WCHAR>(arg));
tsepezf3dc8c62016-08-10 06:29:29 -0700470 vRet = CJS_Value(pRuntime, wStr.c_str());
tsepez4cf55152016-11-02 14:37:54 -0700471 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472}