Details
Details
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Branch
- default
- Lint
No Linters Available - Unit
No Unit Test Coverage
No Linters Available |
No Unit Test Coverage |
Path | Packages | |||
---|---|---|---|---|
M | rust/hg-core/src/utils.rs (20 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
62441636ec51 | d0a96e98b7b7 | Raphaël Gomès | Jan 14 2020, 12:00 PM |
// utils module | // utils module | ||||
// | // | ||||
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | ||||
// | // | ||||
// This software may be used and distributed according to the terms of the | // This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | // GNU General Public License version 2 or any later version. | ||||
//! Contains useful functions, traits, structs, etc. for use in core. | //! Contains useful functions, traits, structs, etc. for use in core. | ||||
pub mod files; | pub mod files; | ||||
pub mod hg_path; | pub mod hg_path; | ||||
/// Useful until rust/issues/56345 is stable | |||||
/// | |||||
/// # Examples | |||||
/// | |||||
/// ``` | |||||
/// use crate::hg::utils::find_slice_in_slice; | |||||
/// | |||||
/// let haystack = b"This is the haystack".to_vec(); | |||||
/// assert_eq!(find_slice_in_slice(&haystack, b"the"), Some(8)); | |||||
/// assert_eq!(find_slice_in_slice(&haystack, b"not here"), None); | |||||
/// ``` | |||||
pub fn find_slice_in_slice<T>(slice: &[T], needle: &[T]) -> Option<usize> | |||||
where | |||||
for<'a> &'a [T]: PartialEq, | |||||
{ | |||||
slice | |||||
.windows(needle.len()) | |||||
.position(|window| window == needle) | |||||
} | |||||
/// Replaces the `from` slice with the `to` slice inside the `buf` slice. | /// Replaces the `from` slice with the `to` slice inside the `buf` slice. | ||||
/// | /// | ||||
/// # Examples | /// # Examples | ||||
/// | /// | ||||
/// ``` | /// ``` | ||||
/// use crate::hg::utils::replace_slice; | /// use crate::hg::utils::replace_slice; | ||||
/// let mut line = b"I hate writing tests!".to_vec(); | /// let mut line = b"I hate writing tests!".to_vec(); | ||||
/// replace_slice(&mut line, b"hate", b"love"); | /// replace_slice(&mut line, b"hate", b"love"); |