Sha256: a60c93d043c3f90dbee3e3b792fbc8dcb98fc2596c1a2d478baaccc7342d04b3

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

use std::path::MAIN_SEPARATOR;

static SEP: u8 = MAIN_SEPARATOR as u8;

pub fn extract_last_path_segment(path: &str) -> &str {
  // Works with bytes directly because MAIN_SEPARATOR is always in the ASCII 7-bit range so we can
  // avoid the overhead of full UTF-8 processing.
  // See src/benches/path_parsing.rs for benchmarks of different approaches.
  let ptr = path.as_ptr();
  let mut i = path.len() as isize - 1;
  while i >= 0 {
    let c = unsafe { *ptr.offset(i) };
    if c != SEP { break; };
    i -= 1;
  }
  let end = (i + 1) as usize;
  while i >= 0 {
    let c = unsafe { *ptr.offset(i) };
    if c == SEP {
      return &path[(i + 1) as usize..end];
    };
    i -= 1;
  }
  &path[..end]
}


// Returns the byte offset of the last byte preceding a MAIN_SEPARATOR.
pub fn last_non_sep_i(path: &str) -> isize {
  last_non_sep_i_before(path, path.len() as isize - 1)
}

// Returns the byte offset of the last byte preceding a MAIN_SEPARATOR before the given end offset.
pub fn last_non_sep_i_before(path: &str, end: isize) -> isize {
  let ptr = path.as_ptr();
  let mut i = end;
  while i >= 0 {
    if unsafe { *ptr.offset(i) } != SEP { break; };
    i -= 1;
  }
  i
}

// Returns the byte offset of the last MAIN_SEPARATOR before the given end offset.
pub fn last_sep_i(path: &str, end: isize) -> isize {
  let ptr = path.as_ptr();
  let mut i = end - 1;
  while i >= 0 {
    if unsafe { *ptr.offset(i) } == SEP {
      return i;
    };
    i -= 1;
  }
  -1
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
faster_path-0.2.2 src/path_parsing.rs
faster_path-0.2.1 src/path_parsing.rs
faster_path-0.2.0 src/path_parsing.rs
faster_path-0.1.13 src/path_parsing.rs
faster_path-0.1.12 src/path_parsing.rs
faster_path-0.1.11 src/path_parsing.rs
faster_path-0.1.10 src/path_parsing.rs