blob: 09f765312bfec0b65ecd162fefbc43e3fba7028f [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
Henrique Nakashimab9776c72017-06-23 15:03:50 -04002// 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 Zhang740b1312021-04-29 18:08:12 +00009#include <ostream>
Lei Zhangb6992dd2019-02-05 23:30:20 +000010
Lei Zhang423d99b2021-03-30 21:11:54 +000011#include "core/fxcrt/cfx_datetime.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000012#include "core/fxcrt/fx_string.h"
Lei Zhang45829202021-04-16 16:42:11 +000013#include "third_party/base/check_op.h"
Lei Zhang91d9d4e2023-08-01 00:39:46 +000014#include "third_party/base/containers/span.h"
Henrique Nakashimab9776c72017-06-23 15:03:50 -040015
16std::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 Zhangb6992dd2019-02-05 23:30:20 +000024
25std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
26 std::vector<std::string> result;
27 size_t pos = 0;
Anton Bikineev7ac13342022-01-24 21:25:15 +000028 while (true) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000029 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
40std::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
46std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
47 if (!wstr)
asweintraubd405ea82019-06-07 18:44:14 +000048 return std::wstring();
Lei Zhangb6992dd2019-02-05 23:30:20 +000049
50 size_t characters = 0;
51 while (wstr[characters])
52 ++characters;
53
Tom Sepezc4d647a2022-05-17 20:44:40 +000054 std::wstring platform_string;
55 platform_string.reserve(characters);
56 for (size_t i = 0; i < characters; ++i) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000057 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
Tom Sepezc4d647a2022-05-17 20:44:40 +000058 platform_string.push_back(ptr[0] + 256 * ptr[1]);
Lei Zhangb6992dd2019-02-05 23:30:20 +000059 }
60 return platform_string;
61}
62
Lei Zhangf0f67682019-04-08 17:03:21 +000063ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhange0023992022-03-16 03:19:31 +000064 size_t length = sizeof(uint16_t) * (wstr.size() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000065 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Lei Zhang3419af42019-04-05 22:13:13 +000066 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
67 length);
Lei Zhangb6992dd2019-02-05 23:30:20 +000068 size_t i = 0;
69 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000070 result_span[i++] = w & 0xff;
71 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000072 }
Lei Zhang3419af42019-04-05 22:13:13 +000073 result_span[i++] = 0;
74 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000075 return result;
76}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000077
78std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
Lei Zhang212493e2023-08-01 20:15:26 +000079 DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0u);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000080 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
81}