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