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