blob: e13bd2d73f8cc6a4dbad11e5ce7f4abd06a0f490 [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"
Lei Zhang3419af42019-04-05 22:13:13 +000011#include "third_party/base/span.h"
Henrique Nakashimab9776c72017-06-23 15:03:50 -040012
13std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt) {
14 os << dt.GetYear() << "-" << std::to_string(dt.GetMonth()) << "-"
15 << std::to_string(dt.GetDay()) << " " << std::to_string(dt.GetHour())
16 << ":" << std::to_string(dt.GetMinute()) << ":"
17 << std::to_string(dt.GetSecond()) << "."
18 << std::to_string(dt.GetMillisecond());
19 return os;
20}
Lei Zhangb6992dd2019-02-05 23:30:20 +000021
22std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
23 std::vector<std::string> result;
24 size_t pos = 0;
25 while (1) {
26 size_t found = str.find(delimiter, pos);
27 if (found == std::string::npos)
28 break;
29
30 result.push_back(str.substr(pos, found - pos));
31 pos = found + 1;
32 }
33 result.push_back(str.substr(pos));
34 return result;
35}
36
37std::string GetPlatformString(FPDF_WIDESTRING wstr) {
38 WideString wide_string =
39 WideString::FromUTF16LE(wstr, WideString::WStringLength(wstr));
40 return std::string(wide_string.ToUTF8().c_str());
41}
42
43std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
44 if (!wstr)
45 return nullptr;
46
47 size_t characters = 0;
48 while (wstr[characters])
49 ++characters;
50
51 std::wstring platform_string(characters, L'\0');
52 for (size_t i = 0; i < characters + 1; ++i) {
53 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
54 platform_string[i] = ptr[0] + 256 * ptr[1];
55 }
56 return platform_string;
57}
58
59std::unique_ptr<unsigned short, pdfium::FreeDeleter> GetFPDFWideString(
60 const std::wstring& wstr) {
61 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
62 std::unique_ptr<unsigned short, pdfium::FreeDeleter> result(
63 static_cast<unsigned short*>(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}