blob: 1c057e69b2458081c6043c6a94f64859146e9dfc [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
Lei Zhang423d99b2021-03-30 21:11:54 +000010#include "core/fxcrt/cfx_datetime.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000011#include "core/fxcrt/fx_string.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000012#include "third_party/base/check.h"
Lei Zhang3419af42019-04-05 22:13:13 +000013#include "third_party/base/span.h"
Henrique Nakashimab9776c72017-06-23 15:03:50 -040014
15std::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 Zhangb6992dd2019-02-05 23:30:20 +000023
24std::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
39std::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
45std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
46 if (!wstr)
asweintraubd405ea82019-06-07 18:44:14 +000047 return std::wstring();
Lei Zhangb6992dd2019-02-05 23:30:20 +000048
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 Zhangf0f67682019-04-08 17:03:21 +000061ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000062 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000063 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Lei Zhang3419af42019-04-05 22:13:13 +000064 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
65 length);
Lei Zhangb6992dd2019-02-05 23:30:20 +000066 size_t i = 0;
67 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000068 result_span[i++] = w & 0xff;
69 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000070 }
Lei Zhang3419af42019-04-05 22:13:13 +000071 result_span[i++] = 0;
72 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000073 return result;
74}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000075
76std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
Tom Sepez25f33d02021-01-29 01:58:51 +000077 DCHECK(length_bytes % sizeof(FPDF_WCHAR) == 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000078 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
79}