blob: 8adf38265fd5d1bc7aa6f14c523bc168d5ecf55d [file] [log] [blame]
Henrique Nakashimab9776c72017-06-23 15:03:50 -04001// 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 Zhangb6992dd2019-02-05 23:30:20 +00009
10#include "core/fxcrt/fx_string.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000011#include "third_party/base/check.h"
Lei Zhang3419af42019-04-05 22:13:13 +000012#include "third_party/base/span.h"
Henrique Nakashimab9776c72017-06-23 15:03:50 -040013
14std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt) {
15 os << dt.GetYear() << "-" << std::to_string(dt.GetMonth()) << "-"
16 << std::to_string(dt.GetDay()) << " " << std::to_string(dt.GetHour())
17 << ":" << std::to_string(dt.GetMinute()) << ":"
18 << std::to_string(dt.GetSecond()) << "."
19 << std::to_string(dt.GetMillisecond());
20 return os;
21}
Lei Zhangb6992dd2019-02-05 23:30:20 +000022
23std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
24 std::vector<std::string> result;
25 size_t pos = 0;
26 while (1) {
27 size_t found = str.find(delimiter, pos);
28 if (found == std::string::npos)
29 break;
30
31 result.push_back(str.substr(pos, found - pos));
32 pos = found + 1;
33 }
34 result.push_back(str.substr(pos));
35 return result;
36}
37
38std::string GetPlatformString(FPDF_WIDESTRING wstr) {
39 WideString wide_string =
40 WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr));
41 return std::string(wide_string.ToUTF8().c_str());
42}
43
44std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
45 if (!wstr)
asweintraubd405ea82019-06-07 18:44:14 +000046 return std::wstring();
Lei Zhangb6992dd2019-02-05 23:30:20 +000047
48 size_t characters = 0;
49 while (wstr[characters])
50 ++characters;
51
52 std::wstring platform_string(characters, L'\0');
53 for (size_t i = 0; i < characters + 1; ++i) {
54 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
55 platform_string[i] = ptr[0] + 256 * ptr[1];
56 }
57 return platform_string;
58}
59
Lei Zhangf0f67682019-04-08 17:03:21 +000060ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000061 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000062 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Lei Zhang3419af42019-04-05 22:13:13 +000063 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
64 length);
Lei Zhangb6992dd2019-02-05 23:30:20 +000065 size_t i = 0;
66 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000067 result_span[i++] = w & 0xff;
68 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000069 }
Lei Zhang3419af42019-04-05 22:13:13 +000070 result_span[i++] = 0;
71 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000072 return result;
73}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000074
75std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
Tom Sepez25f33d02021-01-29 01:58:51 +000076 DCHECK(length_bytes % sizeof(FPDF_WCHAR) == 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000077 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
78}