PoDoFo  1.0.0-dev
PdfString.h
1 
7 #ifndef PDF_STRING_H
8 #define PDF_STRING_H
9 
10 #include "PdfBaseDataTypes.h"
11 
12 namespace PoDoFo {
13 
23 class PODOFO_API PdfString final : private PdfDataMember, public PdfDataProvider<PdfString>
24 {
25 public:
28  PdfString();
29 
30  PdfString(charbuff&& buff, bool isHex);
31 
32  ~PdfString();
33 
34  template<std::size_t N>
35  PdfString(const char(&str)[N])
36  : PdfDataMember(PdfDataType::String), m_isHex(false)
37  {
38  initFromUtf8String(str, N - 1, true);
39  }
40 
41  template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
42  PdfString(T str)
43  : PdfDataMember(PdfDataType::String), m_isHex(false)
44  {
45  initFromUtf8String(str, std::char_traits<char>::length(str), false);
46  }
47 
53  PdfString(const std::string_view& view);
54  PdfString(const std::string& str);
55 
59  PdfString(std::string&& str);
60 
64  PdfString(const PdfString& rhs);
65 
66  PdfString(PdfString&& rhs) noexcept;
67 
73  static PdfString FromRaw(const bufferview& view, bool hex = true);
74 
80  static PdfString FromHexData(const std::string_view& hexView, const PdfStatefulEncrypt* encrypt = { });
81 
90  inline bool IsHex() const { return m_isHex; }
91 
96  PdfStringCharset GetCharset() const;
97 
98  bool IsEmpty() const;
99 
103  bool IsStringEvaluated() const;
104 
115  std::string_view GetString() const;
116 
117  std::string_view GetRawData() const;
118 
119  void Write(OutputStream& stream, PdfWriteFlags writeMode,
120  const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
121 
126  PdfString& operator=(const PdfString& rhs);
127  PdfString& operator=(PdfString&& rhs) noexcept;
128 
138  bool operator==(const PdfString& rhs) const;
139  bool operator==(const char* str) const;
140  bool operator==(const std::string& str) const;
141  bool operator==(const std::string_view& view) const;
142 
147  bool operator!=(const PdfString& rhs) const;
148  bool operator!=(const char* str) const;
149  bool operator!=(const std::string& str) const;
150  bool operator!=(const std::string_view& view) const;
151 
154  operator std::string_view() const;
155 
156 private:
157  // Delete constructor with nullptr
158  PdfString(std::nullptr_t) = delete;
159 
168  void initFromUtf8String(const char* str, size_t length, bool literal);
169  void ensureCharsEvaluated() const;
170  void moveFrom(PdfString&& rhs);
171 
172 private:
173  struct StringData
174  {
175  StringData(charbuff&& buff, bool stringEvaluated);
176 
177  charbuff Chars;
178  bool StringEvaluated;
179  };
180 
181 private:
182  bool m_dataAllocated;
183  bool m_isHex; // This string is converted to hex during writing it out
184  union
185  {
186  std::string_view m_Utf8View;
187  std::shared_ptr<StringData> m_data;
188  };
189 };
190 
191 // Comparator to enable heterogeneous lookup in
192 // PdfDictionary with both PdfString and string_view
193 // See https://stackoverflow.com/a/31924435/213871
194 struct PODOFO_API PdfStringInequality
195 {
196  using is_transparent = std::true_type;
197 
198  inline bool operator()(const PdfString& lhs, const PdfString& rhs) const
199  {
200  return lhs.GetString() < rhs.GetString();
201  }
202  inline bool operator()(const PdfString& lhs, const std::string_view& rhs) const
203  {
204  return lhs.GetString() < rhs;
205  }
206  bool operator()(const std::string_view& lhs, const PdfString& rhs) const
207  {
208  return lhs < rhs.GetString();
209  }
210 };
211 
212 struct PODOFO_API PdfStringHashing
213 {
214  using is_transparent = std::true_type;
215 
216  inline std::size_t operator()(const std::string_view& str) const
217  {
218  return std::hash<std::string_view>()(str);
219  }
220  inline std::size_t operator()(const PdfString& str) const
221  {
222  return std::hash<std::string_view>()(str);
223  }
224 };
225 
226 struct PODOFO_API PdfStringEquality
227 {
228  using is_transparent = std::true_type;
229 
230  inline bool operator()(const PdfString& lhs, const PdfString& rhs) const
231  {
232  return lhs.GetString() == rhs.GetString();
233  }
234  inline bool operator()(const PdfString& lhs, const std::string_view& rhs) const
235  {
236  return lhs.GetString() == rhs;
237  }
238  inline bool operator()(const std::string_view& lhs, const PdfString& rhs) const
239  {
240  return lhs == rhs.GetString();
241  }
242 };
243 
244 template<typename TValue>
245 using PdfStringMap = std::map<PdfString, TValue, PdfStringInequality>;
246 
247 template<typename TValue>
248 using PdfStringHashMap = std::unordered_map<PdfString, TValue, PdfStringHashing, PdfStringEquality>;
249 
250 }
251 
252 #endif // PDF_STRING_H
An interface for writing blocks of data to a data source.
Definition: OutputStream.h:18
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition: PdfBaseDataTypes.h:21
An helper class to inherit to provide common serialization methods.
Definition: PdfBaseDataTypes.h:40
A string that can be written to a PDF document.
Definition: PdfString.h:24
bool IsHex() const
Check if this is a hex string.
Definition: PdfString.h:90
PdfString(const std::string_view &view)
Construct a new PdfString from a utf-8 string The input string will be copied.
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition: basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
@ String
String datatype in PDF file. Strings have the form (Hallo World!) in PDF files.
PdfStringCharset
Definition: PdfDeclarations.h:105
PdfWriteFlags
Specify additional options for writing the PDF.
Definition: PdfDeclarations.h:137
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19