mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 02:08:09 +08:00
feat: add examples
This commit is contained in:
1
examples/unicode/.gitignore
vendored
Normal file
1
examples/unicode/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
canonicalize
|
||||
15
examples/unicode/CMakeLists.txt
Normal file
15
examples/unicode/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(uni-algo
|
||||
GIT_REPOSITORY https://github.com/uni-algo/uni-algo.git
|
||||
GIT_SHALLOW ON # Download the branch without its history
|
||||
GIT_TAG v1.0.0) # The version you want to download
|
||||
|
||||
# Be aware that FetchContent_MakeAvailable requires CMake 3.14 or higher
|
||||
FetchContent_MakeAvailable(uni-algo)
|
||||
|
||||
add_executable(canonicalize canonicalize.cpp)
|
||||
target_link_libraries(canonicalize PRIVATE toml11::toml11 uni-algo::uni-algo)
|
||||
set_target_properties(canonicalize PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
15
examples/unicode/README.md
Normal file
15
examples/unicode/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# reflect
|
||||
|
||||
Compare TOML key after NFC canonicalization.
|
||||
|
||||
It depends on [uni-algo](https://github.com/uni-algo/uni-algo.git).
|
||||
|
||||
The example contains two keys that are different in bytewise comparison, but becomes the same after NFC normalization.
|
||||
|
||||
## build
|
||||
|
||||
Build toml11 with `-DTOML11_BUILD_EXAMPLES=ON`.
|
||||
|
||||
```cpp
|
||||
$ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON
|
||||
```
|
||||
79
examples/unicode/canonicalize.cpp
Normal file
79
examples/unicode/canonicalize.cpp
Normal file
@@ -0,0 +1,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;
|
||||
}
|
||||
2
examples/unicode/input.toml
Normal file
2
examples/unicode/input.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
"W\u0302" = 1 # in NFC, this key is the same as the following key
|
||||
"Ŵ" = 2 # so it fails.
|
||||
Reference in New Issue
Block a user