#lexicographically #string #lexical #characters #ascii #case #halfway

bin+lib midstring

在两个其他字符串之间创建一个字符串,该字符串在字典序上位于它们之间

4 个版本

0.1.3 2024 年 7 月 22 日
0.1.2 2024 年 7 月 22 日
0.1.1 2024 年 7 月 22 日
0.1.0 2024 年 7 月 22 日

#423文本处理

Download history 278/week @ 2024-07-20 93/week @ 2024-07-27

371 每月下载次数

MIT 许可证

13KB
111

此 Rust crate 用于创建两个其他字符串之间的字符串,该字符串在字典序上位于它们之间。

// "aaa" |
//       | -> creates "aan"
// "aaz" |

这对于仅通过一次更新对数据库中的项目进行重新排序非常有用。获取左右键字符串(即用于排序的字符串),然后使用 mid_string() 函数创建新的键字符串。然后将此键字符串分配给位于前两个项目之间的新项目。如果按此键排序,它应该可以正确排序。

重要!目前,此功能仅适用于小写英文字符:[a ~ z]。其他字符可能导致不可预测的行为。

此库基于 "m69" 对以下 stackoverflow.com 问题的回答

https://stackoverflow.com/questions/38923376/return-a-new-string-that-sorts-between-two-given-strings

问题:返回一个介于两个给定字符串之间的新字符串

由 "m69 snarky and unwelcoming" 提供的答案:https://stackoverflow.com/a/38927158/1762976

示例

use midstring::mid_string;

// create new strings from empty strings
println!("_, i => {}", mid_string("", "i")); // -> e
println!("_, b => {}", mid_string("", "b")); // -> an
println!("_, _ => {}", mid_string("", "")); // -> n

assert_eq!(mid_string("", "i"), "e");
assert_eq!(mid_string("", "b"), "an");
assert_eq!(mid_string("", ""), "n");

// create new strings between other strings
assert_eq!(mid_string("aaa", "aaz"), "aan");
assert_eq!(mid_string("abc", "abcab"), "abcaan");

assert_eq!(mid_string(&String::from("abcde"), "abchi"), String::from("abcf"));

let left = String::from("abc");
let right = "abcab".to_string();
let should_be = String::from("abcaan");
assert_eq!(mid_string(&left, &right), should_be);

无运行时依赖