1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/*!
# Alphanumeric Sort
This crate can help you sort order for files and folders whose names contain numerals.
## Motives and Examples
With the Rust native `sort` method, strings and paths are arranged into lexicographical order. In some cases, it is not so intuitive. For example, there are screen snap shots named by **shot-%N** like **shot-2**, **shot-1**, **shot-11**. After a lexicographical sorting, they will be ordered into **shot-1**, **shot-11**, **shot-2**. However, we would prefer **shot-1**, **shot-2**, **shot-11** mostly.
```rust
let mut names = ["shot-2", "shot-1", "shot-11"];
names.sort();
assert_eq!(["shot-1", "shot-11", "shot-2"], names);
```
Thus, in this kind of case, an alphanumeric sort might come in handy.
```rust
let mut names = ["shot-2", "shot-1", "shot-11"];
alphanumeric_sort::sort_str_slice(&mut names);
assert_eq!(["shot-1", "shot-2", "shot-11"], names);
```
```rust
# #[cfg(feature = "std")] {
use std::path::Path;
let mut paths = [Path::new("shot-2"), Path::new("shot-1"), Path::new("shot-11")];
alphanumeric_sort::sort_path_slice(&mut paths);
assert_eq!([Path::new("shot-1"), Path::new("shot-2"), Path::new("shot-11")], paths);
# }
```
## About the `compare_*` Functions and the `sort_*` Functions
To sort a slice, the code can also be written like,
```rust
# #[cfg(feature = "std")] {
use std::path::Path;
let mut paths = [Path::new("shot-2"), Path::new("shot-1"), Path::new("shot-11")];
paths.sort_by(|a, b| alphanumeric_sort::compare_path(a, b));
assert_eq!([Path::new("shot-1"), Path::new("shot-2"), Path::new("shot-11")], paths);
# }
```
But it is not recommended because the `compare_*` functions try to convert data (e.g `Path`, `CStr`) to `&str` every time in its execution and thus they are slower than the `sort_*` functions when sorting a slice.
## Version `1.3` to `1.4`
No breaking change in API is made, though the order has some changes.
* `"0001"` is greater than `"001"` instead of being equal.
* `"中"` is greater than `"1"` instead of being less. `"第1章"` is still less than `"第1-2章"`, even though `"章"` is greater than `"-"`.
## No Std
Disable the default features to compile this crate without std.
```toml
[dependencies.alphanumeric-sort]
version = "*"
default-features = false
```
## Benchmark
```bash
cargo bench
```
*/
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
extern crate alloc; // used for sorting
#[cfg(feature = "std")]
mod std_functions;
use core::{cmp::Ordering, str::Chars};
#[cfg(feature = "std")]
pub use std_functions::*;
/// Compare two strings.
pub fn compare_str<A: AsRef<str>, B: AsRef<str>>(a: A, b: B) -> Ordering {
let mut c1 = a.as_ref().chars();
let mut c2 = b.as_ref().chars();
// this flag is to handle something like "1點" < "1-1點"
let mut last_is_number = false;
let mut v1: Option<char> = None;
let mut v2: Option<char> = None;
loop {
let mut ca = {
match v1.take() {
Some(c) => c,
None => match c1.next() {
Some(c) => c,
None => {
if v2.take().is_some() || c2.next().is_some() {
return Ordering::Less;
} else {
return Ordering::Equal;
}
},
},
}
};
let mut cb = {
match v2.take() {
Some(c) => c,
None => match c2.next() {
Some(c) => c,
None => {
return Ordering::Greater;
},
},
}
};
if ca.is_ascii_digit() && cb.is_ascii_digit() {
// count the digit length, but ignore the leading zeros and the following same part (prefix)
let mut la = 1usize;
let mut lb = 1usize;
// this counter is to handle something like "001" > "01"
let mut lc = 0isize;
// find the first non-zero digit in c1
while ca == '0' {
lc += 1;
if let Some(c) = c1.next() {
if c.is_ascii_digit() {
ca = c;
} else {
v1 = Some(c);
la = 0;
break;
}
} else {
la = 0;
break;
}
}
// find the first non-zero digit in c2
while cb == '0' {
lc -= 1;
if let Some(c) = c2.next() {
if c.is_ascii_digit() {
cb = c;
} else {
v2 = Some(c);
lb = 0;
break;
}
} else {
lb = 0;
break;
}
}
// consume the remaining ascii digit
let consume_ascii_digit = |chars: &mut Chars, store: &mut Option<char>| {
let mut counter = 0;
for c in chars.by_ref() {
if c.is_ascii_digit() {
counter += 1;
} else {
*store = Some(c);
break;
}
}
counter
};
let mut ordering = Ordering::Equal;
if la == 0 {
if lb == 0 {
// e.g. 000 vs 000, 000 vs 0000, 0000 vs 000
} else {
// e.g. 0000 vs 001
return Ordering::Less;
}
} else if lb == 0 {
// e.g. 001 vs 0000
return Ordering::Greater;
} else {
// e.g. 1 vs 12, 001 vs 0012
// skip the same prefix and compare the next ascii digit
loop {
ordering = ca.cmp(&cb);
if ordering == Ordering::Equal {
if let Some(c) = c1.next() {
if c.is_ascii_digit() {
if let Some(cc) = c2.next() {
if cc.is_ascii_digit() {
ca = c;
cb = cc;
} else {
return Ordering::Greater;
}
} else {
return Ordering::Greater;
}
} else {
let n = consume_ascii_digit(&mut c2, &mut v2);
v1 = Some(c);
if n > 0 {
return Ordering::Less;
}
break;
}
} else if c2.next().is_some() {
return Ordering::Less;
} else {
break;
}
} else {
la += consume_ascii_digit(&mut c1, &mut v1);
lb += consume_ascii_digit(&mut c2, &mut v2);
if la != lb {
ordering = la.cmp(&lb);
}
break;
}
}
}
if ordering == Ordering::Equal {
match lc.cmp(&0) {
Ordering::Equal => {
last_is_number = true;
},
Ordering::Greater => return Ordering::Greater,
Ordering::Less => return Ordering::Less,
}
} else {
return ordering;
}
} else {
match ca.cmp(&cb) {
Ordering::Equal => last_is_number = false,
Ordering::Greater => {
return if last_is_number && (ca > (255 as char)) ^ (cb > (255 as char)) {
Ordering::Less
} else {
Ordering::Greater
};
},
Ordering::Less => {
return if last_is_number && (ca > (255 as char)) ^ (cb > (255 as char)) {
Ordering::Greater
} else {
Ordering::Less
};
},
}
}
}
}
// TODO -----------
/// Sort a slice by a `str` key, but may not preserve the order of equal elements.
#[inline]
pub fn sort_slice_unstable_by_str_key<A, T: ?Sized + AsRef<str>, F: FnMut(&A) -> &T>(
slice: &mut [A],
mut f: F,
) {
slice.sort_unstable_by(|a, b| compare_str(f(a), f(b)));
}
/// Sort a slice by a `str` key.
#[inline]
pub fn sort_slice_by_str_key<A, T: ?Sized + AsRef<str>, F: FnMut(&A) -> &T>(
slice: &mut [A],
mut f: F,
) {
slice.sort_by(|a, b| compare_str(f(a), f(b)));
}
/// Reversely sort a slice by a `str` key, but may not preserve the order of equal elements.
#[inline]
pub fn sort_slice_rev_unstable_by_str_key<A, T: ?Sized + AsRef<str>, F: FnMut(&A) -> &T>(
slice: &mut [A],
mut f: F,
) {
slice.sort_unstable_by(|a, b| compare_str(f(b), f(a)));
}
/// Reversely sort a slice by a `str` key.
#[inline]
pub fn sort_slice_rev_by_str_key<A, T: ?Sized + AsRef<str>, F: FnMut(&A) -> &T>(
slice: &mut [A],
mut f: F,
) {
slice.sort_by(|a, b| compare_str(f(b), f(a)));
}
// TODO -----------
/// Sort a `str` slice.
#[inline]
pub fn sort_str_slice<S: AsRef<str>>(slice: &mut [S]) {
slice.sort_unstable_by(|a, b| compare_str(a, b));
}
/// Reversely sort a `str` slice.
#[inline]
pub fn sort_str_slice_rev<S: AsRef<str>>(slice: &mut [S]) {
slice.sort_unstable_by(|a, b| compare_str(b, a));
}