11#include < odr/document_path.hpp>
22
3- #include < odr/document_element.hpp>
4-
5- #include < algorithm>
63#include < stdexcept>
74
85namespace odr {
@@ -12,45 +9,10 @@ const std::string &DocumentPath::Child::prefix_string() {
129 return result;
1310}
1411
15- std::pair<Element, DocumentPath::Child>
16- DocumentPath::Child::extract (const Element element) {
17- if (!element) {
18- throw std::invalid_argument (" element is null" );
19- }
20- const Element parent = element.parent ();
21- if (!parent) {
22- throw std::invalid_argument (" parent not found" );
23- }
24-
25- Element current = element;
26- std::uint32_t distance = 0 ;
27- for (; current.previous_sibling (); current = current.previous_sibling ()) {
28- ++distance;
29- }
30-
31- return {parent, Child (distance)};
32- }
33-
34- Element DocumentPath::Child::resolve (const Element element,
35- const Child &child) {
36- if (!element) {
37- throw std::invalid_argument (" element is null" );
38- }
39- if (!element.first_child ()) {
40- throw std::invalid_argument (" child not found" );
41- }
42- Element result = element.first_child ();
43- for (std::uint32_t i = 0 ; i < child.m_number ; ++i) {
44- if (!result.next_sibling ()) {
45- throw std::invalid_argument (" sibling not found" );
46- }
47- result = result.next_sibling ();
48- }
49- return result;
50- }
51-
5212DocumentPath::Child::Child (const std::uint32_t number) : m_number{number} {}
5313
14+ std::uint32_t DocumentPath::Child::number () const { return m_number; }
15+
5416bool DocumentPath::Child::operator ==(const Child &other) const noexcept {
5517 return m_number == other.m_number ;
5618}
@@ -64,37 +26,11 @@ const std::string &DocumentPath::Cell::prefix_string() {
6426 return result;
6527}
6628
67- std::pair<Sheet, DocumentPath::Cell>
68- DocumentPath::Cell::extract (const SheetCell &element) {
69- if (!element) {
70- throw std::invalid_argument (" element is null" );
71- }
72- const Element parent = element.parent ();
73- if (!parent) {
74- throw std::invalid_argument (" parent not found" );
75- }
76- const Sheet sheet = parent.as_sheet ();
77- if (!sheet) {
78- throw std::invalid_argument (" parent is not a sheet" );
79- }
80- const SheetCell cell = element.as_sheet_cell ();
81- if (!cell) {
82- throw std::invalid_argument (" element is not a sheet cell" );
83- }
84- return {sheet, Cell (cell.position ())};
85- }
86-
87- SheetCell DocumentPath::Cell::resolve (const Sheet &element, const Cell &cell) {
88- if (!element) {
89- throw std::invalid_argument (" element is null" );
90- }
91- return element.as_sheet ().cell (cell.m_position .column (),
92- cell.m_position .row ());
93- }
94-
9529DocumentPath::Cell::Cell (const TablePosition &position)
9630 : m_position{position} {}
9731
32+ TablePosition DocumentPath::Cell::position () const { return m_position; }
33+
9834bool DocumentPath::Cell::operator ==(const Cell &other) const noexcept {
9935 return m_position == other.m_position ;
10036}
@@ -124,57 +60,6 @@ DocumentPath::component_from_string(const std::string &string) {
12460 throw std::invalid_argument (" string" );
12561}
12662
127- DocumentPath DocumentPath::extract (const Element element) {
128- return extract (element, {});
129- }
130-
131- DocumentPath DocumentPath::extract (const Element element, const Element root) {
132- std::vector<Component> reverse;
133-
134- for (Element current = element; current != root;) {
135- if (!current.parent ()) {
136- break ;
137- }
138-
139- if (const SheetCell sheet_cell = current.as_sheet_cell (); sheet_cell) {
140- const auto [parent, cell] = Cell::extract (sheet_cell);
141- reverse.emplace_back (cell);
142- current = static_cast <Element>(parent);
143- } else {
144- const auto [parent, child] = Child::extract (current);
145- reverse.emplace_back (child);
146- current = parent;
147- }
148- }
149-
150- std::ranges::reverse (reverse);
151- return DocumentPath (reverse);
152- }
153-
154- Element DocumentPath::resolve (const Element root, const DocumentPath &path) {
155- Element element = root;
156-
157- for (const Component &c : path) {
158- if (const auto *child = std::get_if<Child>(&c); child != nullptr ) {
159- element = Child::resolve (element, *child);
160- } else if (const auto *cell = std::get_if<Cell>(&c); cell != nullptr ) {
161- const Sheet sheet = element.as_sheet ();
162- if (!sheet) {
163- throw std::invalid_argument (" element is not a sheet" );
164- }
165- const SheetCell sheet_cell = Cell::resolve (sheet, *cell);
166- element = static_cast <Element>(sheet_cell);
167- } else {
168- throw std::invalid_argument (" unknown component" );
169- }
170- if (!element) {
171- throw std::invalid_argument (" element not found" );
172- }
173- }
174-
175- return element;
176- }
177-
17863DocumentPath::DocumentPath () noexcept = default ;
17964
18065DocumentPath::DocumentPath (const Container &components)
@@ -228,7 +113,7 @@ std::string DocumentPath::to_string() const noexcept {
228113
229114bool DocumentPath::empty () const noexcept { return m_components.empty (); }
230115
231- DocumentPath::Component DocumentPath::back () const {
116+ const DocumentPath::Component & DocumentPath::back () const {
232117 return m_components.back ();
233118}
234119
0 commit comments