2 个不稳定版本
| 0.2.0 | 2023 年 2 月 5 日 |
|---|---|
| 0.1.0 | 2021 年 7 月 19 日 |
#230 在 配置
35 每月下载量
用于 2 crates
27KB
496 行
Ini 流式解析器
兼容 no_std。
这个库实现了一个非常基础但非常快速的流式 INI 解析器。
示例
use ini_core as ini;
let document = "\
[SECTION]
;this is a comment
Key=Value";
let elements = [
ini::Item::SectionEnd,
ini::Item::Section("SECTION"),
ini::Item::Comment("this is a comment"),
ini::Item::Property("Key", Some("Value")),
ini::Item::SectionEnd,
];
for (index, item) in ini::Parser::new(document).enumerate() {
assert_eq!(item, elements[index]);
}
SectionEnd 伪元素在新的部分之前和文档末尾返回。这有助于在解析完属性后处理部分。
解析器非常基于行,它会继续进行,并返回作为项目的无意义内容
use ini_core as ini;
let document = "\
[SECTION
nonsense";
let elements = [
ini::Item::SectionEnd,
ini::Item::Error("[SECTION"),
ini::Item::Property("nonsense", None),
ini::Item::SectionEnd,
];
for (index, item) in ini::Parser::new(document).enumerate() {
assert_eq!(item, elements[index]);
}
以 [ 开头的行,但既没有闭合的 ] 也没有后跟换行的闭合 ],作为 Item::Error 返回。缺少 = 的行作为 Item::Property 返回,其值为 None。
格式
INI 不是一个良好指定的格式,这个解析器尽量少做假设,但确实做出了决定。
- 换行符可以是
"\r\n"、"\n"或"\r"。它们可以在单个文档中混合使用,但这不是推荐的。 - 部分标题是
"" section " 换行符。section可以是任何内容,但不能包含换行符。 - 属性是
key "=" value 换行符。key和value可以是任何内容,但不能包含换行符。 - 注释是
";" comment newline,空白则是仅仅newline。注释字符可以自定义。
请注意,默认情况下不会去除填充空格:节 [ SECTION ] 的名称是 <space>SECTION<space>。属性 KEY = VALUE 有键 KEY<space> 和值 <space>VALUE。注释 ; comment 的注释是 <space>comment。
输入不会进行进一步处理,例如,如果需要转义序列,则必须由用户处理。
性能
与其他在crates.io上可用的INI解析器进行了测试。警告:除 light_ini 之外的所有解析器都使用文档模型,使用 HashMap 和 String 分配内存,这并不完全公平...无论如何,ini_core 仍然以回调为基础的流式解析器 light_ini 为标准。
这些测试是在AMD Ryzen 5 3600X上,使用 -C target-cpu=native 运行的。
解析大型INI文件(241 KB,7640行)
running 5 tests
test configparser ... bench: 1,921,162 ns/iter (+/- 207,479) = 128 MB/s
test ini_core ... bench: 99,860 ns/iter (+/- 2,396) = 2472 MB/s
test light_ini ... bench: 320,363 ns/iter (+/- 33,904) = 770 MB/s
test simpleini ... bench: 7,814,605 ns/iter (+/- 271,016) = 31 MB/s
test tini ... bench: 2,613,870 ns/iter (+/- 188,756) = 94 MB/s
test result: ok. 0 passed; 0 failed; 0 ignored; 5 measured; 0 filtered out; finished in 19.99s
解析小型INI文件(17.2 KB,800行)
running 5 tests
test configparser ... bench: 266,602 ns/iter (+/- 21,394) = 66 MB/s
test ini_core ... bench: 8,179 ns/iter (+/- 845) = 2159 MB/s
test light_ini ... bench: 149,990 ns/iter (+/- 16,379) = 117 MB/s
test simpleini ... bench: 563,204 ns/iter (+/- 55,080) = 31 MB/s
test tini ... bench: 340,383 ns/iter (+/- 28,667) = 51 MB/s
test result: ok. 0 passed; 0 failed; 0 ignored; 5 measured; 0 filtered out; finished in 15.75s
竞争者连边都沾不上。😎
许可证
根据 MIT许可证 许可,请参阅 license.txt。
贡献
除非你明确表示,否则你提交的任何有意包含在作品中的贡献,都应按照上述方式许可,不附加任何其他条款或条件。