HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
spreadsheet_address.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
26#pragma once
27
28#include "../utility/utility.hpp"
29#include "../algorithm/algorithm.hpp"
30#include "../macros.hpp"
31#include <tuple>
32#include <string_view>
33#include <format>
34
35hi_export_module(hikogui.layout.spreadsheet_address);
36
37hi_export namespace hi::inline v1 {
38
39inline std::tuple<bool, std::size_t, bool, std::size_t> _parse_spreadsheet_address(std::string_view& address)
40{
41 bool column_nr_is_relative = true;
42 std::size_t column_nr = 0;
43 bool row_nr_is_relative = true;
44 std::size_t row_nr = 0;
45
46 if (address.starts_with("$")) {
47 column_nr_is_relative = false;
48 address.remove_prefix(1);
49 }
50
51 while (!address.empty() && is_alpha(address.front())) {
52 column_nr *= 26;
53 column_nr += address.front() - (is_upper(address.front()) ? 'A' : 'a') + 1;
54 address.remove_prefix(1);
55 }
56
57 if (address.starts_with("$")) {
58 row_nr_is_relative = false;
59 address.remove_prefix(1);
60 }
61
62 while (!address.empty() && is_digit(address.front())) {
63 row_nr *= 10;
64 row_nr += address.front() - '0';
65 address.remove_prefix(1);
66 }
67
68 return {column_nr_is_relative, column_nr - 1, row_nr_is_relative, row_nr - 1};
69}
70
80parse_spreadsheet_address(std::string_view address, std::size_t start_column_nr = 0, std::size_t start_row_nr = 0)
81{
82 auto [column_nr_is_relative, column_nr, row_nr_is_relative, row_nr] = _parse_spreadsheet_address(address);
83 hi_check(address.empty(), "Extra characters in spread sheet address {}", address);
84
85 if (column_nr_is_relative) {
86 column_nr += start_column_nr;
87 }
88 if (row_nr_is_relative) {
89 row_nr += start_row_nr;
90 }
91 return {column_nr, row_nr};
92}
93
103parse_spreadsheet_range(std::string_view address, std::size_t start_column_nr = 0, std::size_t start_row_nr = 0)
104{
105 auto [column_nr_is_relative1, column_nr1, row_nr_is_relative1, row_nr1] = _parse_spreadsheet_address(address);
106 if (column_nr_is_relative1) {
107 column_nr1 += start_column_nr;
108 }
109 if (row_nr_is_relative1) {
110 row_nr1 += start_row_nr;
111 }
112
113 if (address.starts_with(":")) {
114 address.remove_prefix(1);
115 auto [column_nr_is_relative2, column_nr2, row_nr_is_relative2, row_nr2] = _parse_spreadsheet_address(address);
116 hi_check(address.empty(), "Extra characters in spread sheet address {}", address);
117
118 if (column_nr_is_relative2) {
119 column_nr2 += start_column_nr;
120 }
121 if (row_nr_is_relative2) {
122 row_nr2 += start_row_nr;
123 }
124
125 hi_check(column_nr1 <= column_nr2, "Column range must be in ascending direction");
126 hi_check(row_nr1 <= row_nr2, "Column range must be in ascending direction");
127 return {column_nr1, row_nr1, column_nr2 + 1, row_nr2 + 1};
128
129 } else {
130 hi_check(address.empty(), "Extra characters in spread sheet address {}", address);
131 return {column_nr1, row_nr1, column_nr1 + 1, row_nr1 + 1};
132 }
133}
134
135} // namespace hi::inline v1
std::pair< std::size_t, std::size_t > parse_spreadsheet_address(std::string_view address, std::size_t start_column_nr=0, std::size_t start_row_nr=0)
Parse a spreadsheet address.
Definition spreadsheet_address.hpp:80
std::tuple< std::size_t, std::size_t, std::size_t, std::size_t > parse_spreadsheet_range(std::string_view address, std::size_t start_column_nr=0, std::size_t start_row_nr=0)
Parse a spreadsheet range.
Definition spreadsheet_address.hpp:103
DOXYGEN BUG.
Definition algorithm_misc.hpp:20