blob: 6a48c09a08128ee6571b8ba9cb410019a02d8033 [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
Tom Sepez37458412015-10-06 11:33:46 -07007#include "util.h"
8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../../include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070010#include "JS_Context.h"
11#include "JS_Define.h"
12#include "JS_EventHandler.h"
13#include "JS_Object.h"
14#include "JS_Runtime.h"
15#include "JS_Value.h"
16#include "PublicMethods.h"
17#include "resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020#include <ctype.h>
21#endif
22
Tom Sepezba038bc2015-10-08 12:03:00 -070023static v8::Isolate* GetIsolate(IJS_Context* cc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 CJS_Context* pContext = (CJS_Context*)cc;
25 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
28 ASSERT(pRuntime != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 return pRuntime->GetIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
33BEGIN_JS_STATIC_CONST(CJS_Util)
34END_JS_STATIC_CONST()
35
36BEGIN_JS_STATIC_PROP(CJS_Util)
37END_JS_STATIC_PROP()
38
39BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040JS_STATIC_METHOD_ENTRY(printd)
41JS_STATIC_METHOD_ENTRY(printf)
42JS_STATIC_METHOD_ENTRY(printx)
43JS_STATIC_METHOD_ENTRY(scand)
44JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045END_JS_STATIC_METHOD()
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051util::~util() {
52}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054struct stru_TbConvert {
55 const FX_WCHAR* lpszJSMark;
56 const FX_WCHAR* lpszCppMark;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057};
58
59const stru_TbConvert fcTable[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 {L"mmmm", L"%B"},
61 {L"mmm", L"%b"},
62 {L"mm", L"%m"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070063 //"m"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 {L"dddd", L"%A"},
65 {L"ddd", L"%a"},
66 {L"dd", L"%d"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070067 //"d", "%w",
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 {L"yyyy", L"%Y"},
69 {L"yy", L"%y"},
70 {L"HH", L"%H"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070071 //"H"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 {L"hh", L"%I"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070073 //"h"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 {L"MM", L"%M"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070075 //"M"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 {L"ss", L"%S"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070077 //"s
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 {L"TT", L"%p"},
79//"t"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 {L"tt", L"%p"},
82 {L"h", L"%#I"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 {L"tt", L"%P"},
85 {L"h", L"%l"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086#endif
87};
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089#define UTIL_INT 0
90#define UTIL_DOUBLE 1
91#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093int util::ParstDataType(std::wstring* sFormat) {
94 bool bPercent = FALSE;
95 for (size_t i = 0; i < sFormat->length(); ++i) {
96 wchar_t c = (*sFormat)[i];
97 if (c == L'%') {
98 bPercent = true;
99 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700100 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (bPercent) {
103 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
104 c == L'u' || c == L'x' || c == L'X') {
105 return UTIL_INT;
106 }
107 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
108 return UTIL_DOUBLE;
109 }
110 if (c == L's' || c == L'S') {
111 // Map s to S since we always deal internally
112 // with wchar_t strings.
113 (*sFormat)[i] = L'S';
114 return UTIL_STRING;
115 }
116 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
117 CJS_PublicMethods::IsDigit(c)) {
118 continue;
119 }
120 break;
121 }
122 }
123
124 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Tom Sepezba038bc2015-10-08 12:03:00 -0700127FX_BOOL util::printf(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 const CJS_Parameters& params,
129 CJS_Value& vRet,
130 CFX_WideString& sError) {
131 int iSize = params.size();
132 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700133 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
135 std::vector<std::wstring> c_strConvers;
136 int iOffset = 0;
137 int iOffend = 0;
138 c_ConvChar.insert(c_ConvChar.begin(), L'S');
139 while (iOffset != -1) {
140 iOffend = c_ConvChar.find(L"%", iOffset + 1);
141 std::wstring strSub;
142 if (iOffend == -1)
143 strSub = c_ConvChar.substr(iOffset);
144 else
145 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
146 c_strConvers.push_back(strSub);
147 iOffset = iOffend;
148 }
149
150 std::wstring c_strResult;
151
152 // for(int iIndex = 1;iIndex < params.size();iIndex++)
153 std::wstring c_strFormat;
154 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
155 c_strFormat = c_strConvers[iIndex];
156 if (iIndex == 0) {
157 c_strResult = c_strFormat;
158 continue;
159 }
160
161 CFX_WideString strSegment;
162 if (iIndex >= iSize) {
163 c_strResult += c_strFormat;
164 continue;
165 }
166
167 switch (ParstDataType(&c_strFormat)) {
168 case UTIL_INT:
169 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
170 break;
171 case UTIL_DOUBLE:
172 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
173 break;
174 case UTIL_STRING:
175 strSegment.Format(c_strFormat.c_str(),
176 params[iIndex].ToCFXWideString().c_str());
177 break;
178 default:
179 strSegment.Format(L"%S", c_strFormat.c_str());
180 break;
181 }
182 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
183 }
184
185 c_strResult.erase(c_strResult.begin());
186 vRet = c_strResult.c_str();
187 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188}
189
Tom Sepezba038bc2015-10-08 12:03:00 -0700190FX_BOOL util::printd(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 const CJS_Parameters& params,
192 CJS_Value& vRet,
193 CFX_WideString& sError) {
194 v8::Isolate* isolate = GetIsolate(cc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 int iSize = params.size();
197 if (iSize < 2)
198 return FALSE;
199
200 CJS_Value p1(isolate);
201 p1 = params[0];
202
203 CJS_Value p2 = params[1];
204 CJS_Date jsDate(isolate);
205 if (!p2.ConvertToDate(jsDate)) {
206 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
207 return FALSE;
208 }
209
210 if (!jsDate.IsValidDate()) {
211 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
212 return FALSE;
213 }
214
Tom Sepez39bfe122015-09-17 15:25:23 -0700215 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 int nFormat = p1.ToInt();
217 CFX_WideString swResult;
218
219 switch (nFormat) {
220 case 0:
221 swResult.Format(L"D:%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 1:
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 case 2:
233 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
234 jsDate.GetMonth() + 1, jsDate.GetDay(),
235 jsDate.GetHours(), jsDate.GetMinutes(),
236 jsDate.GetSeconds());
237 break;
238 default:
239 return FALSE;
240 }
241
242 vRet = swResult.c_str();
243 return TRUE;
244 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700245 if (p1.GetType() == CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
247
248 bool bXFAPicture = false;
249 if (iSize > 2) {
250 bXFAPicture = params[2].ToBool();
251 }
252
253 if (bXFAPicture) {
254 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700255 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
Lei Zhangb9c31972015-08-11 14:09:35 -0700257 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 int iStart = 0;
259 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700260 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
261 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
262 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 iStart = iEnd;
264 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700265 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 int iYear, iMonth, iDay, iHour, iMin, iSec;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700268 iYear = jsDate.GetYear();
269 iMonth = jsDate.GetMonth();
270 iDay = jsDate.GetDay();
271 iHour = jsDate.GetHours();
272 iMin = jsDate.GetMinutes();
273 iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700275 struct tm time = {};
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 time.tm_year = iYear - 1900;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700277 time.tm_mon = iMonth;
278 time.tm_mday = iDay;
279 time.tm_hour = iHour;
280 time.tm_min = iMin;
281 time.tm_sec = iSec;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 struct stru_TbConvertAd {
284 const FX_WCHAR* lpszJSMark;
285 int iValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700286 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 stru_TbConvertAd cTableAd[] = {
289 {L"m", iMonth + 1}, {L"d", iDay},
290 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
291 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700292 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293
Lei Zhangb9c31972015-08-11 14:09:35 -0700294 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 wchar_t tszValue[10];
296 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700297 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
299 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 int iStart = 0;
302 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700303 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 if (iEnd > 0) {
305 if (cFormat[iEnd - 1] == L'%') {
306 iStart = iEnd + 1;
307 continue;
308 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700309 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700310 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 iStart = iEnd;
312 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700313 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700315 CFX_WideString strFormat;
316 wchar_t buf[64] = {};
317 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
318 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700320 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 }
322 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323}
324
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325void util::printd(const std::wstring& cFormat2,
326 CJS_Date jsDate,
327 bool bXFAPicture,
328 std::wstring& cPurpose) {
329 std::wstring cFormat = cFormat2;
330
331 if (bXFAPicture) {
332 return; // currently, it doesn't support XFAPicture.
333 }
334
Lei Zhangb9c31972015-08-11 14:09:35 -0700335 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 int iStart = 0;
337 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700338 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
339 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
340 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 iStart = iEnd;
342 }
343 }
344
345 int iYear, iMonth, iDay, iHour, iMin, iSec;
346 iYear = jsDate.GetYear();
347 iMonth = jsDate.GetMonth();
348 iDay = jsDate.GetDay();
349 iHour = jsDate.GetHours();
350 iMin = jsDate.GetMinutes();
351 iSec = jsDate.GetSeconds();
352
353 struct tm time = {};
354 time.tm_year = iYear - 1900;
355 time.tm_mon = iMonth;
356 time.tm_mday = iDay;
357 time.tm_hour = iHour;
358 time.tm_min = iMin;
359 time.tm_sec = iSec;
360 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
361 // CString strFormat = cppTm.Format(cFormat.c_str());
362
363 struct stru_TbConvertAd {
364 const FX_WCHAR* lpszJSMark;
365 int iValue;
366 };
367
368 stru_TbConvertAd cTableAd[] = {
369 {L"m", iMonth + 1}, {L"d", iDay},
370 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
371 {L"M", iMin}, {L"s", iSec},
372 };
373
374 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
Lei Zhangb9c31972015-08-11 14:09:35 -0700375 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 wchar_t tszValue[10];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700378 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
380 sValue.GetLength() * sizeof(wchar_t));
381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 int iStart = 0;
383 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700384 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 if (iEnd > 0) {
386 if (cFormat[iEnd - 1] == L'%') {
387 iStart = iEnd + 1;
388 continue;
389 }
390 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700391 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 iStart = iEnd;
393 }
394 }
395
396 CFX_WideString strFormat;
397 wchar_t buf[64] = {};
398 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
399 cFormat = buf;
400 cPurpose = cFormat;
401}
402
Tom Sepezba038bc2015-10-08 12:03:00 -0700403FX_BOOL util::printx(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404 const CJS_Parameters& params,
405 CJS_Value& vRet,
406 CFX_WideString& sError) {
407 int iSize = params.size();
408 if (iSize < 2)
409 return FALSE;
410 CFX_WideString sFormat = params[0].ToCFXWideString();
411 CFX_WideString sSource = params[1].ToCFXWideString();
412 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
413 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
414 std::string cDest;
415 printx(cFormat, cSource, cDest);
416 vRet = cDest.c_str();
417 return TRUE;
418}
419
420void util::printx(const std::string& cFormat,
421 const std::string& cSource2,
422 std::string& cPurpose) {
423 std::string cSource(cSource2);
424 if (!cPurpose.empty())
425 // cPurpose.clear();
426 cPurpose.erase();
427 int itSource = 0;
428 int iSize = cSource.size();
429 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
430 iIndex++) {
431 char letter = cFormat[iIndex];
432 switch (letter) {
433 case '?':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 cPurpose += cSource[itSource];
435 itSource++;
436 break;
437 case 'X': {
438 while (itSource < iSize) {
439 if ((cSource[itSource] >= '0' && cSource[itSource] <= '9') ||
440 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
441 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700442 cPurpose += cSource[itSource];
443 itSource++;
444 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 }
446 itSource++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700447 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 break;
449 } break;
450 case 'A': {
451 while (itSource < iSize) {
452 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
453 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 cPurpose += cSource[itSource];
455 itSource++;
456 break;
457 }
458 itSource++;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 break;
461 } break;
462 case '9': {
463 while (itSource < iSize) {
464 if (cSource[itSource] >= '0' && cSource[itSource] <= '9') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 cPurpose += cSource[itSource];
466 itSource++;
467 break;
468 }
469 itSource++;
470 }
471 break;
472 }
473 case '*': {
474 cPurpose.append(cSource, itSource, iSize - itSource);
475 itSource = iSize - 1;
476 break;
477 }
478 case '\\':
479 break;
480 case '>': {
481 for (std::string::iterator it = cSource.begin(); it != cSource.end();
482 it++) {
483 *it = toupper(*it);
484 }
485 break;
486 }
487 case '<': {
488 for (std::string::iterator it = cSource.begin(); it != cSource.end();
489 it++) {
490 *it = tolower(*it);
491 }
492 break;
493 }
494 case '=':
495 break;
496 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 cPurpose += letter;
498 break;
499 }
500 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700501}
502
Tom Sepezba038bc2015-10-08 12:03:00 -0700503FX_BOOL util::scand(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 const CJS_Parameters& params,
505 CJS_Value& vRet,
506 CFX_WideString& sError) {
507 v8::Isolate* isolate = GetIsolate(cc);
508 int iSize = params.size();
509 if (iSize < 2)
510 return FALSE;
511
512 CFX_WideString sFormat = params[0].ToCFXWideString();
513 CFX_WideString sDate = params[1].ToCFXWideString();
514 double dDate = JS_GetDateTime();
515 if (sDate.GetLength() > 0) {
516 FX_BOOL bWrongFormat = FALSE;
517 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, bWrongFormat);
518 }
519
520 if (!JS_PortIsNan(dDate)) {
521 CJS_Date date(isolate, dDate);
522 vRet = date;
523 } else {
524 vRet.SetNull();
525 }
526
527 return TRUE;
528}
529
530int64_t FX_atoi64(const char* nptr) {
531 int c; /* current char */
532 int64_t total; /* current total */
533 int sign; /* if '-', then negative, otherwise positive */
534
535 /* skip whitespace */
536 while (isspace((int)(unsigned char)*nptr))
537 ++nptr;
538
539 c = (int)(unsigned char)*nptr++;
540 sign = c; /* save sign indication */
541 if (c == '-' || c == '+')
542 c = (int)(unsigned char)*nptr++; /* skip sign */
543
544 total = 0;
545
546 while (isdigit(c)) {
547 total = 10 * total + (c - '0'); /* accumulate digit */
548 c = (int)(unsigned char)*nptr++; /* get next char */
549 }
550
551 return sign == '-' ? -total : total;
552}
553
Tom Sepezba038bc2015-10-08 12:03:00 -0700554FX_BOOL util::byteToChar(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 const CJS_Parameters& params,
556 CJS_Value& vRet,
557 CFX_WideString& sError) {
558 int iSize = params.size();
559 if (iSize == 0)
560 return FALSE;
561 int nByte = params[0].ToInt();
562 unsigned char cByte = (unsigned char)nByte;
563 CFX_WideString csValue;
564 csValue.Format(L"%c", cByte);
565 vRet = csValue.c_str();
566 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700567}