human-sort-0.2.2/.gitignore010066400037200003720000000000451345467355200140360ustar0000000000000000/target /.idea **/*.rs.bk Cargo.lock human-sort-0.2.2/.travis.yml010066400037200003720000000002111345467355200141520ustar0000000000000000language: rust cache: cargo script: - cargo test deploy: provider: cargo token: $API_TOKEN on: branch: master tags: true human-sort-0.2.2/Cargo.toml.orig010066400037200003720000000006671345467355200147470ustar0000000000000000[package] name = "human-sort" version = "0.2.2" authors = ["Vlad Nagikh "] license = "MIT" repository = "https://github.com/paradakh/human-sort" description = "Human sort (natural sort) implementation" keywords = ["sort", "natural", "human", "order"] readme = "README.md" edition = "2018" categories = ["algorithms"] [badges] travis-ci = { repository = "paradakh/human-sort", branch = "master" } [dependencies] human-sort-0.2.2/Cargo.toml0000644000000016730000000000000111760ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "human-sort" version = "0.2.2" authors = ["Vlad Nagikh "] description = "Human sort (natural sort) implementation" readme = "README.md" keywords = ["sort", "natural", "human", "order"] categories = ["algorithms"] license = "MIT" repository = "https://github.com/paradakh/human-sort" [dependencies] [badges.travis-ci] branch = "master" repository = "paradakh/human-sort" human-sort-0.2.2/LICENSE010066400037200003720000000020541345467355200130550ustar0000000000000000MIT License Copyright (c) 2019 Vlad Nagikh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. human-sort-0.2.2/README.md010066400037200003720000000016071345467355200133320ustar0000000000000000[![Crates.io](https://img.shields.io/crates/v/human-sort.svg)](https://crates.io/crates/human-sort) [![Build Status](https://travis-ci.org/paradakh/human-sort.svg?branch=master)](https://travis-ci.org/paradakh/human-sort) # human-sort Utilities to sort and compare strings with numeric symbols in human-friendly order. It built over iterators and compare string slices char by char (except for numerals) until the first difference found without creating Strings or another structures with whole data from provided &str, so doesn't require lots of memory. ## Examples ```rust use human_sort::sort; let mut arr = ["file10.txt", "file2.txt", "file1.txt"]; sort(&mut arr); assert_eq!(arr, ["file1.txt", "file2.txt", "file10.txt"]); ``` ```rust use std::cmp::Ordering; use human_sort::compare; assert_eq!(compare("item200", "item3"), Ordering::Greater); ``` ## License Licensed under MIT license. human-sort-0.2.2/README.tpl010066400037200003720000000002711345467355200135250ustar0000000000000000[![Crates.io](https://img.shields.io/crates/v/human-sort.svg)](https://crates.io/crates/human-sort) {{badges}} # {{crate}} {{readme}} ## License Licensed under {{license}} license. human-sort-0.2.2/src/iter_pair.rs010066400037200003720000000010031345467355200151540ustar0000000000000000use std::{iter::Peekable, str::Chars}; pub struct IterPair<'a> { pub fst: Peekable>, pub lst: Peekable>, } impl<'a> IterPair<'a> { pub fn from(i1: Chars<'a>, i2: Chars<'a>) -> Self { Self { fst: i1.peekable(), lst: i2.peekable(), } } pub fn next(&mut self) -> [Option; 2] { [self.fst.next(), self.lst.next()] } pub fn peek(&mut self) -> [Option<&char>; 2] { [self.fst.peek(), self.lst.peek()] } } human-sort-0.2.2/src/lib.rs010066400037200003720000000046721345467355200137630ustar0000000000000000//! Utilities to sort and compare strings with numeric symbols in human-friendly order. //! //! It built over iterators and compare string slices char by char (except for numerals) //! until the first difference found without creating Strings or another structures with whole //! data from provided &str, so doesn't require lots of memory. //! //! # Examples //! //! ``` //! use human_sort::sort; //! //! let mut arr = ["file10.txt", "file2.txt", "file1.txt"]; //! sort(&mut arr); //! //! assert_eq!(arr, ["file1.txt", "file2.txt", "file10.txt"]); //! ``` //! //! ``` //! use std::cmp::Ordering; //! use human_sort::compare; //! //! assert_eq!(compare("item200", "item3"), Ordering::Greater); //! ``` mod iter_pair; use iter_pair::IterPair; use std::{cmp::Ordering, iter::Peekable, str::Chars}; /// Sorts [&str] in human-friendly order /// /// # Example /// /// ``` /// use human_sort::sort; /// /// let mut arr = ["file10.txt", "file2.txt", "file1.txt"]; /// sort(&mut arr); /// /// assert_eq!(arr, ["file1.txt", "file2.txt", "file10.txt"]); /// ``` /// pub fn sort(arr: &mut [&str]) { arr.sort_by(|a, b| compare(a, b)); } /// Compares string slices /// /// # Example /// /// ``` /// use std::cmp::Ordering; /// use human_sort::compare; /// /// assert_eq!(compare("item200", "item3"), Ordering::Greater); /// ``` /// pub fn compare(s1: &str, s2: &str) -> Ordering { compare_chars_iters(s1.chars(), s2.chars()).unwrap_or(s1.cmp(s2)) } /// /// ``` /// use std::cmp::Ordering; /// use human_sort::compare_chars_iters; /// assert_eq!(compare_chars_iters("aaa".chars(), "bbb".chars()), Ok(Ordering::Less)); /// ``` /// pub fn compare_chars_iters<'a>(c1: Chars<'a>, c2: Chars<'a>) -> Result { let mut iters = IterPair::from(c1, c2); while let [Some(x), Some(y)] = iters.peek() { if x == y { iters.next(); } else if x.is_numeric() && y.is_numeric() { match take_numeric(&mut iters.fst).cmp(&take_numeric(&mut iters.lst)) { Ordering::Equal => iters.next(), ref a => return Ok(*a), }; } else { return Ok(x.cmp(y)); } } Err(()) } fn take_numeric(iter: &mut Peekable) -> u32 { let mut sum = 0; while let Some(p) = iter.peek() { match p.to_string().parse::() { Ok(n) => { sum = sum * 10 + n; iter.next(); } _ => break, } } sum } human-sort-0.2.2/.cargo_vcs_info.json0000644000000001120000000000000131630ustar00{ "git": { "sha1": "d3e11b7f122961b45d13a6ff6f5cfe0e0b48a8a0" } }