Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2010-2015 openpyxl
3 : : // Copyright (c) 2024-2025 xlnt-community
4 : : //
5 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
6 : : // of this software and associated documentation files (the "Software"), to deal
7 : : // in the Software without restriction, including without limitation the rights
8 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : : // copies of the Software, and to permit persons to whom the Software is
10 : : // furnished to do so, subject to the following conditions:
11 : : //
12 : : // The above copyright notice and this permission notice shall be included in
13 : : // all copies or substantial portions of the Software.
14 : : //
15 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : : // THE SOFTWARE
22 : : //
23 : : // @license: http://www.opensource.org/licenses/mit-license.php
24 : : // @author: see AUTHORS file
25 : : #include <cctype>
26 : :
27 : : #include <xlnt/cell/index_types.hpp>
28 : : #include <xlnt/utils/exceptions.hpp>
29 : : #include <detail/constants.hpp>
30 : :
31 : : namespace xlnt {
32 : :
33 :CBC 1100 : column_t::index_t column_t::column_index_from_string(const std::string &column_string)
34 : : {
35 [ + + + + : 1100 : if (column_string.length() > 3 || column_string.empty())
+ + ]
36 : : {
37 [ + ]: 2 : throw invalid_column_index();
38 : : }
39 : :
40 : 1098 : column_t::index_t column_index = 0;
41 : 1098 : int place = 1;
42 : :
43 [ + + ]: 2213 : for (int i = static_cast<int>(column_string.length()) - 1; i >= 0; i--)
44 : : {
45 [ + + ]: 1116 : if (!std::isalpha(column_string[static_cast<std::size_t>(i)]))
46 : : {
47 [ + ]: 1 : throw invalid_column_index();
48 : : }
49 : :
50 : 1115 : auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)]) - 'A';
51 : :
52 : 1115 : column_index += static_cast<column_t::index_t>((char_index + 1) * place);
53 : 1115 : place *= 26;
54 : : }
55 : :
56 : 1097 : return column_index;
57 : : }
58 : :
59 : : // Convert a column number into a column letter (3 -> 'C')
60 : : // Right shift the column col_idx by 26 to find column letters in reverse
61 : : // order.These numbers are 1 - based, and can be converted to ASCII
62 : : // ordinals by adding 64.
63 : 1412 : std::string column_t::column_string_from_index(column_t::index_t column_index)
64 : : {
65 : : // these indicies corrospond to A->ZZZ and include all allowed
66 : : // columns
67 [ + + + + : 1412 : if (column_index < constants::min_column() || column_index > constants::max_column())
- + + + ]
68 : : {
69 [ + ]: 1 : throw invalid_column_index();
70 : : }
71 : :
72 : 1411 : int temp = static_cast<int>(column_index);
73 [ + ]: 1411 : std::string column_letter = "";
74 : :
75 [ + + ]: 2824 : while (temp > 0)
76 : : {
77 : 1413 : int quotient = temp / 26, remainder = temp % 26;
78 : :
79 : : // check for exact division and borrow if needed
80 [ + + ]: 1413 : if (remainder == 0)
81 : : {
82 : 1 : quotient -= 1;
83 : 1 : remainder = 26;
84 : : }
85 : :
86 [ + + ]: 1413 : column_letter = std::string(1, char(remainder + 64)) + column_letter;
87 : 1413 : temp = quotient;
88 : : }
89 : :
90 : 1411 : return column_letter;
91 :UBC 0 : }
92 : :
93 :CBC 1019 : column_t::column_t()
94 : 1019 : : index(1)
95 : : {
96 : 1019 : }
97 : :
98 : 139485 : column_t::column_t(index_t column_index)
99 : 139485 : : index(column_index)
100 : : {
101 : 139485 : }
102 : :
103 : 1097 : column_t::column_t(const std::string &column_string)
104 : 1097 : : index(column_index_from_string(column_string))
105 : : {
106 : 1097 : }
107 : :
108 : 58 : column_t::column_t(const char *column_string)
109 [ + + ]: 58 : : column_t(std::string(column_string))
110 : : {
111 : 58 : }
112 : :
113 : 1410 : std::string column_t::column_string() const
114 : : {
115 : 1410 : return column_string_from_index(index);
116 : : }
117 : :
118 : 1 : column_t &column_t::operator=(const std::string &rhs)
119 : : {
120 [ + ]: 1 : return *this = column_t(rhs);
121 : : }
122 : :
123 : 1 : column_t &column_t::operator=(const char *rhs)
124 : : {
125 [ + ]: 1 : return *this = column_t(rhs);
126 : : }
127 : :
128 : 31079 : bool column_t::operator==(const column_t &other) const
129 : : {
130 : 31079 : return index == other.index;
131 : : }
132 : :
133 : 1 : bool column_t::operator!=(const column_t &other) const
134 : : {
135 : 1 : return !(*this == other);
136 : : }
137 : :
138 : 12958 : bool column_t::operator==(int other) const
139 : : {
140 : 12958 : return *this == column_t(static_cast<index_t>(other));
141 : : }
142 : :
143 : 2 : bool column_t::operator==(index_t other) const
144 : : {
145 : 2 : return *this == column_t(other);
146 : : }
147 : :
148 : 2 : bool column_t::operator==(const std::string &other) const
149 : : {
150 [ + ]: 2 : return *this == column_t(other);
151 : : }
152 : :
153 : 15 : bool column_t::operator==(const char *other) const
154 : : {
155 [ + ]: 15 : return *this == column_t(other);
156 : : }
157 : :
158 : 1 : bool column_t::operator!=(int other) const
159 : : {
160 : 1 : return !(*this == other);
161 : : }
162 : :
163 : 1 : bool column_t::operator!=(index_t other) const
164 : : {
165 : 1 : return !(*this == other);
166 : : }
167 : :
168 : 1 : bool column_t::operator!=(const std::string &other) const
169 : : {
170 : 1 : return !(*this == other);
171 : : }
172 : :
173 : 1 : bool column_t::operator!=(const char *other) const
174 : : {
175 : 1 : return !(*this == other);
176 : : }
177 : :
178 : 1453 : bool column_t::operator>(const column_t &other) const
179 : : {
180 : 1453 : return index > other.index;
181 : : }
182 : :
183 : 3 : bool column_t::operator>=(const column_t &other) const
184 : : {
185 : 3 : return index >= other.index;
186 : : }
187 : :
188 : 15925 : bool column_t::operator<(const column_t &other) const
189 : : {
190 : 15925 : return index < other.index;
191 : : }
192 : :
193 : 33868 : bool column_t::operator<=(const column_t &other) const
194 : : {
195 : 33868 : return index <= other.index;
196 : : }
197 : :
198 : 1 : bool column_t::operator>(const column_t::index_t &other) const
199 : : {
200 : 1 : return index > other;
201 : : }
202 : :
203 : 1 : bool column_t::operator>=(const column_t::index_t &other) const
204 : : {
205 : 1 : return index >= other;
206 : : }
207 : :
208 : 1 : bool column_t::operator<(const column_t::index_t &other) const
209 : : {
210 : 1 : return index < other;
211 : : }
212 : :
213 : 1 : bool column_t::operator<=(const column_t::index_t &other) const
214 : : {
215 : 1 : return index <= other;
216 : : }
217 : :
218 : 9107 : column_t &column_t::operator++()
219 : : {
220 : 9107 : index++;
221 : 9107 : return *this;
222 : : }
223 : :
224 : 2 : column_t &column_t::operator--()
225 : : {
226 : 2 : index--;
227 : 2 : return *this;
228 : : }
229 : :
230 : 1436 : column_t column_t::operator++(int)
231 : : {
232 : 1436 : column_t copy(index);
233 : 1436 : ++(*this);
234 : 1436 : return copy;
235 : : }
236 : :
237 : 1 : column_t column_t::operator--(int)
238 : : {
239 : 1 : column_t copy(index);
240 : 1 : --(*this);
241 : 1 : return copy;
242 : : }
243 : :
244 : 2 : column_t operator+(column_t lhs, const column_t &rhs)
245 : : {
246 : 2 : lhs += rhs;
247 : 2 : return lhs;
248 : : }
249 : :
250 : 142 : column_t operator-(column_t lhs, const column_t &rhs)
251 : : {
252 : 142 : lhs -= rhs;
253 : 142 : return lhs;
254 : : }
255 : :
256 : 2 : column_t &column_t::operator+=(const column_t &rhs)
257 : : {
258 : 2 : index += rhs.index;
259 : 2 : return *this;
260 : : }
261 : :
262 : 142 : column_t &column_t::operator-=(const column_t &rhs)
263 : : {
264 : 142 : index -= rhs.index;
265 : 142 : return *this;
266 : : }
267 : :
268 : 1413 : bool operator>(const column_t::index_t &left, const column_t &right)
269 : : {
270 : 1413 : return column_t(left) > right;
271 : : }
272 : :
273 : 2 : bool operator>=(const column_t::index_t &left, const column_t &right)
274 : : {
275 : 2 : return column_t(left) >= right;
276 : : }
277 : :
278 : 1412 : bool operator<(const column_t::index_t &left, const column_t &right)
279 : : {
280 : 1412 : return column_t(left) < right;
281 : : }
282 : :
283 : 2 : bool operator<=(const column_t::index_t &left, const column_t &right)
284 : : {
285 : 2 : return column_t(left) <= right;
286 : : }
287 : :
288 :UBC 0 : void swap(column_t &left, column_t &right)
289 : : {
290 : : using std::swap;
291 : 0 : swap(left.index, right.index);
292 : 0 : }
293 : :
294 :CBC 95523 : std::size_t column_hash::operator()(const column_t &k) const
295 : : {
296 : : static std::hash<column_t::index_t> hasher;
297 : 95523 : return hasher(k.index);
298 : : }
299 : :
300 : : } // namespace xlnt
|