6个版本 (3个稳定版)
2.0.0 | 2023年2月1日 |
---|---|
1.0.1 | 2022年4月10日 |
1.0.0 | 2022年2月4日 |
0.1.0-dev.1 | 2021年11月30日 |
0.0.0-reserve.0 | 2021年11月29日 |
#1020 在 编码
每月下载量436
在 12 个crate中使用 (直接使用3个)
29KB
615 行
ordered-varint
提供字节序可排序的可变长度有符号和无符号整数编码。
该crate提供Variable
trait,它将整数编码和解码为1到16字节的长度的缩写格式。所有有符号整数类型(i8、i16、i32、i64和i128)之间可以相互比较,所有无符号整数类型(u8、u16、u32、u64和u128)之间也可以相互比较。 但是,编码的有符号和无符号值不能有意义地相互比较。
示例
要运行包含的示例,请执行 cargo run --example demo
。输出将类似于以下内容
Original bytes: 1968
Encoded bytes: 1068
1 encodes as [01]
3 encodes as [03]
7 encodes as [07]
15 encodes as [0f]
31 encodes as [10, 1f]
63 encodes as [10, 3f]
127 encodes as [10, 7f]
255 encodes as [10, ff]
511 encodes as [11, ff]
[...]
1329227995784915872903807060280344575 encodes as [f0, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
2658455991569831745807614120560689151 encodes as [f1, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
5316911983139663491615228241121378303 encodes as [f3, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
10633823966279326983230456482242756607 encodes as [f7, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
运行以下命令可以获得显示有符号编码的另一个示例: cargo run --example demo-signed
。输出将类似于以下内容
Original bytes: 3920
Encoded bytes: 2165
-5316911983139663491615228241121378305 encodes as [03, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
-2658455991569831745807614120560689153 encodes as [05, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
-1329227995784915872903807060280344577 encodes as [06, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
-664613997892457936451903530140172289 encodes as [07, 7f, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
[...]
-65 encodes as [77, bf]
-33 encodes as [77, df]
-17 encodes as [77, ef]
-9 encodes as [77, f7]
-5 encodes as [7b]
-3 encodes as [7d]
1 encodes as [81]
3 encodes as [83]
7 encodes as [87]
15 encodes as [88, 0f]
31 encodes as [88, 1f]
63 encodes as [88, 3f]
[...]
1329227995784915872903807060280344575 encodes as [f8, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
2658455991569831745807614120560689151 encodes as [f9, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
5316911983139663491615228241121378303 encodes as [fb, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
10633823966279326983230456482242756607 encodes as [ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff]
开源许可证
该项目,如所有来自Khonsu Labs的项目一样,是开源的。此仓库可在MIT许可证或Apache许可证2.0下获得。
有关贡献的更多信息,请参阅CONTRIBUTING.md。