blob: 88f499c47f179bb5e2c4b3ef4c07531d3f2d8f16 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#pragma once
#include <cstddef>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include "esp_partition.h"
#include "strxfrm.h"
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<std::string> = 0;
virtual auto Transform(const std::string&) -> std::string = 0;
};
/* Creates and returns the best available collator. */
auto CreateCollator() -> std::unique_ptr<ICollator>;
/*
* Collator that doesn't do anything. Used only when there is no available
* locale data.
*/
class NoopCollator : public ICollator {
public:
auto Describe() -> std::optional<std::string> override { return {}; }
auto Transform(const std::string& in) -> std::string override { return in; }
};
/*
* 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<std::string> override { return name_; }
auto Transform(const std::string& in) -> std::string override;
private:
GLibCollator(const std::string& name,
const esp_partition_mmap_handle_t,
std::unique_ptr<locale_data_t>);
const std::string name_;
const esp_partition_mmap_handle_t handle_;
std::unique_ptr<locale_data_t> locale_data_;
};
} // namespace locale
|