1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

Spreadsheet/XSV: Add at() accessors

These are just aliases for operator[].
Also make the headers() getter return a vector of empty strings when the
csv file has no explicit headers.
This commit is contained in:
AnotherTest 2021-03-19 17:27:35 +03:30 committed by Andreas Kling
parent 39f3f3be94
commit 3bbcde9192
2 changed files with 24 additions and 3 deletions

View file

@ -48,8 +48,17 @@ void XSV::set_error(ReadError error)
Vector<String> XSV::headers() const
{
Vector<String> headers;
for (auto& field : m_names)
headers.append(field.is_string_view ? field.as_string_view : field.as_string.view());
if (has_explicit_headers()) {
for (auto& field : m_names)
headers.append(field.is_string_view ? field.as_string_view : field.as_string.view());
} else {
// No headers read, grab one of the rows and generate empty names
if (m_rows.is_empty())
return headers;
for ([[maybe_unused]] auto& field : m_rows.first())
headers.append(String::empty());
}
return headers;
}
@ -263,6 +272,11 @@ const XSV::Row XSV::operator[](size_t index) const
return const_cast<XSV&>(*this)[index];
}
XSV::Row XSV::at(size_t index) const
{
return this->operator[](index);
}
XSV::Row XSV::operator[](size_t index)
{
VERIFY(m_rows.size() > index);