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/fx_string.h"
13#include "fpdfsdk/cpdfsdk_helpers.h"
14#include "third_party/base/check_op.h"
15#include "third_party/base/containers/span.h"
16
17std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt) {
18 os << dt.GetYear() << "-" << std::to_string(dt.GetMonth()) << "-"
19 << std::to_string(dt.GetDay()) << " " << std::to_string(dt.GetHour())
20 << ":" << std::to_string(dt.GetMinute()) << ":"
21 << std::to_string(dt.GetSecond()) << "."
22 << std::to_string(dt.GetMillisecond());
23 return os;
24}
25
26std::vector<std::string> StringSplit(const std::string& str, char delimiter) {
27 std::vector<std::string> result;
28 size_t pos = 0;
29 while (true) {
30 size_t found = str.find(delimiter, pos);
31 if (found == std::string::npos)
32 break;
33
34 result.push_back(str.substr(pos, found - pos));
35 pos = found + 1;
36 }
37 result.push_back(str.substr(pos));
38 return result;
39}
40
41std::string GetPlatformString(FPDF_WIDESTRING wstr) {
42 WideString wide_string = WideStringFromFPDFWideString(wstr);
43 return std::string(wide_string.ToUTF8().c_str());
44}
45
46std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) {
47 if (!wstr)
48 return std::wstring();
49
50 size_t characters = 0;
51 while (wstr[characters])
52 ++characters;
53
54 std::wstring platform_string;
55 platform_string.reserve(characters);
56 for (size_t i = 0; i < characters; ++i) {
57 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&wstr[i]);
58 platform_string.push_back(ptr[0] + 256 * ptr[1]);
59 }
60 return platform_string;
61}
62
63ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
64 size_t length = sizeof(uint16_t) * (wstr.size() + 1);
65 ScopedFPDFWideString result(static_cast<FPDF_WCHAR*>(malloc(length)));
66 pdfium::span<uint8_t> result_span(reinterpret_cast<uint8_t*>(result.get()),
67 length);
68 size_t i = 0;
69 for (wchar_t w : wstr) {
70 result_span[i++] = w & 0xff;
71 result_span[i++] = (w >> 8) & 0xff;
72 }
73 result_span[i++] = 0;
74 result_span[i] = 0;
75 return result;
76}
77
79 DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0u);
80 return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
81}
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
const char * c_str() const
Definition bytestring.h:76
ByteString ToUTF8() const
WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
std::vector< std::string > StringSplit(const std::string &str, char delimiter)
ScopedFPDFWideString GetFPDFWideString(const std::wstring &wstr)
std::vector< FPDF_WCHAR > GetFPDFWideStringBuffer(size_t length_bytes)
std::string GetPlatformString(FPDF_WIDESTRING wstr)
std::wstring GetPlatformWString(FPDF_WIDESTRING wstr)