Commit 3b90f8994386f83f6ee4d264ef7e8a219533d1ab

Authored by m-holger
1 parent ab019a96

Move QPDF_Name static methods to new class Name

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