blob: 363fa40d39c6ee248fafa5c31729e958e822f450 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
Henrique Nakashimab9776c72017-06-23 15:03:50 -04002// 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 Zhang91085882024-02-22 00:49:57 +000012#include "core/fxcrt/check_op.h"
Tom Sepez590668b2024-04-09 19:44:34 +000013#include "core/fxcrt/compiler_specific.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000014#include "core/fxcrt/fx_string.h"
Lei Zhang37935952024-02-16 04:39:56 +000015#include "core/fxcrt/span.h"
Nico Weber40d92c42023-11-29 03:25:30 +000016#include "fpdfsdk/cpdfsdk_helpers.h"
Henrique Nakashimab9776c72017-06-23 15:03:50 -040017
18std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt) {
19 os << dt.GetYear() << "-" << std::to_string(dt.GetMonth()) << "-"
20 << std::to_string(dt.GetDay()) << " " << std::to_string(dt.GetHour())
21 << ":" << std::to_string(dt.GetMinute()) << ":"
22 << std::to_string(dt.GetSecond()) << "."
23 << std::to_string(dt.GetMillisecond());
24 return os;
25}
Lei Zhangb6992dd2019-02-05 23:30:20 +000026
27std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
28 std::vector<std::string> result;
29 size_t pos = 0;
Anton Bikineev7ac13342022-01-24 21:25:15 +000030 while (true) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000031 size_t found = str.find(delimiter, pos);
32 if (found == std::string::npos)
33 break;
34
35 result.push_back(str.substr(pos, found - pos));
36 pos = found + 1;
37 }
38 result.push_back(str.substr(pos));
39 return result;
40}
41
42std::string GetPlatformString(FPDF_WIDESTRING wstr) {
Nico Weber40d92c42023-11-29 03:25:30 +000043 WideString wide_string = WideStringFromFPDFWideString(wstr);
Lei Zhangb6992dd2019-02-05 23:30:20 +000044 return std::string(wide_string.ToUTF8().c_str());
45}
46
47std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
48 if (!wstr)
asweintraubd405ea82019-06-07 18:44:14 +000049 return std::wstring();
Lei Zhangb6992dd2019-02-05 23:30:20 +000050
51 size_t characters = 0;
52 while (wstr[characters])
53 ++characters;
54
Tom Sepezc4d647a2022-05-17 20:44:40 +000055 std::wstring platform_string;
56 platform_string.reserve(characters);
57 for (size_t i = 0; i < characters; ++i) {
Lei Zhangb6992dd2019-02-05 23:30:20 +000058 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
Tom Sepezc4d647a2022-05-17 20:44:40 +000059 platform_string.push_back(ptr[0] + 256 * ptr[1]);
Lei Zhangb6992dd2019-02-05 23:30:20 +000060 }
61 return platform_string;
62}
63
Lei Zhangf0f67682019-04-08 17:03:21 +000064ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
Lei Zhange0023992022-03-16 03:19:31 +000065 size_t length = sizeof(uint16_t) * (wstr.size() + 1);
Lei Zhangf0f67682019-04-08 17:03:21 +000066 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
Tom Sepez590668b2024-04-09 19:44:34 +000067
68 // SAFETY: length was argument to malloc above.
69 pdfium::span<uint8_t> result_span = UNSAFE_BUFFERS(
70 pdfium::make_span(reinterpret_cast<uint8_t*>(result.get()), length));
71
Lei Zhangb6992dd2019-02-05 23:30:20 +000072 size_t i = 0;
73 for (wchar_t w : wstr) {
Lei Zhang3419af42019-04-05 22:13:13 +000074 result_span[i++] = w & 0xff;
75 result_span[i++] = (w >> 8) & 0xff;
Lei Zhangb6992dd2019-02-05 23:30:20 +000076 }
Lei Zhang3419af42019-04-05 22:13:13 +000077 result_span[i++] = 0;
78 result_span[i] = 0;
Lei Zhangb6992dd2019-02-05 23:30:20 +000079 return result;
80}
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000081
82std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
Lei Zhang212493e2023-08-01 20:15:26 +000083 DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0u);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000084 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
85}