OLD | NEW |
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/util.h" | 7 #include "fpdfsdk/javascript/util.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 302 |
303 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR); | 303 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSTYPEERROR); |
304 return FALSE; | 304 return FALSE; |
305 } | 305 } |
306 | 306 |
307 | 307 |
308 FX_BOOL util::printx(IJS_Context* cc, | 308 FX_BOOL util::printx(IJS_Context* cc, |
309 const std::vector<CJS_Value>& params, | 309 const std::vector<CJS_Value>& params, |
310 CJS_Value& vRet, | 310 CJS_Value& vRet, |
311 CFX_WideString& sError) { | 311 CFX_WideString& sError) { |
312 int iSize = params.size(); | 312 if (params.size() < 2) { |
313 if (iSize < 2) | 313 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); |
314 return FALSE; | 314 return FALSE; |
315 CFX_WideString sFormat = params[0].ToCFXWideString(); | 315 } |
316 CFX_WideString sSource = params[1].ToCFXWideString(); | 316 vRet = printx(params[0].ToCFXWideString(), params[1].ToCFXWideString()); |
317 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str(); | |
318 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str(); | |
319 std::string cDest; | |
320 printx(cFormat, cSource, cDest); | |
321 vRet = cDest.c_str(); | |
322 return TRUE; | 317 return TRUE; |
323 } | 318 } |
324 | 319 |
325 void util::printx(const std::string& cFormat, | 320 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
326 const std::string& cSource2, | 321 |
327 std::string& cPurpose) { | 322 static FX_WCHAR TranslateCase(FX_WCHAR input, CaseMode eMode) { |
328 std::string cSource(cSource2); | 323 if (eMode == kLowerCase && input >= 'A' && input <= 'Z') |
329 if (!cPurpose.empty()) | 324 return input | 0x20; |
330 // cPurpose.clear(); | 325 if (eMode == kUpperCase && input >= 'a' && input <= 'z') |
331 cPurpose.erase(); | 326 return input & ~0x20; |
332 int itSource = 0; | 327 return input; |
333 int iSize = cSource.size(); | 328 } |
334 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize; | 329 |
335 iIndex++) { | 330 CFX_WideString util::printx(const CFX_WideString& wsFormat, |
336 char letter = cFormat[iIndex]; | 331 const CFX_WideString& wsSource) { |
337 switch (letter) { | 332 CFX_WideString wsResult; |
338 case '?': | 333 FX_STRSIZE iSourceIdx = 0; |
339 cPurpose += cSource[itSource]; | 334 FX_STRSIZE iFormatIdx = 0; |
340 itSource++; | 335 CaseMode eCaseMode = kPreserveCase; |
341 break; | 336 bool bEscaped = false; |
| 337 while (iFormatIdx < wsFormat.GetLength()) { |
| 338 if (bEscaped) { |
| 339 bEscaped = false; |
| 340 wsResult += wsFormat[iFormatIdx]; |
| 341 ++iFormatIdx; |
| 342 continue; |
| 343 } |
| 344 switch (wsFormat[iFormatIdx]) { |
| 345 case '\\': { |
| 346 bEscaped = true; |
| 347 ++iFormatIdx; |
| 348 } break; |
| 349 case '<': { |
| 350 eCaseMode = kLowerCase; |
| 351 ++iFormatIdx; |
| 352 } break; |
| 353 case '>': { |
| 354 eCaseMode = kUpperCase; |
| 355 ++iFormatIdx; |
| 356 } break; |
| 357 case '=': { |
| 358 eCaseMode = kPreserveCase; |
| 359 ++iFormatIdx; |
| 360 } break; |
| 361 case '?': { |
| 362 if (iSourceIdx < wsSource.GetLength()) { |
| 363 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 364 ++iSourceIdx; |
| 365 } |
| 366 ++iFormatIdx; |
| 367 } break; |
342 case 'X': { | 368 case 'X': { |
343 while (itSource < iSize) { | 369 if (iSourceIdx < wsSource.GetLength()) { |
344 if (std::isdigit(cSource[itSource]) || | 370 if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') || |
345 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || | 371 (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') || |
346 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { | 372 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) { |
347 cPurpose += cSource[itSource]; | 373 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
348 itSource++; | 374 ++iFormatIdx; |
349 break; | |
350 } | 375 } |
351 itSource++; | 376 ++iSourceIdx; |
| 377 } else { |
| 378 ++iFormatIdx; |
352 } | 379 } |
353 break; | |
354 } break; | 380 } break; |
355 case 'A': { | 381 case 'A': { |
356 while (itSource < iSize) { | 382 if (iSourceIdx < wsSource.GetLength()) { |
357 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || | 383 if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') || |
358 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { | 384 (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) { |
359 cPurpose += cSource[itSource]; | 385 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
360 itSource++; | 386 ++iFormatIdx; |
361 break; | |
362 } | 387 } |
363 itSource++; | 388 ++iSourceIdx; |
| 389 } else { |
| 390 ++iFormatIdx; |
364 } | 391 } |
365 break; | |
366 } break; | 392 } break; |
367 case '9': { | 393 case '9': { |
368 while (itSource < iSize) { | 394 if (iSourceIdx < wsSource.GetLength()) { |
369 if (std::isdigit(cSource[itSource])) { | 395 if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') { |
370 cPurpose += cSource[itSource]; | 396 wsResult += wsSource[iSourceIdx]; |
371 itSource++; | 397 ++iFormatIdx; |
372 break; | |
373 } | 398 } |
374 itSource++; | 399 ++iSourceIdx; |
| 400 } else { |
| 401 ++iFormatIdx; |
375 } | 402 } |
376 break; | 403 } break; |
377 } | |
378 case '*': { | 404 case '*': { |
379 cPurpose.append(cSource, itSource, iSize - itSource); | 405 if (iSourceIdx < wsSource.GetLength()) { |
380 itSource = iSize - 1; | 406 wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
381 break; | 407 ++iSourceIdx; |
382 } | 408 } else { |
383 case '\\': | 409 ++iFormatIdx; |
384 break; | 410 } |
385 case '>': { | 411 } break; |
386 for (char& c : cSource) | 412 default: { |
387 c = toupper(c); | 413 wsResult += wsFormat[iFormatIdx]; |
388 break; | 414 ++iFormatIdx; |
389 } | 415 } break; |
390 case '<': { | |
391 for (char& c : cSource) | |
392 c = tolower(c); | |
393 break; | |
394 } | |
395 case '=': | |
396 break; | |
397 default: | |
398 cPurpose += letter; | |
399 break; | |
400 } | 416 } |
401 } | 417 } |
| 418 return wsResult; |
402 } | 419 } |
403 | 420 |
404 FX_BOOL util::scand(IJS_Context* cc, | 421 FX_BOOL util::scand(IJS_Context* cc, |
405 const std::vector<CJS_Value>& params, | 422 const std::vector<CJS_Value>& params, |
406 CJS_Value& vRet, | 423 CJS_Value& vRet, |
407 CFX_WideString& sError) { | 424 CFX_WideString& sError) { |
408 int iSize = params.size(); | 425 int iSize = params.size(); |
409 if (iSize < 2) | 426 if (iSize < 2) |
410 return FALSE; | 427 return FALSE; |
411 | 428 |
(...skipping 20 matching lines...) Expand all Loading... |
432 int iSize = params.size(); | 449 int iSize = params.size(); |
433 if (iSize == 0) | 450 if (iSize == 0) |
434 return FALSE; | 451 return FALSE; |
435 int nByte = params[0].ToInt(); | 452 int nByte = params[0].ToInt(); |
436 unsigned char cByte = (unsigned char)nByte; | 453 unsigned char cByte = (unsigned char)nByte; |
437 CFX_WideString csValue; | 454 CFX_WideString csValue; |
438 csValue.Format(L"%c", cByte); | 455 csValue.Format(L"%c", cByte); |
439 vRet = csValue.c_str(); | 456 vRet = csValue.c_str(); |
440 return TRUE; | 457 return TRUE; |
441 } | 458 } |
OLD | NEW |