#slice #string #utf-8 #substring #unicode

no-std stringslice

A collection of methods to slice strings based on character indices rather than bytes

4 releases

Uses old Rust 2015

0.2.0 Jan 1, 2024
0.1.2 Mar 20, 2021
0.1.1 Mar 17, 2021
0.1.0 Mar 17, 2021

#282 in Parser tooling

Download history 2655/week @ 2024-03-14 2464/week @ 2024-03-21 2427/week @ 2024-03-28 1346/week @ 2024-04-04 1478/week @ 2024-04-11 1712/week @ 2024-04-18 1732/week @ 2024-04-25 1368/week @ 2024-05-02 3118/week @ 2024-05-09 2252/week @ 2024-05-16 2025/week @ 2024-05-23 2057/week @ 2024-05-30 1871/week @ 2024-06-06 1346/week @ 2024-06-13 2206/week @ 2024-06-20 1795/week @ 2024-06-27

7,679 downloads per month

MIT/Apache

12KB
98 lines

stringslice   Test statusTest coverageCrate versionRust version

A collection of methods to slice strings based on character indices rather than bytes.

This crate implements the StringSlice trait for &str, containing the slice, try_slice, substring, and try_substring methods.

Features

  • Uses primitive &str and standard String types
  • #[no_std] compatible by default
  • Small footprint
    • ~50 LoC excluding blank lines, comments, and tests
    • No additional dependencies (only dev-dependencies)

Usage

Add stringslice to your Cargo.toml file:

[dependencies]
stringslice = "0.2"

Examples

The slice method can be used to slice a &str.

use stringslice::StringSlice;

assert_eq!("Ùníc😎de".slice(4..5), "😎");
assert_eq!("世界こんにちは".slice(2..), "こんにちは");

The substring method is provided for convenience and accepts separate parameters for the start and end of the slice.

use stringslice::StringSlice;

assert_eq!("Γεια σου κόσμε".substring(9, 14), "κόσμε");

There are also equivalent try_slice and try_substring methods which return None for invalid input.

use stringslice::StringSlice;

assert_eq!("string".try_slice(4..2), None);

Licence

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps