blob: 17d90cc1773e08101104ba539e6e3716b019f66e [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
Lei Zhangf0f67682019-04-08 17:03:21 +000059ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000060 size_t length = sizeof(uint16_t) * (wstr.length() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000061 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Lei Zhang3419af42019-04-05 22:13:13 +000062 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
63 length);
Lei Zhangb6992dd2019-02-05 23:30:20 +000064 size_t i = 0;
65 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000066 result_span[i++] = w & 0xff;
67 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000068 }
Lei Zhang3419af42019-04-05 22:13:13 +000069 result_span[i++] = 0;
70 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000071 return result;
72}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000073
74std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
75 ASSERT(length_bytes % sizeof(FPDF_WCHAR) == 0);
76 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
77}