]> git.lizzy.rs Git - rust.git/commitdiff
Bit-magic for faster is_char_boundary
authorRaph Levien <raph@google.com>
Sun, 10 Apr 2016 00:11:20 +0000 (17:11 -0700)
committerRaph Levien <raph@google.com>
Sun, 10 Apr 2016 00:16:54 +0000 (17:16 -0700)
The asm generated for b < 128 || b >= 192 is not ideal, as it computes
both sub-inequalities. This patch replaces it with bit magic.

Fixes #32471

src/libcore/str/mod.rs

index 305546df5be2da171140dd5aaad372dae1ea6ba2..f1be10da872411e80e6ffead263c559e91e89696 100644 (file)
@@ -1940,7 +1940,8 @@ fn is_char_boundary(&self, index: usize) -> bool {
         if index == 0 || index == self.len() { return true; }
         match self.as_bytes().get(index) {
             None => false,
-            Some(&b) => b < 128 || b >= 192,
+            // This is bit magic equivalent to: b < 128 || b >= 192
+            Some(&b) => (b as i8) >= -0x40,
         }
     }