Commit 9e4660843dc21909b9cdc15e8032d6bf9e0c5bab

Authored by m-holger
1 parent 1d7ebddb

Refactor: Replace size and length checks with `.empty()`

Simplified checks for empty containers and strings across the codebase using the `.empty()` method. This improves code readability and adheres to best practices for checking emptiness.
examples/pdf-mod-info.cc
@@ -90,11 +90,11 @@ main(int argc, char* argv[]) @@ -90,11 +90,11 @@ main(int argc, char* argv[])
90 } else if ((!strcmp(argv[i], "--key")) && (++i < argc)) { 90 } else if ((!strcmp(argv[i], "--key")) && (++i < argc)) {
91 QTC::TC("examples", "pdf-mod-info -key"); 91 QTC::TC("examples", "pdf-mod-info -key");
92 cur_key = argv[i]; 92 cur_key = argv[i];
93 - if (!((cur_key.length() > 0) && (cur_key.at(0) == '/'))) { 93 + if (cur_key.empty() || cur_key.at(0) != '/') {
94 cur_key = "/" + cur_key; 94 cur_key = "/" + cur_key;
95 } 95 }
96 Keys[cur_key] = ""; 96 Keys[cur_key] = "";
97 - } else if ((!strcmp(argv[i], "--val")) && (++i < argc)) { 97 + } else if (!strcmp(argv[i], "--val") && ++i < argc) {
98 if (cur_key.empty()) { 98 if (cur_key.empty()) {
99 QTC::TC("examples", "pdf-mod-info usage wrong val"); 99 QTC::TC("examples", "pdf-mod-info usage wrong val");
100 usage(); 100 usage();
@@ -115,7 +115,7 @@ main(int argc, char* argv[]) @@ -115,7 +115,7 @@ main(int argc, char* argv[])
115 QTC::TC("examples", "pdf-mod-info in-place"); 115 QTC::TC("examples", "pdf-mod-info in-place");
116 fl_out = fl_in; 116 fl_out = fl_in;
117 } 117 }
118 - if (Keys.size() == 0) { 118 + if (Keys.empty()) {
119 QTC::TC("examples", "pdf-mod-info no keys"); 119 QTC::TC("examples", "pdf-mod-info no keys");
120 usage(); 120 usage();
121 } 121 }
@@ -141,7 +141,7 @@ main(int argc, char* argv[]) @@ -141,7 +141,7 @@ main(int argc, char* argv[])
141 filetrailer.replaceKey("/Info", fileinfo); 141 filetrailer.replaceKey("/Info", fileinfo);
142 } 142 }
143 } 143 }
144 - if (it.second == "") { 144 + if (it.second.empty()) {
145 fileinfo.removeKey(it.first); 145 fileinfo.removeKey(it.first);
146 } else { 146 } else {
147 QPDFObjectHandle elt = fileinfo.newString(it.second); 147 QPDFObjectHandle elt = fileinfo.newString(it.second);
libqpdf/QPDFAcroFormDocumentHelper.cc
@@ -675,7 +675,7 @@ QPDFAcroFormDocumentHelper::adjustAppearanceStream( @@ -675,7 +675,7 @@ QPDFAcroFormDocumentHelper::adjustAppearanceStream(
675 resources.mergeResources(merge_with, &dr_map); 675 resources.mergeResources(merge_with, &dr_map);
676 // Remove empty subdictionaries 676 // Remove empty subdictionaries
677 for (auto iter: resources.ditems()) { 677 for (auto iter: resources.ditems()) {
678 - if (iter.second.isDictionary() && (iter.second.getKeys().size() == 0)) { 678 + if (iter.second.isDictionary() && iter.second.getKeys().empty()) {
679 resources.removeKey(iter.first); 679 resources.removeKey(iter.first);
680 } 680 }
681 } 681 }
libqpdf/QPDFArgParser.cc
@@ -471,7 +471,7 @@ QPDFArgParser::parseArgs() @@ -471,7 +471,7 @@ QPDFArgParser::parseArgs()
471 // positional arguments. Besides, it doesn't make sense to have an empty option. 471 // positional arguments. Besides, it doesn't make sense to have an empty option.
472 arg_s = arg; 472 arg_s = arg;
473 size_t equal_pos = std::string::npos; 473 size_t equal_pos = std::string::npos;
474 - if (arg_s.length() > 0) { 474 + if (!arg_s.empty()) {
475 equal_pos = arg_s.find('=', 1); 475 equal_pos = arg_s.find('=', 1);
476 } 476 }
477 if (equal_pos != std::string::npos) { 477 if (equal_pos != std::string::npos) {
@@ -686,7 +686,7 @@ QPDFArgParser::addHelpTopic( @@ -686,7 +686,7 @@ QPDFArgParser::addHelpTopic(
686 QTC::TC("libtests", "QPDFArgParser add reserved help topic"); 686 QTC::TC("libtests", "QPDFArgParser add reserved help topic");
687 throw std::logic_error("QPDFArgParser: can't register reserved help topic " + topic); 687 throw std::logic_error("QPDFArgParser: can't register reserved help topic " + topic);
688 } 688 }
689 - if (!((topic.length() > 0) && (topic.at(0) != '-'))) { 689 + if (topic.empty() || topic.at(0) == '-') {
690 QTC::TC("libtests", "QPDFArgParser bad topic for help"); 690 QTC::TC("libtests", "QPDFArgParser bad topic for help");
691 throw std::logic_error("QPDFArgParser: help topics must not start with -"); 691 throw std::logic_error("QPDFArgParser: help topics must not start with -");
692 } 692 }
libqpdf/QPDFJob.cc
@@ -396,7 +396,7 @@ QPDFJob::parseRotationParameter(std::string const&amp; parameter) @@ -396,7 +396,7 @@ QPDFJob::parseRotationParameter(std::string const&amp; parameter)
396 } else { 396 } else {
397 angle_str = parameter; 397 angle_str = parameter;
398 } 398 }
399 - if (angle_str.length() > 0) { 399 + if (!angle_str.empty()) {
400 char first = angle_str.at(0); 400 char first = angle_str.at(0);
401 if ((first == '+') || (first == '-')) { 401 if ((first == '+') || (first == '-')) {
402 relative = ((first == '+') ? 1 : -1); 402 relative = ((first == '+') ? 1 : -1);
libqpdf/QPDFJob_config.cc
@@ -696,7 +696,7 @@ QPDFJob::Config::passwordFile(std::string const&amp; parameter) @@ -696,7 +696,7 @@ QPDFJob::Config::passwordFile(std::string const&amp; parameter)
696 QTC::TC("qpdf", "QPDFJob_config password file"); 696 QTC::TC("qpdf", "QPDFJob_config password file");
697 lines = QUtil::read_lines_from_file(parameter.c_str()); 697 lines = QUtil::read_lines_from_file(parameter.c_str());
698 } 698 }
699 - if (lines.size() >= 1) { 699 + if (!lines.empty()) {
700 o.m->password = QUtil::make_shared_cstr(lines.front()); 700 o.m->password = QUtil::make_shared_cstr(lines.front());
701 701
702 if (lines.size() > 1) { 702 if (lines.size() > 1) {
libqpdf/QPDFObjectHandle.cc
@@ -467,7 +467,7 @@ BaseHandle::write_json(int json_version, JSON::Writer&amp; p) const @@ -467,7 +467,7 @@ BaseHandle::write_json(int json_version, JSON::Writer&amp; p) const
467 case ::ot_real: 467 case ::ot_real:
468 { 468 {
469 auto const& val = std::get<QPDF_Real>(obj->value).val; 469 auto const& val = std::get<QPDF_Real>(obj->value).val;
470 - if (val.length() == 0) { 470 + if (val.empty()) {
471 // Can't really happen... 471 // Can't really happen...
472 p << "0"; 472 p << "0";
473 } else if (val.at(0) == '.') { 473 } else if (val.at(0) == '.') {
libqpdf/QPDFWriter.cc
@@ -271,7 +271,7 @@ void @@ -271,7 +271,7 @@ void
271 QPDFWriter::setExtraHeaderText(std::string const& text) 271 QPDFWriter::setExtraHeaderText(std::string const& text)
272 { 272 {
273 m->extra_header_text = text; 273 m->extra_header_text = text;
274 - if ((m->extra_header_text.length() > 0) && (*(m->extra_header_text.rbegin()) != '\n')) { 274 + if (!m->extra_header_text.empty() && *m->extra_header_text.rbegin() != '\n') {
275 QTC::TC("qpdf", "QPDFWriter extra header text add newline"); 275 QTC::TC("qpdf", "QPDFWriter extra header text add newline");
276 m->extra_header_text += "\n"; 276 m->extra_header_text += "\n";
277 } else { 277 } else {
@@ -1419,7 +1419,7 @@ QPDFWriter::unparseObject( @@ -1419,7 +1419,7 @@ QPDFWriter::unparseObject(
1419 have_extensions_adbe = true; 1419 have_extensions_adbe = true;
1420 keys.erase("/ADBE"); 1420 keys.erase("/ADBE");
1421 } 1421 }
1422 - if (keys.size() > 0) { 1422 + if (!keys.empty()) {
1423 have_extensions_other = true; 1423 have_extensions_other = true;
1424 } 1424 }
1425 } 1425 }
libqpdf/QPDF_json.cc
@@ -192,7 +192,7 @@ QPDF::test_json_validators() @@ -192,7 +192,7 @@ QPDF::test_json_validators()
192 check(is_unicode_string("u:potato", str)); 192 check(is_unicode_string("u:potato", str));
193 check(str == "potato"); 193 check(str == "potato");
194 check(is_unicode_string("u:", str)); 194 check(is_unicode_string("u:", str));
195 - check(str == ""); 195 + check(str.empty());
196 check(!is_binary_string("", str)); 196 check(!is_binary_string("", str));
197 check(!is_binary_string("x:", str)); 197 check(!is_binary_string("x:", str));
198 check(!is_binary_string("b:1", str)); 198 check(!is_binary_string("b:1", str));
libqpdf/QPDF_objects.cc
@@ -153,7 +153,7 @@ QPDF::parse(char const* password) @@ -153,7 +153,7 @@ QPDF::parse(char const* password)
153 153
154 initializeEncryption(); 154 initializeEncryption();
155 m->parsed = true; 155 m->parsed = true;
156 - if (m->xref_table.size() > 0 && !getRoot().getKey("/Pages").isDictionary()) { 156 + if (!m->xref_table.empty() && !getRoot().getKey("/Pages").isDictionary()) {
157 // QPDFs created from JSON have an empty xref table and no root object yet. 157 // QPDFs created from JSON have an empty xref table and no root object yet.
158 throw damagedPDF("", -1, "unable to find page tree"); 158 throw damagedPDF("", -1, "unable to find page tree");
159 } 159 }
@@ -1932,8 +1932,8 @@ QPDF::tableSize() @@ -1932,8 +1932,8 @@ QPDF::tableSize()
1932 { 1932 {
1933 // If obj_cache is dense, accommodate all object in tables,else accommodate only original 1933 // If obj_cache is dense, accommodate all object in tables,else accommodate only original
1934 // objects. 1934 // objects.
1935 - auto max_xref = m->xref_table.size() ? m->xref_table.crbegin()->first.getObj() : 0;  
1936 - auto max_obj = m->obj_cache.size() ? m->obj_cache.crbegin()->first.getObj() : 0; 1935 + auto max_xref = !m->xref_table.empty() ? m->xref_table.crbegin()->first.getObj() : 0;
  1936 + auto max_obj = !m->obj_cache.empty() ? m->obj_cache.crbegin()->first.getObj() : 0;
1937 auto max_id = std::numeric_limits<int>::max() - 1; 1937 auto max_id = std::numeric_limits<int>::max() - 1;
1938 if (max_obj >= max_id || max_xref >= max_id) { 1938 if (max_obj >= max_id || max_xref >= max_id) {
1939 // Temporary fix. Long-term solution is 1939 // Temporary fix. Long-term solution is
qpdf/test_driver.cc
@@ -1442,7 +1442,7 @@ test_42(QPDF&amp; pdf, char const* arg2) @@ -1442,7 +1442,7 @@ test_42(QPDF&amp; pdf, char const* arg2)
1442 assert(i == di.end()); 1442 assert(i == di.end());
1443 assert(!i_value.second); 1443 assert(!i_value.second);
1444 } 1444 }
1445 - assert("" == qtest.getStringValue()); 1445 + assert(qtest.getStringValue().empty());
1446 array.getArrayItem(-1).assertNull(); 1446 array.getArrayItem(-1).assertNull();
1447 array.getArrayItem(16059).assertNull(); 1447 array.getArrayItem(16059).assertNull();
1448 integer.getArrayItem(0).assertNull(); 1448 integer.getArrayItem(0).assertNull();
@@ -3111,7 +3111,7 @@ test_87(QPDF&amp; pdf, char const* arg2) @@ -3111,7 +3111,7 @@ test_87(QPDF&amp; pdf, char const* arg2)
3111 assert(dict.getKeys() == std::set<std::string>({"/A"})); 3111 assert(dict.getKeys() == std::set<std::string>({"/A"}));
3112 dict.replaceKey("/A", QPDFObjectHandle::newNull()); 3112 dict.replaceKey("/A", QPDFObjectHandle::newNull());
3113 assert(dict.unparse() == "<< >>"); 3113 assert(dict.unparse() == "<< >>");
3114 - assert(dict.getKeys() == std::set<std::string>()); 3114 + assert(dict.getKeys().empty());
3115 dict = QPDFObjectHandle::newDictionary({ 3115 dict = QPDFObjectHandle::newDictionary({
3116 {"/A", "2"_qpdf}, 3116 {"/A", "2"_qpdf},
3117 {"/B", QPDFObjectHandle::newNull()}, 3117 {"/B", QPDFObjectHandle::newNull()},
qpdf/test_parsedoffset.cc
@@ -120,7 +120,7 @@ main(int argc, char* argv[]) @@ -120,7 +120,7 @@ main(int argc, char* argv[])
120 process(argv[1], table); 120 process(argv[1], table);
121 121
122 for (size_t i = 0; i < table.size(); ++i) { 122 for (size_t i = 0; i < table.size(); ++i) {
123 - if (table[i].size() == 0) { 123 + if (table[i].empty()) {
124 continue; 124 continue;
125 } 125 }
126 126