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