| Crates.io | rk-utils |
| lib.rs | rk-utils |
| version | 0.2.2 |
| created_at | 2024-05-13 08:35:57.959499+00 |
| updated_at | 2024-05-13 15:04:03.918939+00 |
| description | A collection of utility functions and data structures for rust. |
| homepage | |
| repository | https://github.com/rockie/rk-utils |
| max_upload_size | |
| id | 1238158 |
| size | 20,285 |
A collection of utility functions and data structures for rust.
str: String utilities
is_quoted, substring, unquote, url_to_nodes, ensure_prefix, ensure_suffix, drop_prefix, drop_suffix, join_path_segment, join_path_segmentstopo_sort: Topological sortingtrie: Trie data structure for longest matching a path of nodesAdd this to your 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