mirror of
https://github.com/RGBCube/serenity
synced 2025-06-27 22:42:08 +00:00
Spreadsheet: Use String::bijective_base_from for column name resolution
Updated Spreadsheet to use the new way of converting a number to a String represenation using the alphabet. The code responsible for this conversion now lives in AK/String, so it gets deleted from Spreadsheet.cpp.
This commit is contained in:
parent
4016e04061
commit
ef8f97635e
1 changed files with 5 additions and 32 deletions
|
@ -82,33 +82,6 @@ size_t Sheet::add_row()
|
||||||
return m_rows++;
|
return m_rows++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String convert_to_string(size_t value, unsigned base = 26, StringView map = {})
|
|
||||||
{
|
|
||||||
if (map.is_null())
|
|
||||||
map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
|
|
||||||
VERIFY(base >= 2 && base <= map.length());
|
|
||||||
|
|
||||||
// The '8 bits per byte' assumption may need to go?
|
|
||||||
Array<char, round_up_to_power_of_two(sizeof(size_t) * 8 + 1, 2)> buffer;
|
|
||||||
size_t i = 0;
|
|
||||||
do {
|
|
||||||
buffer[i++] = map[value % base];
|
|
||||||
value /= base;
|
|
||||||
} while (value > 0);
|
|
||||||
|
|
||||||
// NOTE: Weird as this may seem, the thing that comes after 'Z' is 'AA', which as a number would be '00'
|
|
||||||
// to make this work, only the most significant digit has to be in a range of (1..25) as opposed to (0..25),
|
|
||||||
// but only if it's not the only digit in the string.
|
|
||||||
if (i > 1)
|
|
||||||
--buffer[i - 1];
|
|
||||||
|
|
||||||
for (size_t j = 0; j < i / 2; ++j)
|
|
||||||
swap(buffer[j], buffer[i - j - 1]);
|
|
||||||
|
|
||||||
return String { ReadonlyBytes(buffer.data(), i) };
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t convert_from_string(StringView str, unsigned base = 26, StringView map = {})
|
static size_t convert_from_string(StringView str, unsigned base = 26, StringView map = {})
|
||||||
{
|
{
|
||||||
if (map.is_null())
|
if (map.is_null())
|
||||||
|
@ -119,7 +92,7 @@ static size_t convert_from_string(StringView str, unsigned base = 26, StringView
|
||||||
size_t value = 0;
|
size_t value = 0;
|
||||||
for (size_t i = str.length(); i > 0; --i) {
|
for (size_t i = str.length(); i > 0; --i) {
|
||||||
auto digit_value = map.find_first_of(str[i - 1]).value_or(0);
|
auto digit_value = map.find_first_of(str[i - 1]).value_or(0);
|
||||||
// NOTE: Refer to the note in `convert_to_string()'.
|
// NOTE: Refer to the note in `String::bijective_base_from()'.
|
||||||
if (i == str.length() && str.length() > 1)
|
if (i == str.length() && str.length() > 1)
|
||||||
++digit_value;
|
++digit_value;
|
||||||
value = value * base + digit_value;
|
value = value * base + digit_value;
|
||||||
|
@ -130,7 +103,7 @@ static size_t convert_from_string(StringView str, unsigned base = 26, StringView
|
||||||
|
|
||||||
String Sheet::add_column()
|
String Sheet::add_column()
|
||||||
{
|
{
|
||||||
auto next_column = convert_to_string(m_columns.size());
|
auto next_column = String::bijective_base_from(m_columns.size());
|
||||||
m_columns.append(next_column);
|
m_columns.append(next_column);
|
||||||
return next_column;
|
return next_column;
|
||||||
}
|
}
|
||||||
|
@ -491,11 +464,11 @@ Position Sheet::written_data_bounds() const
|
||||||
|
|
||||||
/// The sheet is allowed to have nonstandard column names
|
/// The sheet is allowed to have nonstandard column names
|
||||||
/// this checks whether all existing columns are 'standard'
|
/// this checks whether all existing columns are 'standard'
|
||||||
/// (i.e. as generated by 'convert_to_string()'
|
/// (i.e. as generated by 'String::bijective_base_from()'
|
||||||
bool Sheet::columns_are_standard() const
|
bool Sheet::columns_are_standard() const
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_columns.size(); ++i) {
|
for (size_t i = 0; i < m_columns.size(); ++i) {
|
||||||
if (m_columns[i] != convert_to_string(i))
|
if (m_columns[i] != String::bijective_base_from(i))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -626,7 +599,7 @@ RefPtr<Sheet> Sheet::from_xsv(const Reader::XSV& xsv, Workbook& workbook)
|
||||||
} else {
|
} else {
|
||||||
sheet->m_columns.ensure_capacity(cols.size());
|
sheet->m_columns.ensure_capacity(cols.size());
|
||||||
for (size_t i = 0; i < cols.size(); ++i)
|
for (size_t i = 0; i < cols.size(); ++i)
|
||||||
sheet->m_columns.append(convert_to_string(i));
|
sheet->m_columns.append(String::bijective_base_from(i));
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < max(rows, Sheet::default_row_count); ++i)
|
for (size_t i = 0; i < max(rows, Sheet::default_row_count); ++i)
|
||||||
sheet->add_row();
|
sheet->add_row();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue