about summary refs log tree commit diff
path: root/src/rustllvm/ExecutionEngineWrapper.cpp
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2016-03-23 21:57:44 +0100
committerUlrik Sverdrup <bluss@users.noreply.github.com>2016-03-24 01:25:59 +0100
commitf621193e5ea7ac54fcd37f0e730e955fd9f61200 (patch)
tree09139c7dad6b0c26ff29c5ca25c439bd5a40ec32 /src/rustllvm/ExecutionEngineWrapper.cpp
parent80e7a1be359fc0de4eb17056230ae2fc130b7090 (diff)
downloadrust-f621193e5ea7ac54fcd37f0e730e955fd9f61200.tar.gz
rust-f621193e5ea7ac54fcd37f0e730e955fd9f61200.zip
Accept 0 as a valid str char boundary
Index 0 must be a valid char boundary (invariant of str that it contains
valid UTF-8 data).

If we check explicitly for index == 0, that removes the need to read the
byte at index 0, so it avoids a trip to the string's memory, and it
optimizes out the slicing index' bounds check whenever it is zero.

With this change, the following examples all change from having a read of
the byte at 0 and a branch to possibly panicing, to having the bounds
checking optimized away.

```rust
pub fn split(s: &str) -> (&str, &str) {
    s.split_at(0)
}

pub fn both(s: &str) -> &str {
    &s[0..s.len()]
}

pub fn first(s: &str) -> &str {
    &s[..0]
}

pub fn last(s: &str) -> &str {
    &s[0..]
}
```
Diffstat (limited to 'src/rustllvm/ExecutionEngineWrapper.cpp')
0 files changed, 0 insertions, 0 deletions