Skip to content

Commit 44fcb73

Browse files
committed
Use Iterator::reverse() everywhere in subdoc
Replace off-by-one or overflow bug-prone math with .reverse(). If the iterator is not a DoubleEndedIterator then with .collect_vec().into_iter().reverse().
1 parent 9f7e107 commit 44fcb73

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

subdoc/lib/database.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,10 @@ struct Database {
496496
"non-namespace.";
497497
return sus::result::err(sus::move(s).str());
498498
}
499-
if (!target.as_mut<Target::Namespace>().namespaces.contains(
500-
id)) {
499+
if (!target.as_mut<Target::Namespace>().namespaces.contains(id)) {
501500
std::ostringstream s;
502501
s << "Inherited comment at " << c->begin_loc
503-
<< " can't find namespace "
504-
<< e.as<InheritPathNamespace>();
502+
<< " can't find namespace " << e.as<InheritPathNamespace>();
505503
return sus::result::err(sus::move(s).str());
506504
}
507505
target = sus::choice<Target::Namespace>(
@@ -526,8 +524,7 @@ struct Database {
526524
find_record(target.as_mut<Target::Namespace>().records);
527525
break;
528526
case Target::Record:
529-
record =
530-
find_record(target.as_mut<Target::Record>().records);
527+
record = find_record(target.as_mut<Target::Record>().records);
531528
break;
532529
case Target::Function: {
533530
std::ostringstream s;
@@ -648,11 +645,11 @@ struct Database {
648645
auto ns_cursor = [&]() -> sus::Option<const NamespaceElement&> {
649646
const NamespaceElement* cursor = &global;
650647

651-
auto it = iter_namespace_path(decl);
652-
// TODO: Make Iterator::reverse() and use that.
653-
auto v = sus::move(it).collect_vec();
654-
for (usize i = 1u; i < v.len(); i += 1u) {
655-
const Namespace& n = v[v.len() - i - 1u];
648+
// Namespace path goes from the outside in to the global, we want the
649+
// inverse, and then to skip the global namespace.
650+
auto it = iter_namespace_path(decl).collect_vec().into_iter().reverse();
651+
it.next(); // TODO: Use Iterator::skip().
652+
for (const Namespace& n : it) {
656653
switch (n) {
657654
case Namespace::Tag::Global: {
658655
// We skipped the 1st namespace in the path which is the global
@@ -685,13 +682,12 @@ struct Database {
685682
clang::dyn_cast<clang::RecordDecl>(decl->getDeclContext())) {
686683
const RecordElement* cursor = nullptr;
687684

688-
auto it = iter_record_path(containing_record_decl);
689-
// TODO: Make Iterator::reverse() and use that.
690-
auto v = sus::move(it).collect_vec();
691-
for (usize i; i < v.len(); i += 1u) {
692-
std::string_view name = v[v.len() - i - 1u];
693-
694-
if (i == 0u) {
685+
bool first = true; // TODO: Use Iterator::enumerate().
686+
for (std::string_view name : iter_record_path(containing_record_decl)
687+
.collect_vec()
688+
.into_iter()
689+
.reverse()) {
690+
if (first) {
695691
auto r_it = ns_cursor->records.find(RecordId(name));
696692
if (r_it == ns_cursor->records.end()) {
697693
return sus::none();
@@ -704,6 +700,7 @@ struct Database {
704700
}
705701
cursor = &r_it->second;
706702
}
703+
first = false;
707704
}
708705
return sus::some(*cursor);
709706
} else {

subdoc/lib/gen/files.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ inline std::filesystem::path construct_html_file_path(
4545
std::filesystem::path p = sus::move(root);
4646

4747
std::ostringstream fname;
48-
// TODO: Add Iterator::reverse() and use that.
49-
for (size_t i = 0; i < namespace_path.len(); ++i) {
50-
const Namespace& n = namespace_path[namespace_path.len() - i - 1u];
48+
for (const Namespace& n : namespace_path.iter().reverse()) {
5149
switch (n) {
5250
case Namespace::Tag::Global: break;
5351
case Namespace::Tag::Anonymous:
@@ -60,10 +58,8 @@ inline std::filesystem::path construct_html_file_path(
6058
break;
6159
}
6260
}
63-
// TODO: Add Iterator::reverse.
64-
for (size_t i = 0; i < record_path.len(); ++i) {
65-
const std::string& n = record_path[record_path.len() - i - 1u];
66-
fname << n + "-";
61+
for (std::string_view n : record_path.iter().reverse()) {
62+
fname << n << "-";
6763
}
6864
fname << name;
6965
fname << ".html";

subdoc/lib/gen/generate_record.cc

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ void generate_record_overview(HtmlWriter::OpenDiv& record_div,
5050
{
5151
// TODO: This code gets duplicated a lot, share it.
5252

53-
// TODO: Add Iterator::reverse() and use that.
54-
for (size_t i = 0; i < element.namespace_path.len(); ++i) {
55-
const Namespace& n =
56-
element.namespace_path[element.namespace_path.len() - i - 1u];
53+
for (const Namespace &n : element.namespace_path.iter().reverse()) {
5754
switch (n) {
5855
case Namespace::Tag::Global: break;
5956
case Namespace::Tag::Anonymous: {
@@ -71,10 +68,7 @@ void generate_record_overview(HtmlWriter::OpenDiv& record_div,
7168
break;
7269
}
7370
}
74-
// TODO: Add Iterator::reverse() and use that.
75-
for (size_t i = 0; i < element.record_path.len(); ++i) {
76-
const std::string& record_name =
77-
element.record_path[element.record_path.len() - i - 1u];
71+
for (std::string_view record_name: element.record_path.iter().reverse()) {
7872
{
7973
auto record_anchor = full_type_span.open_a();
8074
record_anchor.write_text(record_name);
@@ -234,10 +228,7 @@ void generate_record(const RecordElement& element,
234228

235229
{
236230
std::ostringstream title;
237-
// TODO: Add Iterator::reverse() and use that.
238-
for (size_t i = 0; i < element.namespace_path.len(); ++i) {
239-
const Namespace& n =
240-
element.namespace_path[element.namespace_path.len() - i - 1u];
231+
for (const Namespace& n: element.namespace_path.iter().reverse()) {
241232
switch (n) {
242233
case Namespace::Tag::Global: break;
243234
case Namespace::Tag::Anonymous:
@@ -250,10 +241,7 @@ void generate_record(const RecordElement& element,
250241
break;
251242
}
252243
}
253-
// TODO: Add Iterator::reverse() and use that.
254-
for (size_t i = 0; i < element.record_path.len(); ++i) {
255-
const std::string& record_name =
256-
element.record_path[element.record_path.len() - i - 1u];
244+
for (std::string_view record_name: element.record_path.iter().reverse()) {
257245
title << record_name;
258246
title << "::";
259247
}

subdoc/lib/path.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ inline std::string namespace_with_path_to_string(
4343
std::ostringstream s;
4444
bool add_colons = false;
4545

46-
// TODO: Add Iterator::reverse() and use that here.
47-
for (usize i = path.len(); i > 0u; i -= 1u) {
46+
for (const Namespace& n : path.iter().reverse()) {
4847
if (add_colons) s << "::";
49-
const Namespace& n = path[i - 1u];
5048
switch (n) {
5149
case Namespace::Tag::Global: break;
5250
case Namespace::Tag::Anonymous:
@@ -64,9 +62,7 @@ inline std::string namespace_with_path_to_string(
6462
switch (tail) {
6563
case Namespace::Tag::Global: s << "Global namespace"; break;
6664
case Namespace::Tag::Anonymous: s << "(anonymous)"; break;
67-
case Namespace::Tag::Named:
68-
s << tail.as<Namespace::Tag::Named>();
69-
break;
65+
case Namespace::Tag::Named: s << tail.as<Namespace::Tag::Named>(); break;
7066
}
7167

7268
return s.str();

0 commit comments

Comments
 (0)