File: README.md

package info (click to toggle)
rust-substring 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 112 kB
  • sloc: makefile: 4
file content (66 lines) | stat: -rw-r--r-- 2,692 bytes parent folder | download
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
# substring

[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Anders429/substring/Tests)](https://github.com/Anders429/substring/actions)
[![codecov.io](https://img.shields.io/codecov/c/gh/Anders429/substring)](https://codecov.io/gh/Anders429/substring)
[![crates.io](https://img.shields.io/crates/v/substring)](https://crates.io/crates/substring)
[![docs.rs](https://docs.rs/substring/badge.svg)](https://docs.rs/substring)
[![MSRV](https://img.shields.io/badge/rustc-1.0.0+-yellow.svg)](#minimum-supported-rust-version)
[![License](https://img.shields.io/crates/l/substring)](#license)

Substring method for string types.

This crate provides a `substring` method on Rust string types. The method takes a start and end
character index and returns a string slice containing the characters within that range.

The method is provided via the `Substring` trait which is implemented on the
[`str`](https://doc.rust-lang.org/std/primitive.str.html) primitive.

## Usage

To use this crate, simply bring the `Substring` trait into scope and call the `substring` method on
your string types.

```rust
use substring::Substring;

assert_eq!("hello, world!".substring(7, 12), "world");
```

Note that the indexing of substrings is based on
[*Unicode Scalar Value*](http://www.unicode.org/glossary/#unicode_scalar_value). As such,
substrings may not always match your intuition:

```rust
use substring::Substring;

assert_eq!("ã".substring(0, 1), "a");  // As opposed to "ã".
assert_eq!("ã".substring(1, 2), "\u{0303}")
```

The above example occurs because "ã" is technically made up of two UTF-8 scalar values: the letter
"a" and a combining tilde.


## Performance

As Rust strings are UTF-8 encoded, the algorithm for finding a character substring has temporal
complexity *O(n)*, where *n* is the byte length of the string. This is due to characters not being
of predictible byte lengths.

## Minimum Supported Rust Version
This crate is guaranteed to compile on stable `rustc 1.0.0` and up.

## License
This project is licensed under either of

* Apache License, Version 2.0
([LICENSE-APACHE](https://github.com/Anders429/substring/blob/HEAD/LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](https://github.com/Anders429/substring/blob/HEAD/LICENSE-MIT) or
http://opensource.org/licenses/MIT)

at your option.

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