Henrique Nakashima | b9776c7 | 2017-06-23 15:03:50 -0400 | [diff] [blame] | 1 | // 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. |
| 4 | |
| 5 | #include "testing/fx_string_testhelpers.h" |
| 6 | |
| 7 | #include <iomanip> |
| 8 | #include <ios> |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 9 | |
Lei Zhang | 423d99b | 2021-03-30 21:11:54 +0000 | [diff] [blame] | 10 | #include "core/fxcrt/cfx_datetime.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 11 | #include "core/fxcrt/fx_string.h" |
Lei Zhang | 4582920 | 2021-04-16 16:42:11 +0000 | [diff] [blame^] | 12 | #include "third_party/base/check_op.h" |
Lei Zhang | 3419af4 | 2019-04-05 22:13:13 +0000 | [diff] [blame] | 13 | #include "third_party/base/span.h" |
Henrique Nakashima | b9776c7 | 2017-06-23 15:03:50 -0400 | [diff] [blame] | 14 | |
| 15 | std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt) { |
| 16 | os << dt.GetYear() << "-" << std::to_string(dt.GetMonth()) << "-" |
| 17 | << std::to_string(dt.GetDay()) << " " << std::to_string(dt.GetHour()) |
| 18 | << ":" << std::to_string(dt.GetMinute()) << ":" |
| 19 | << std::to_string(dt.GetSecond()) << "." |
| 20 | << std::to_string(dt.GetMillisecond()); |
| 21 | return os; |
| 22 | } |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 23 | |
| 24 | std::vector<std::string> StringSplit(const std::string& str, char delimiter) { |
| 25 | std::vector<std::string> result; |
| 26 | size_t pos = 0; |
| 27 | while (1) { |
| 28 | size_t found = str.find(delimiter, pos); |
| 29 | if (found == std::string::npos) |
| 30 | break; |
| 31 | |
| 32 | result.push_back(str.substr(pos, found - pos)); |
| 33 | pos = found + 1; |
| 34 | } |
| 35 | result.push_back(str.substr(pos)); |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | std::string GetPlatformString(FPDF_WIDESTRING wstr) { |
| 40 | WideString wide_string = |
| 41 | WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr)); |
| 42 | return std::string(wide_string.ToUTF8().c_str()); |
| 43 | } |
| 44 | |
| 45 | std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) { |
| 46 | if (!wstr) |
asweintraub | d405ea8 | 2019-06-07 18:44:14 +0000 | [diff] [blame] | 47 | return std::wstring(); |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 48 | |
| 49 | size_t characters = 0; |
| 50 | while (wstr[characters]) |
| 51 | ++characters; |
| 52 | |
| 53 | std::wstring platform_string(characters, L'\0'); |
| 54 | for (size_t i = 0; i < characters + 1; ++i) { |
| 55 | const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]); |
| 56 | platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 57 | } |
| 58 | return platform_string; |
| 59 | } |
| 60 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 61 | ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) { |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 62 | size_t length = sizeof(uint16_t) * (wstr.length() + 1); |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame] | 63 | ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length))); |
Lei Zhang | 3419af4 | 2019-04-05 22:13:13 +0000 | [diff] [blame] | 64 | pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()), |
| 65 | length); |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 66 | size_t i = 0; |
| 67 | for (wchar_t w : wstr) { |
Lei Zhang | 3419af4 | 2019-04-05 22:13:13 +0000 | [diff] [blame] | 68 | result_span[i++] = w & 0xff; |
| 69 | result_span[i++] = (w >> 8) & 0xff; |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 70 | } |
Lei Zhang | 3419af4 | 2019-04-05 22:13:13 +0000 | [diff] [blame] | 71 | result_span[i++] = 0; |
| 72 | result_span[i] = 0; |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 73 | return result; |
| 74 | } |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 75 | |
| 76 | std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) { |
Lei Zhang | 4582920 | 2021-04-16 16:42:11 +0000 | [diff] [blame^] | 77 | DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0); |
Lei Zhang | 5bf8c7f | 2019-04-08 17:50:11 +0000 | [diff] [blame] | 78 | return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR)); |
| 79 | } |