From 4cc5fa4c9c324c18908a7786358c7f6cac8a7f82 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 31 Oct 2023 09:01:34 +1100 Subject: Store the current collator in the database --- src/locale/include/collation.hpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src/locale/include') diff --git a/src/locale/include/collation.hpp b/src/locale/include/collation.hpp index e8b6fa17..b666860d 100644 --- a/src/locale/include/collation.hpp +++ b/src/locale/include/collation.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include "esp_partition.h" @@ -17,31 +18,56 @@ namespace locale { +/* + * Interface for sorting database entries. + * For performance, our database exclusively orders entries via byte + * comparisons of each key. Our collators therefore work by transforming keys + * such that a byte-order comparison results in a natural ordering. + */ class ICollator { public: virtual ~ICollator() {} + /* + * Returns an identify that uniquely describes this collator. Does not need + * to be human readable. + */ + virtual auto Describe() -> std::optional = 0; virtual auto Transform(const std::string&) -> std::string = 0; }; +/* Creates and returns the best available collator. */ +auto CreateCollator() -> std::unique_ptr; + +/* + * Collator that doesn't do anything. Used only when there is no available + * locale data. + */ class NoopCollator : public ICollator { public: + auto Describe() -> std::optional override { return {}; } auto Transform(const std::string& in) -> std::string override { return in; } }; -auto CreateCollator() -> std::unique_ptr; - +/* + * Collator that uses glibc's `strxfrm` to transform keys. Relies on an + * LC_COLLATE file (+ 8 byte name header) flashed on a partition in internal + * flash. + */ class GLibCollator : public ICollator { public: static auto create() -> GLibCollator*; ~GLibCollator(); + auto Describe() -> std::optional override { return name_; } auto Transform(const std::string& in) -> std::string override; private: - GLibCollator(const esp_partition_mmap_handle_t, + GLibCollator(const std::string& name, + const esp_partition_mmap_handle_t, std::unique_ptr); + const std::string name_; const esp_partition_mmap_handle_t handle_; std::unique_ptr locale_data_; }; -- cgit v1.2.3