• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
operator <<(std::ostream & os,const CFX_DateTime & dt)18 std::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 
StringSplit(const std::string & str,char delimiter)27 std::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 
GetPlatformString(FPDF_WIDESTRING wstr)42 std::string GetPlatformString(FPDF_WIDESTRING wstr) {
43   WideString wide_string = WideStringFromFPDFWideString(wstr);
44   return std::string(wide_string.ToUTF8().c_str());
45 }
46 
GetPlatformWString(FPDF_WIDESTRING wstr)47 std::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 
GetFPDFWideString(const std::wstring & wstr)64 ScopedFPDFWideString GetFPDFWideString(const std::wstring& wstr) {
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 
GetFPDFWideStringBuffer(size_t length_bytes)82 std::vector<FPDF_WCHAR> GetFPDFWideStringBuffer(size_t length_bytes) {
83   DCHECK_EQ(length_bytes % sizeof(FPDF_WCHAR), 0u);
84   return std::vector<FPDF_WCHAR>(length_bytes / sizeof(FPDF_WCHAR));
85 }
86