mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
doc: add Fuzzy search section to README
This commit is contained in:
85
README.md
85
README.md
@@ -270,6 +270,46 @@ const auto color = toml::find<std::string>(data, "fruit", "physical", "color");
|
|||||||
const auto shape = toml::find<std::string>(data, "fruit", "physical", "shape");
|
const auto shape = toml::find<std::string>(data, "fruit", "physical", "shape");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Dotted keys
|
||||||
|
|
||||||
|
TOML v0.5.0 has a new feature named "dotted keys".
|
||||||
|
You can chain keys to represent the structure of the data.
|
||||||
|
|
||||||
|
```toml
|
||||||
|
physical.color = "orange"
|
||||||
|
physical.shape = "round"
|
||||||
|
```
|
||||||
|
|
||||||
|
This is equivalent to the following.
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[physical]
|
||||||
|
color = "orange"
|
||||||
|
shape = "round"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can get both of the above tables with the same c++ code.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
const auto physical = toml::find(data, "physical");
|
||||||
|
const auto color = toml::find<std::string>(physical, "color");
|
||||||
|
```
|
||||||
|
|
||||||
|
The following code does not work for the above toml file.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// XXX this does not work!
|
||||||
|
const auto color = toml::find<std::string>(data, "physical.color");
|
||||||
|
```
|
||||||
|
|
||||||
|
The above code works with the following toml file.
|
||||||
|
|
||||||
|
```toml
|
||||||
|
"physical.color" = "orange"
|
||||||
|
# equivalent to {"physical.color": "orange"},
|
||||||
|
# NOT {"physical": {"color": "orange"}}.
|
||||||
|
```
|
||||||
|
|
||||||
### In case of error
|
### In case of error
|
||||||
|
|
||||||
If the value does not exist, `toml::find` throws an error with the location of
|
If the value does not exist, `toml::find` throws an error with the location of
|
||||||
@@ -310,46 +350,39 @@ shared by `toml::value`s and remains on the heap memory. It is recommended to
|
|||||||
destruct all the `toml::value` classes after configuring your application
|
destruct all the `toml::value` classes after configuring your application
|
||||||
if you have a large TOML file compared to the memory resource.
|
if you have a large TOML file compared to the memory resource.
|
||||||
|
|
||||||
### Dotted keys
|
### Fuzzy Search
|
||||||
|
|
||||||
TOML v0.5.0 has a new feature named "dotted keys".
|
To find a value, you can use `find_fuzzy` instead of `find`.
|
||||||
You can chain keys to represent the structure of the data.
|
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
physical.color = "orange"
|
# typo!
|
||||||
physical.shape = "round"
|
anseer = 42
|
||||||
```
|
```
|
||||||
|
|
||||||
This is equivalent to the following.
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[physical]
|
|
||||||
color = "orange"
|
|
||||||
shape = "round"
|
|
||||||
```
|
|
||||||
|
|
||||||
You can get both of the above tables with the same c++ code.
|
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
const auto physical = toml::find(data, "physical");
|
const auto data = toml::parse("sample.toml");
|
||||||
const auto color = toml::find<std::string>(physical, "color");
|
const auto answer = toml::find_fuzzy<int>(data, "answer"); // it finds "anseer".
|
||||||
```
|
```
|
||||||
|
|
||||||
The following code does not work for the above toml file.
|
When the specified key is not found, `toml::find_fuzzy` calculates
|
||||||
|
[levenstein distance](https://en.wikipedia.org/wiki/Levenshtein_distance)
|
||||||
|
between the specified key and other keys.
|
||||||
|
If it finds a key that is 1 away from the specified key by the Levenstein
|
||||||
|
distance, it returns the corresponding value.
|
||||||
|
|
||||||
```cpp
|
However, in many cases rather than just allowing typographical errors,
|
||||||
// XXX this does not work!
|
you will want to suggest a message and encouledge users to correct it.
|
||||||
const auto color = toml::find<std::string>(data, "physical.color");
|
|
||||||
```
|
|
||||||
|
|
||||||
The above code works with the following toml file.
|
If you pass a `FuzzyMatcher` to `toml::find`, a suggestion will be displayed
|
||||||
|
in the error message.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
"physical.color" = "orange"
|
toml::levenstein_matcher lev(1); // finds keys within distance <= 1
|
||||||
# equivalent to {"physical.color": "orange"},
|
const auto answer = toml::find<int>(data, "answer"); // it throws!
|
||||||
# NOT {"physical": {"color": "orange"}}.
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note: Currently, it receives only one key.
|
||||||
|
|
||||||
## Casting a toml value
|
## Casting a toml value
|
||||||
|
|
||||||
### `toml::get`
|
### `toml::get`
|
||||||
|
Reference in New Issue
Block a user