PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfString.h
1// SPDX-FileCopyrightText: 2006 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_STRING_H
6#define PDF_STRING_H
7
8#include "PdfBaseDataTypes.h"
9
10namespace PoDoFo {
11
20class PODOFO_API PdfString final : private PdfDataMember, public PdfDataProvider<PdfString>
21{
22public:
24 PdfString();
25
27
28 ~PdfString();
29
30 template<std::size_t N>
31 PdfString(const char(&str)[N])
32 : PdfDataMember(PdfDataType::String), m_isHex(false)
33 {
34 initFromUtf8String(str, N - 1, true);
35 }
36
37 template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
38 PdfString(T str)
39 : PdfDataMember(PdfDataType::String), m_isHex(false)
40 {
41 initFromUtf8String(str, std::char_traits<char>::length(str), false);
42 }
43
48 PdfString(const std::string_view& view);
49 PdfString(const std::string& str);
50
53 PdfString(std::string&& str);
54
57 PdfString(const PdfString& rhs);
58
59 PdfString(PdfString&& rhs) noexcept;
60
65 static PdfString FromRaw(const bufferview& view, bool hex = true);
66
70 static PdfString FromHexData(const std::string_view& hexView, const PdfStatefulEncrypt* encrypt = { });
71
79 inline bool IsHex() const { return m_isHex; }
80
83 PdfStringCharset GetCharset() const;
84
85 bool IsEmpty() const;
86
88 bool IsStringEvaluated() const;
89
99 std::string_view GetString() const;
100
101 std::string_view GetRawData() const;
102
104 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
105
109 PdfString& operator=(const PdfString& rhs);
110 PdfString& operator=(PdfString&& rhs) noexcept;
111
120 bool operator==(const PdfString& rhs) const;
121 bool operator==(const char* str) const;
122 bool operator==(const std::string& str) const;
123 bool operator==(const std::string_view& view) const;
124
128 bool operator!=(const PdfString& rhs) const;
129 bool operator!=(const char* str) const;
130 bool operator!=(const std::string& str) const;
131 bool operator!=(const std::string_view& view) const;
132
134 operator std::string_view() const;
135
136private:
137 // Delete constructor with nullptr
138 PdfString(std::nullptr_t) = delete;
139
146 void initFromUtf8String(const char* str, size_t length, bool literal);
147 void ensureCharsEvaluated() const;
148 void moveFrom(PdfString&& rhs);
149
150private:
151 struct StringData
152 {
153 StringData(charbuff&& buff, bool stringEvaluated);
154
155 charbuff Chars;
156 bool StringEvaluated;
157 };
158
159private:
160 bool m_dataAllocated;
161 bool m_isHex; // This string is converted to hex during writing it out
162 union
163 {
164 std::string_view m_Utf8View;
165 std::shared_ptr<StringData> m_data;
166 };
167};
168
169// Comparator to enable heterogeneous lookup in
170// PdfDictionary with both PdfString and string_view
171// See https://stackoverflow.com/a/31924435/213871
172struct PODOFO_API PdfStringInequality
173{
174 using is_transparent = std::true_type;
175
176 inline bool operator()(const PdfString& lhs, const PdfString& rhs) const
177 {
178 return lhs.GetString() < rhs.GetString();
179 }
180 inline bool operator()(const PdfString& lhs, const std::string_view& rhs) const
181 {
182 return lhs.GetString() < rhs;
183 }
184 bool operator()(const std::string_view& lhs, const PdfString& rhs) const
185 {
186 return lhs < rhs.GetString();
187 }
188};
189
190struct PODOFO_API PdfStringHashing
191{
192 using is_transparent = std::true_type;
193
194 inline std::size_t operator()(const std::string_view& str) const
195 {
196 return std::hash<std::string_view>()(str);
197 }
198 inline std::size_t operator()(const PdfString& str) const
199 {
200 return std::hash<std::string_view>()(str);
201 }
202};
203
204struct PODOFO_API PdfStringEquality
205{
206 using is_transparent = std::true_type;
207
208 inline bool operator()(const PdfString& lhs, const PdfString& rhs) const
209 {
210 return lhs.GetString() == rhs.GetString();
211 }
212 inline bool operator()(const PdfString& lhs, const std::string_view& rhs) const
213 {
214 return lhs.GetString() == rhs;
215 }
216 inline bool operator()(const std::string_view& lhs, const PdfString& rhs) const
217 {
218 return lhs == rhs.GetString();
219 }
220};
221
222template<typename TValue>
223using PdfStringMap = std::map<PdfString, TValue, PdfStringInequality>;
224
225template<typename TValue>
226using PdfStringHashMap = std::unordered_map<PdfString, TValue, PdfStringHashing, PdfStringEquality>;
227
228}
229
230#endif // PDF_STRING_H
An interface for writing blocks of data to a data source.
Definition OutputStream.h:15
A class to inherit for classes that are stored as union members in a PdfVariant.
Definition PdfBaseDataTypes.h:18
An helper class to inherit to provide common serialization methods.
Definition PdfBaseDataTypes.h:36
A string that can be written to a PDF document.
Definition PdfString.h:21
bool IsHex() const
Check if this is a hex string.
Definition PdfString.h:79
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:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfStringCharset
Definition PdfDeclarations.h:106
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:136
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:15