Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_string_testhelpers.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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>
9#include <ostream>
10
11#include "core/fxcrt/cfx_datetime.h"
12#include "core/fxcrt/check_op.h"
13#include "core/fxcrt/compiler_specific.h"
14#include "core/fxcrt/fx_string.h"
15#include "core/fxcrt/span.h"
16#include "fpdfsdk/cpdfsdk_helpers.h"
17
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}
26
27std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
28 std::vector<std::string> result;
29 size_t pos = 0;
30 while (true) {
31 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) {
44 return std::string(wide_string.ToUTF8().c_str());
45}
46
47std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
48 if (!wstr)
49 return std::wstring();
50
51 size_t characters = 0;
52 while (wstr[characters])
53 ++characters;
54
55 std::wstring platform_string;
56 platform_string.reserve(characters);
57 for (size_t i = 0; i < characters; ++i) {
58 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
59 platform_string.push_back(ptr[0] + 256 * ptr[1]);
60 }
61 return platform_string;
62}
63
65 size_t length = sizeof(uint16_t) * (wstr.size() + 1);
66 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
67
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
72 size_t i = 0;
73 for (wchar_t w : wstr) {
74 result_span[i++] = w & 0xff;
75 result_span[i++] = (w >> 8) & 0xff;
76 }
77 result_span[i++] = 0;
78 result_span[i] = 0;
79 return result;
80}
81
83 DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0u);
84 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
85}
#define DCHECK_EQ(x, y)
Definition check_op.h:17
int32_t GetYear() const
uint8_t GetSecond() const
uint8_t GetHour() const
uint8_t GetDay() const
uint8_t GetMinute() const
uint16_t GetMillisecond() const
uint8_t GetMonth() const
ByteString ToUTF8() const
#define UNSAFE_BUFFERS(...)
UNSAFE_BUFFER_USAGE WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
std::unique_ptr< FPDF_WCHAR, pdfium::FreeDeleter > ScopedFPDFWideString
std::vector< FPDF_WCHAR > GetFPDFWideStringBuffer(size_t length_bytes)
std::vector< std::string > StringSplit(const std::string &str, char delimiter)
ScopedFPDFWideString GetFPDFWideString(const std::wstring &wstr)
std::string GetPlatformString(FPDF_WIDESTRING wstr)
std::wstring GetPlatformWString(FPDF_WIDESTRING wstr)
fxcrt::WideString WideString
Definition widestring.h:207