blob: 32ad1207f688ccf57a46232999a9e4e5b026d154 [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 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 Zhang3419af42019-04-05 22:13:13 +000014#include "third_party/base/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
54 std::wstring platform_string(characters, L'\0');
55 for (size_t i = 0; i < characters + 1; ++i) {
56 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
57 platform_string[i] = ptr[0] + 256 * ptr[1];
58 }
59 return platform_string;
60}
61
Lei Zhangf0f67682019-04-08 17:03:21 +000062ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000063 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000064 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Lei Zhang3419af42019-04-05 22:13:13 +000065 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
66 length);
Lei Zhangb6992dd2019-02-05 23:30:20 +000067 size_t i = 0;
68 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000069 result_span[i++] = w & 0xff;
70 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000071 }
Lei Zhang3419af42019-04-05 22:13:13 +000072 result_span[i++] = 0;
73 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000074 return result;
75}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000076
77std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
Lei Zhang45829202021-04-16 16:42:11 +000078 DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000079 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
80}