Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ The release run heads these entries with the version and opens a fresh

## Unreleased

- **Breaking**: `all_file_types()` and `all_text_encodings()` return a
`std::span` over the library's tables instead of a `std::vector`, like the
other table lookups. The bindings still hand out a list.

- `HtmlConfig::editing_scope` narrows the document view's editor to edits
inside one paragraph (`paragraph`); the page refuses the rest with the new
`ErrorCode::edit_out_of_scope` (1010, `outOfScope`). Bound in every binding.
Expand Down
4 changes: 2 additions & 2 deletions apple/src/ODROdr.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ + (NSString *)identification {
+ (NSArray<NSNumber *> *)allFileTypes {
return guarded_value(
[&]() -> NSArray<NSNumber *> * {
const std::vector<odr::FileType> types = odr::all_file_types();
const std::span<const odr::FileType> types = odr::all_file_types();
NSMutableArray<NSNumber *> *const result =
[NSMutableArray arrayWithCapacity:types.size()];
for (const odr::FileType type : types) {
Expand Down Expand Up @@ -149,7 +149,7 @@ + (ODRFileTypeCapabilities *)capabilitiesForFileType:(ODRFileType)type {
+ (NSArray<NSNumber *> *)allTextEncodings {
return guarded_value(
[&]() -> NSArray<NSNumber *> * {
const std::vector<odr::TextEncoding> encodings =
const std::span<const odr::TextEncoding> encodings =
odr::all_text_encodings();
NSMutableArray<NSNumber *> *const result =
[NSMutableArray arrayWithCapacity:encodings.size()];
Expand Down
20 changes: 16 additions & 4 deletions python/src/bind_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,22 @@ void odr_python::bind_core(py::module_ &m) {
}

void odr_python::bind_functions(py::module_ &m) {
m.def("all_file_types", &odr::all_file_types,
"Every file type this library knows about.");
m.def("all_text_encodings", &odr::all_text_encodings,
"Every text encoding the library knows about, excluding `unknown`.");
// pybind11 has no caster for a span
m.def(
"all_file_types",
[] {
const auto types = odr::all_file_types();
return std::vector<odr::FileType>(types.begin(), types.end());
},
"Every file type this library knows about.");
m.def(
"all_text_encodings",
[] {
const auto encodings = odr::all_text_encodings();
return std::vector<odr::TextEncoding>(encodings.begin(),
encodings.end());
},
"Every text encoding the library knows about, excluding `unknown`.");
m.def(
"text_encoding_to_string",
[](const odr::TextEncoding encoding) {
Expand Down
2 changes: 1 addition & 1 deletion src/odr/internal/csv/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fails when an alias is claimed twice.
enum class TextEncoding { unknown, utf8, utf16le, utf16be, utf32le, utf32be,
windows_1252, iso_8859_1, iso_8859_15, shift_jis, … };

std::vector<TextEncoding> all_text_encodings();
std::span<const TextEncoding> all_text_encodings() noexcept;
std::string text_encoding_to_string(TextEncoding);
TextEncoding text_encoding_by_name(std::string_view) noexcept; // unknown if none
std::span<const std::string_view> text_encoding_names(TextEncoding) noexcept;
Expand Down
13 changes: 13 additions & 0 deletions src/odr/internal/encoding/text_encoding_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <algorithm>
#include <array>
#include <cstddef>
#include <string>

namespace odr::internal::encoding {
Expand Down Expand Up @@ -133,12 +134,24 @@ std::string normalize(const std::string_view name) {
return result;
}

constexpr auto encodings_column = [] {
std::array<TextEncoding, table.size()> result{};
for (std::size_t i = 0; i < table.size(); ++i) {
result[i] = table[i].encoding;
}
return result;
}();

} // namespace

std::span<const text_encoding_table::Row> text_encoding_table::rows() noexcept {
return table;
}

std::span<const TextEncoding> text_encoding_table::encodings() noexcept {
return encodings_column;
}

const text_encoding_table::Row *
text_encoding_table::find(const TextEncoding encoding) noexcept {
const auto it = std::ranges::find(table, encoding, &Row::encoding);
Expand Down
2 changes: 2 additions & 0 deletions src/odr/internal/encoding/text_encoding_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct Row final {

/// The whole table, one row per @ref TextEncoding, in declaration order.
[[nodiscard]] std::span<const Row> rows() noexcept;
/// The encoding column of @ref rows.
[[nodiscard]] std::span<const TextEncoding> encodings() noexcept;

/// The row for @p encoding, or `nullptr` if there is none.
[[nodiscard]] const Row *find(TextEncoding encoding) noexcept;
Expand Down
13 changes: 13 additions & 0 deletions src/odr/internal/file_type_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <array>
#include <cstddef>

namespace odr::internal {

Expand Down Expand Up @@ -831,12 +832,24 @@ const Row *find_by_alias(const std::string_view needle,
return it == std::ranges::end(table) ? nullptr : &*it;
}

constexpr auto types_column = [] {
std::array<FileType, table.size()> result{};
for (std::size_t i = 0; i < table.size(); ++i) {
result[i] = table[i].type;
}
return result;
}();

} // namespace

std::span<const file_type_table::Row> file_type_table::rows() noexcept {
return table;
}

std::span<const FileType> file_type_table::types() noexcept {
return types_column;
}

const file_type_table::Row *
file_type_table::find(const FileType type) noexcept {
const auto it = std::ranges::find(table, type, &Row::type);
Expand Down
2 changes: 2 additions & 0 deletions src/odr/internal/file_type_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct Row final {

/// The whole table, one row per @ref FileType, in declaration order.
[[nodiscard]] std::span<const Row> rows() noexcept;
/// The type column of @ref rows.
[[nodiscard]] std::span<const FileType> types() noexcept;

/// The row for @p type, or `nullptr` if there is none.
[[nodiscard]] const Row *find(FileType type) noexcept;
Expand Down
17 changes: 4 additions & 13 deletions src/odr/odr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <odr/internal/open_strategy.hpp>
#include <odr/internal/project_info.hpp>

#include <algorithm>
#include <iterator>
#include <span>
#include <string_view>

Expand Down Expand Up @@ -40,11 +38,8 @@ std::string odr::identify() noexcept {
(is_dirty() ? " [dirty]" : "") + (is_debug() ? " [debug]" : "");
}

std::vector<odr::FileType> odr::all_file_types() {
std::vector<FileType> result;
result.reserve(table::rows().size());
std::ranges::transform(table::rows(), std::back_inserter(result), &Row::type);
return result;
std::span<const odr::FileType> odr::all_file_types() noexcept {
return table::types();
}

odr::FileType
Expand Down Expand Up @@ -152,12 +147,8 @@ odr::capabilities_by_file_type(const FileType type) noexcept {
return row == nullptr ? FileTypeCapabilities{} : row->capabilities;
}

std::vector<odr::TextEncoding> odr::all_text_encodings() {
std::vector<TextEncoding> result;
result.reserve(encoding_table::rows().size());
std::ranges::transform(encoding_table::rows(), std::back_inserter(result),
&EncodingRow::encoding);
return result;
std::span<const odr::TextEncoding> odr::all_text_encodings() noexcept {
return encoding_table::encodings();
}

std::string_view odr::text_encoding_to_string(const TextEncoding encoding) {
Expand Down
4 changes: 2 additions & 2 deletions src/odr/odr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace odr {

/// Every file type this library knows about, in declaration order, including
/// @ref FileType::unknown.
[[nodiscard]] std::vector<FileType> all_file_types();
[[nodiscard]] std::span<const FileType> all_file_types() noexcept;

/// The file type for a file extension, @ref FileType::unknown if none.
[[nodiscard]] FileType
Expand Down Expand Up @@ -66,7 +66,7 @@ capabilities_by_file_type(FileType type) noexcept;

/// Every text encoding this library knows about, in declaration order,
/// excluding @ref TextEncoding::unknown.
[[nodiscard]] std::vector<TextEncoding> all_text_encodings();
[[nodiscard]] std::span<const TextEncoding> all_text_encodings() noexcept;

/// The text encoding's canonical name, a label a browser accepts.
/// @throws UnsupportedTextEncoding for @ref TextEncoding::unknown.
Expand Down
5 changes: 3 additions & 2 deletions test/src/odr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <optional>
#include <set>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -115,9 +116,9 @@ TEST(odr, a_named_file_in_memory_is_offered_its_type) {

TEST(FileTypeTable, covers_every_file_type_exactly_once) {
const std::vector<FileType> expected = every_file_type();
const std::vector<FileType> actual = all_file_types();
const std::span<const FileType> actual = all_file_types();

EXPECT_EQ(actual, expected);
EXPECT_TRUE(std::ranges::equal(actual, expected));

for (const FileType type : expected) {
EXPECT_NE(file_type_to_string(type), "unnamed")
Expand Down
Loading