PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfName.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_NAME_H
6#define PDF_NAME_H
7
8#include <algorithm>
9#include "PdfBaseDataTypes.h"
10
11namespace PoDoFo {
12
21class PODOFO_API PdfName final : private PdfDataMember, public PdfDataProvider<PdfName>
22{
23public:
25 static const PdfName Null;
26
27public:
30 PdfName();
31
32 ~PdfName();
33
34 template<std::size_t N>
35 PdfName(const char(&str)[N])
36 : PdfDataMember(PdfDataType::Name)
37 {
38 initFromUtf8String(str, N - 1);
39 }
40
42 template<std::size_t N>
43 PdfName(char(&str)[N])
44 : PdfDataMember(PdfDataType::Name)
45 {
46 initFromUtf8String(str, (size_t)(std::find(str, str + N, '\0') - str));
47 }
48
49 template<typename T, typename = std::enable_if_t<std::is_same_v<T, const char*>>>
50 PdfName(T str)
52 {
53 initFromUtf8String(str, std::char_traits<char>::length(str));
54 }
55
62 PdfName(const std::string_view& str);
63 PdfName(const std::string& str);
64 PdfName(charbuff&& buff);
65
70 PdfName(const char& str, size_t length);
71
74 PdfName(const PdfName& rhs);
75 PdfName(PdfName&& rhs) noexcept;
76
77 static PdfName FromRaw(const bufferview& rawcontent);
78
84 static PdfName FromEscaped(const std::string_view& name);
85
91 std::string GetEscapedName() const;
92
93 void Write(OutputStream& stream, PdfWriteFlags writeMode,
94 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
95
98 std::string_view GetString() const;
99
101 bool IsNull() const;
102
104 std::string_view GetRawData() const;
105
108 PdfName& operator=(const PdfName& rhs);
109 PdfName& operator=(PdfName&& rhs) noexcept;
110
113 bool operator==(const PdfName& rhs) const;
114 bool operator==(const char* str) const;
115 bool operator==(const std::string& str) const;
116 bool operator==(const std::string_view& view) const;
117
120 bool operator!=(const PdfName& 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 operator std::string_view() const;
129
130private:
131 // Delete constructor with nullptr
132 PdfName(std::nullptr_t) = delete;
133
134 void expandUtf8String();
138 void initFromUtf8String(const char* str, size_t length);
139
142 void initFromUtf8String(const std::string_view& view);
143 void moveFrom(PdfName&& rhs);
144
145private:
146 struct NameData
147 {
148 // The unescaped name raw data, without leading '/'.
149 // It can store also the utf8 expanded string, if coincident
150 charbuff Chars;
151 std::unique_ptr<std::string> Utf8String;
152 bool IsUtf8Expanded;
153 };
154private:
155 bool m_dataAllocated;
156 union
157 {
158 std::shared_ptr<NameData> m_data;
159 std::string_view m_Utf8View; // Holds only global read-only string literal
160 };
161};
162
166inline PdfName operator""_n(const char* name, size_t length)
167{
168 return PdfName(*name, length);
169}
170
171// Comparator to enable heterogeneous lookup in
172// PdfDictionary with both PdfName and string_view
173// See https://stackoverflow.com/a/31924435/213871
174struct PODOFO_API PdfNameInequality
175{
176 using is_transparent = std::true_type;
177
178 bool operator()(const PdfName& lhs, const PdfName& rhs) const
179 {
180 return lhs.GetRawData() < rhs.GetRawData();
181 }
182 bool operator()(const PdfName& lhs, const std::string_view& rhs) const
183 {
184 return lhs.GetRawData() < rhs;
185 }
186 bool operator()(const std::string_view& lhs, const PdfName& rhs) const
187 {
188 return lhs < rhs.GetRawData();
189 }
190};
191
192struct PODOFO_API PdfNameHashing
193{
194 using is_transparent = std::true_type;
195
196 inline std::size_t operator()(const std::string_view& name) const
197 {
198 return std::hash<std::string_view>()(name);
199 }
200 inline std::size_t operator()(const PdfName& name) const
201 {
202 return std::hash<std::string_view>()(name);
203 }
204};
205
206struct PODOFO_API PdfNameEquality
207{
208 using is_transparent = std::true_type;
209
210 inline bool operator()(const PdfName& lhs, const PdfName& rhs) const
211 {
212 return lhs.GetRawData() == rhs.GetRawData();
213 }
214 inline bool operator()(const PdfName& lhs, const std::string_view& rhs) const
215 {
216 return lhs.GetRawData() == rhs;
217 }
218 inline bool operator()(const std::string_view& lhs, const PdfName& rhs) const
219 {
220 return lhs == rhs.GetRawData();
221 }
222};
223
224template<typename TValue>
225using PdfNameMap = std::map<PdfName, TValue, PdfNameInequality>;
226
227template<typename TValue>
228using PdfNameHashMap = std::unordered_map<PdfName, TValue, PdfNameHashing, PdfNameEquality>;
229
230};
231
232#endif // PDF_NAME_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
This class represents a PdfName.
Definition PdfName.h:22
PdfName(const std::string_view &str)
Create a new PdfName object.
bool operator==(const PdfName &rhs) const
compare to PdfName objects.
Definition PdfName.cpp:336
PdfName()
Constructor to create null name (corresponds to "/") use PdfName::Null instead of this constructor.
Definition PdfName.cpp:25
static const PdfName Null
Null name, corresponds to "/".
Definition PdfName.h:25
std::string_view GetString() const
Definition PdfName.cpp:306
static PdfName FromEscaped(const std::string_view &name)
Create a new PdfName object from a string containing an escaped name string without the leading / .
Definition PdfName.cpp:153
bool operator!=(const PdfName &rhs) const
compare two PdfName objects.
Definition PdfName.cpp:341
bool IsNull() const
Definition PdfName.cpp:323
PdfName(char(&str)[N])
Construct a new PdfName from a character buffer, up to its NUL termination or its size.
Definition PdfName.h:43
std::string_view GetRawData() const
Definition PdfName.cpp:328
PdfName & operator=(const PdfName &rhs)
Assign another name to this object.
Definition PdfName.cpp:81
std::string GetEscapedName() const
Definition PdfName.cpp:192
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:15
PdfDataType
Every PDF datatype that can occur in a PDF file is referenced by an own enum (e.g.
Definition PdfDeclarations.h:152
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
Definition PdfDeclarations.h:158
PdfWriteFlags
Specify additional options for writing the PDF.
Definition PdfDeclarations.h:136