File: canonicalize.cpp

package info (click to toggle)
toml11 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,464 kB
  • sloc: cpp: 38,446; makefile: 8; sh: 5
file content (79 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
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
76
77
78
79
#include <toml.hpp>
#include <uni_algo/norm.h>

#include <iostream>
#include <map>

struct nfc_comparator
{
    using first_argument_type  = std::string;
    using second_argument_type = std::string;
    using result_type          = bool;

    result_type operator()(const first_argument_type& lhs, const second_argument_type& rhs) const
    {
        return una::norm::to_nfc_utf8(lhs) < una::norm::to_nfc_utf8(rhs);
    }
};

struct nfc_equal_to
{
    using first_argument_type  = std::string;
    using second_argument_type = std::string;
    using result_type          = bool;

    result_type operator()(const first_argument_type& lhs, const second_argument_type& rhs) const
    {
        return una::norm::to_nfc_utf8(lhs) == una::norm::to_nfc_utf8(rhs);
    }
};

struct nfc_hasher
{
    std::size_t operator()(const std::string& s) const
    {
        return std::hash<std::string>{}(una::norm::to_nfc_utf8(s));
    }
};

struct nfc_config
{
    using comment_type  = toml::preserve_comments;

    using boolean_type  = bool;
    using integer_type  = std::int64_t;
    using floating_type = double;
    using string_type   = std::string;

    template<typename T>
    using array_type = std::vector<T>;
    template<typename K, typename T>
    // using table_type = std::map<K, T, nfc_comparator>;
    using table_type = std::unordered_map<K, T, nfc_hasher, nfc_equal_to>;

    static toml::result<integer_type, toml::error_info>
    parse_int(const std::string& str, const toml::source_location src, const std::uint8_t base)
    {
        return toml::read_int<integer_type>(str, src, base);
    }
    static toml::result<floating_type, toml::error_info>
    parse_float(const std::string& str, const toml::source_location src, const bool is_hex)
    {
        return toml::read_float<floating_type>(str, src, is_hex);
    }
};


int main(int argc, char **argv)
{
    if(argc != 2)
    {
        return 1;
    }

    const auto input = toml::parse<nfc_config>(argv[1]);

    std::cout << toml::format(input) << std::endl;

    return 0;
}