4个版本
0.2.2 | 2024年5月13日 |
---|---|
0.2.1 | 2024年5月13日 |
0.2.0 | 2024年5月13日 |
0.1.0 | 2024年5月13日 |
#738 in 数据结构
每月 26 次下载
19KB
402 行
rk-utils
一组用于Rust的实用函数和数据结构。
特性
str
:字符串实用工具is_quoted
、substring
、unquote
、url_to_nodes
、ensure_prefix
、ensure_suffix
、drop_prefix
、drop_suffix
、join_path_segment
、join_path_segments
topo_sort
:拓扑排序trie
:用于最长匹配节点路径的Trie数据结构
使用方法
将以下内容添加到您的 Cargo.toml
[dependencies]
rk-utils = "0.2"
示例
use rk_utils::StringUtil;
let s = "'Hello, World!'";
assert_eq!(s.is_quoted(), true);
let s = "'\\'Hello, World!\\''";
assert_eq!(s.unquote(true, None), "'Hello, World!'");
let s = "'Hello, World!'";
assert_eq!(s.substring(1, -1), "Hello, World!");
use rk_utils::topo_sort;
use std::collections::{ HashMap, HashSet };
let mut deps = HashMap::new();
deps.insert("b".to_string(), HashSet::from(["a".to_string()]));
deps.insert("c".to_string(), HashSet::from(["b".to_string()]));
let sorted = topo_sort(&deps).unwrap();
assert_eq!(sorted, ["a", "b", "c"]);
use crate::str::StringUtil;
use crate::trie::Trie;
let mut trie = Trie::new();
let route1 = "/cloud/instance/".url_to_nodes();
let route2 = "/cloud/".url_to_nodes();
let route3 = "/builder/instance".url_to_nodes();
let route4 = "/builder".url_to_nodes();
let route5 = "/".url_to_nodes();
trie.insert(route1, 1);
trie.insert(route2, 2);
trie.insert(route3, 3);
trie.insert(route4, 4);
trie.insert(route5, 5);
let input1 = "/cloud/instance/xxx".url_to_nodes();
assert_eq!(trie.find_longest_match(input1), Some(&1));
let input2 = "/cloud/xxx".url_to_nodes();
assert_eq!(trie.find_longest_match(input2), Some(&2));
let input3 = "/builder/instance/".url_to_nodes();
assert_eq!(trie.find_longest_match(input3), Some(&3));
let input4 = "/fjeao".url_to_nodes();
assert_eq!(trie.find_longest_match(input4), Some(&5));
许可证
MIT