Commit 3b90f8994386f83f6ee4d264ef7e8a219533d1ab

Authored by m-holger
1 parent ab019a96

Move QPDF_Name static methods to new class Name

libqpdf/ContentNormalizer.cc
1 #include <qpdf/ContentNormalizer.hh> 1 #include <qpdf/ContentNormalizer.hh>
2 2
3 -#include <qpdf/QPDF_Name.hh> 3 +#include <qpdf/QPDFObjectHandle_private.hh>
4 #include <qpdf/QUtil.hh> 4 #include <qpdf/QUtil.hh>
5 5
  6 +using namespace qpdf;
  7 +
6 ContentNormalizer::ContentNormalizer() : 8 ContentNormalizer::ContentNormalizer() :
7 any_bad_tokens(false), 9 any_bad_tokens(false),
8 last_token_was_bad(false) 10 last_token_was_bad(false)
@@ -55,7 +57,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token) @@ -55,7 +57,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token)
55 break; 57 break;
56 58
57 case QPDFTokenizer::tt_name: 59 case QPDFTokenizer::tt_name:
58 - write(QPDF_Name::normalizeName(token.getValue())); 60 + write(Name::normalize(token.getValue()));
59 break; 61 break;
60 62
61 default: 63 default:
libqpdf/QPDFWriter.cc
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 #include <qpdf/Pl_StdioFile.hh> 15 #include <qpdf/Pl_StdioFile.hh>
16 #include <qpdf/QIntC.hh> 16 #include <qpdf/QIntC.hh>
17 #include <qpdf/QPDF.hh> 17 #include <qpdf/QPDF.hh>
18 -#include <qpdf/QPDFObjectHandle.hh> 18 +#include <qpdf/QPDFObjectHandle_private.hh>
19 #include <qpdf/QPDF_Name.hh> 19 #include <qpdf/QPDF_Name.hh>
20 #include <qpdf/QPDF_String.hh> 20 #include <qpdf/QPDF_String.hh>
21 #include <qpdf/QTC.hh> 21 #include <qpdf/QTC.hh>
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 #include <stdexcept> 27 #include <stdexcept>
28 28
29 using namespace std::literals; 29 using namespace std::literals;
  30 +using namespace qpdf;
30 31
31 QPDFWriter::ProgressReporter::~ProgressReporter() // NOLINT (modernize-use-equals-default) 32 QPDFWriter::ProgressReporter::~ProgressReporter() // NOLINT (modernize-use-equals-default)
32 { 33 {
@@ -1176,7 +1177,7 @@ QPDFWriter::writeTrailer( @@ -1176,7 +1177,7 @@ QPDFWriter::writeTrailer(
1176 for (auto const& key: trailer.getKeys()) { 1177 for (auto const& key: trailer.getKeys()) {
1177 writeStringQDF(" "); 1178 writeStringQDF(" ");
1178 writeStringNoQDF(" "); 1179 writeStringNoQDF(" ");
1179 - writeString(QPDF_Name::normalizeName(key)); 1180 + writeString(Name::normalize(key));
1180 writeString(" "); 1181 writeString(" ");
1181 if (key == "/Size") { 1182 if (key == "/Size") {
1182 writeString(std::to_string(size)); 1183 writeString(std::to_string(size));
@@ -1503,7 +1504,7 @@ QPDFWriter::unparseObject( @@ -1503,7 +1504,7 @@ QPDFWriter::unparseObject(
1503 auto const& key = item.first; 1504 auto const& key = item.first;
1504 writeString(indent); 1505 writeString(indent);
1505 writeStringQDF(" "); 1506 writeStringQDF(" ");
1506 - writeString(QPDF_Name::normalizeName(key)); 1507 + writeString(Name::normalize(key));
1507 writeString(" "); 1508 writeString(" ");
1508 if (key == "/Contents" && object.isDictionaryOfType("/Sig") && 1509 if (key == "/Contents" && object.isDictionaryOfType("/Sig") &&
1509 object.hasKey("/ByteRange")) { 1510 object.hasKey("/ByteRange")) {
libqpdf/QPDF_Dictionary.cc
@@ -63,7 +63,7 @@ QPDF_Dictionary::unparse() @@ -63,7 +63,7 @@ QPDF_Dictionary::unparse()
63 std::string result = "<< "; 63 std::string result = "<< ";
64 for (auto& iter: this->items) { 64 for (auto& iter: this->items) {
65 if (!iter.second.isNull()) { 65 if (!iter.second.isNull()) {
66 - result += QPDF_Name::normalizeName(iter.first) + " " + iter.second.unparse() + " "; 66 + result += Name::normalize(iter.first) + " " + iter.second.unparse() + " ";
67 } 67 }
68 } 68 }
69 result += ">>"; 69 result += ">>";
@@ -78,17 +78,15 @@ QPDF_Dictionary::writeJSON(int json_version, JSON::Writer&amp; p) @@ -78,17 +78,15 @@ QPDF_Dictionary::writeJSON(int json_version, JSON::Writer&amp; p)
78 if (!iter.second.isNull()) { 78 if (!iter.second.isNull()) {
79 p.writeNext(); 79 p.writeNext();
80 if (json_version == 1) { 80 if (json_version == 1) {
81 - p << "\"" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first))  
82 - << "\": ";  
83 - } else if (auto res = QPDF_Name::analyzeJSONEncoding(iter.first); res.first) { 81 + p << "\"" << JSON::Writer::encode_string(Name::normalize(iter.first)) << "\": ";
  82 + } else if (auto res = Name::analyzeJSONEncoding(iter.first); res.first) {
84 if (res.second) { 83 if (res.second) {
85 p << "\"" << iter.first << "\": "; 84 p << "\"" << iter.first << "\": ";
86 } else { 85 } else {
87 p << "\"" << JSON::Writer::encode_string(iter.first) << "\": "; 86 p << "\"" << JSON::Writer::encode_string(iter.first) << "\": ";
88 } 87 }
89 } else { 88 } else {
90 - p << "\"n:" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first))  
91 - << "\": "; 89 + p << "\"n:" << JSON::Writer::encode_string(Name::normalize(iter.first)) << "\": ";
92 } 90 }
93 iter.second.writeJSON(json_version, p); 91 iter.second.writeJSON(json_version, p);
94 } 92 }
libqpdf/QPDF_Name.cc
1 #include <qpdf/QPDF_Name.hh> 1 #include <qpdf/QPDF_Name.hh>
2 2
3 #include <qpdf/JSON_writer.hh> 3 #include <qpdf/JSON_writer.hh>
  4 +#include <qpdf/QPDFObjectHandle_private.hh>
4 #include <qpdf/QUtil.hh> 5 #include <qpdf/QUtil.hh>
5 6
  7 +using namespace qpdf;
  8 +
6 QPDF_Name::QPDF_Name(std::string const& name) : 9 QPDF_Name::QPDF_Name(std::string const& name) :
7 QPDFValue(::ot_name), 10 QPDFValue(::ot_name),
8 name(name) 11 name(name)
@@ -22,7 +25,7 @@ QPDF_Name::copy(bool shallow) @@ -22,7 +25,7 @@ QPDF_Name::copy(bool shallow)
22 } 25 }
23 26
24 std::string 27 std::string
25 -QPDF_Name::normalizeName(std::string const& name) 28 +Name::normalize(std::string const& name)
26 { 29 {
27 if (name.empty()) { 30 if (name.empty()) {
28 return name; 31 return name;
@@ -49,11 +52,11 @@ QPDF_Name::normalizeName(std::string const&amp; name) @@ -49,11 +52,11 @@ QPDF_Name::normalizeName(std::string const&amp; name)
49 std::string 52 std::string
50 QPDF_Name::unparse() 53 QPDF_Name::unparse()
51 { 54 {
52 - return normalizeName(this->name); 55 + return Name::normalize(name);
53 } 56 }
54 57
55 std::pair<bool, bool> 58 std::pair<bool, bool>
56 -QPDF_Name::analyzeJSONEncoding(const std::string& name) 59 +Name::analyzeJSONEncoding(const std::string& name)
57 { 60 {
58 int tail = 0; // Number of continuation characters expected. 61 int tail = 0; // Number of continuation characters expected.
59 bool tail2 = false; // Potential overlong 3 octet utf-8. 62 bool tail2 = false; // Potential overlong 3 octet utf-8.
@@ -105,16 +108,16 @@ QPDF_Name::writeJSON(int json_version, JSON::Writer&amp; p) @@ -105,16 +108,16 @@ QPDF_Name::writeJSON(int json_version, JSON::Writer&amp; p)
105 // For performance reasons this code is duplicated in QPDF_Dictionary::writeJSON. When updating 108 // For performance reasons this code is duplicated in QPDF_Dictionary::writeJSON. When updating
106 // this method make sure QPDF_Dictionary is also update. 109 // this method make sure QPDF_Dictionary is also update.
107 if (json_version == 1) { 110 if (json_version == 1) {
108 - p << "\"" << JSON::Writer::encode_string(normalizeName(name)) << "\""; 111 + p << "\"" << JSON::Writer::encode_string(Name::normalize(name)) << "\"";
109 } else { 112 } else {
110 - if (auto res = analyzeJSONEncoding(name); res.first) { 113 + if (auto res = Name::analyzeJSONEncoding(name); res.first) {
111 if (res.second) { 114 if (res.second) {
112 p << "\"" << name << "\""; 115 p << "\"" << name << "\"";
113 } else { 116 } else {
114 p << "\"" << JSON::Writer::encode_string(name) << "\""; 117 p << "\"" << JSON::Writer::encode_string(name) << "\"";
115 } 118 }
116 } else { 119 } else {
117 - p << "\"n:" << JSON::Writer::encode_string(normalizeName(name)) << "\""; 120 + p << "\"n:" << JSON::Writer::encode_string(Name::normalize(name)) << "\"";
118 } 121 }
119 } 122 }
120 } 123 }
libqpdf/qpdf/QPDFObjectHandle_private.hh
@@ -81,6 +81,18 @@ namespace qpdf @@ -81,6 +81,18 @@ namespace qpdf
81 } 81 }
82 }; 82 };
83 83
  84 + class Name final: public BaseHandle
  85 + {
  86 + public:
  87 + // Put # into strings with characters unsuitable for name token
  88 + static std::string normalize(std::string const& name);
  89 +
  90 + // Check whether name is valid utf-8 and whether it contains characters that require
  91 + // escaping. Return {false, false} if the name is not valid utf-8, otherwise return {true,
  92 + // true} if no characters require or {true, false} if escaping is required.
  93 + static std::pair<bool, bool> analyzeJSONEncoding(std::string const& name);
  94 + };
  95 +
84 class Stream final: public BaseHandle 96 class Stream final: public BaseHandle
85 { 97 {
86 public: 98 public:
libqpdf/qpdf/QPDF_Name.hh
@@ -11,14 +11,6 @@ class QPDF_Name: public QPDFValue @@ -11,14 +11,6 @@ class QPDF_Name: public QPDFValue
11 std::shared_ptr<QPDFObject> copy(bool shallow = false) override; 11 std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
12 std::string unparse() override; 12 std::string unparse() override;
13 void writeJSON(int json_version, JSON::Writer& p) override; 13 void writeJSON(int json_version, JSON::Writer& p) override;
14 -  
15 - // Put # into strings with characters unsuitable for name token  
16 - static std::string normalizeName(std::string const& name);  
17 -  
18 - // Check whether name is valid utf-8 and whether it contains characters that require escaping.  
19 - // Return {false, false} if the name is not valid utf-8, otherwise return {true, true} if no  
20 - // characters require or {true, false} if escaping is required.  
21 - static std::pair<bool, bool> analyzeJSONEncoding(std::string const& name);  
22 std::string 14 std::string
23 getStringValue() const override 15 getStringValue() const override
24 { 16 {