rust/hg-core/src/utils.rs
author Raphaël Gomès <rgomes@octobus.net>
Fri, 13 Mar 2020 00:41:22 +0100
changeset 44562 ece43c79333e
parent 44268 aa0fc32ece9e
child 44973 26114bd6ec60
permissions -rw-r--r--
rust-core: add missing `Debug` traits Turns out you need them when trying to use `hg-core` as a library. Who knew. Differential Revision: https://phab.mercurial-scm.org/D8278
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42751
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     1
// utils module
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     2
//
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     3
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     4
//
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     5
// This software may be used and distributed according to the terms of the
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     6
// GNU General Public License version 2 or any later version.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     7
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     8
//! Contains useful functions, traits, structs, etc. for use in core.
4b3b27d567d5 rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents: 42748
diff changeset
     9
44268
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
    10
use crate::utils::hg_path::HgPath;
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
    11
use std::{io::Write, ops::Deref};
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
    12
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
pub mod files;
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Raphaël Gomès <rgomes@octobus.net>
parents: 42863
diff changeset
    14
pub mod hg_path;
44265
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Raphaël Gomès <rgomes@octobus.net>
parents: 44079
diff changeset
    15
pub mod path_auditor;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
44079
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    17
/// Useful until rust/issues/56345 is stable
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    18
///
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    19
/// # Examples
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    20
///
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    21
/// ```
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    22
/// use crate::hg::utils::find_slice_in_slice;
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    23
///
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    24
/// let haystack = b"This is the haystack".to_vec();
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    25
/// assert_eq!(find_slice_in_slice(&haystack, b"the"), Some(8));
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    26
/// assert_eq!(find_slice_in_slice(&haystack, b"not here"), None);
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    27
/// ```
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    28
pub fn find_slice_in_slice<T>(slice: &[T], needle: &[T]) -> Option<usize>
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    29
where
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    30
    for<'a> &'a [T]: PartialEq,
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    31
{
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    32
    slice
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    33
        .windows(needle.len())
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    34
        .position(|window| window == needle)
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    35
}
191a461d6be6 rust-utils: add util to find a slice in another slice
Raphaël Gomès <rgomes@octobus.net>
parents: 42956
diff changeset
    36
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    37
/// Replaces the `from` slice with the `to` slice inside the `buf` slice.
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    38
///
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    39
/// # Examples
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    40
///
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    41
/// ```
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    42
/// use crate::hg::utils::replace_slice;
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    43
/// let mut line = b"I hate writing tests!".to_vec();
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    44
/// replace_slice(&mut line, b"hate", b"love");
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    45
/// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    46
///     line,
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    47
///     b"I love writing tests!".to_vec()
42841
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42799
diff changeset
    48
/// );
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    49
/// ```
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
pub fn replace_slice<T>(buf: &mut [T], from: &[T], to: &[T])
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
where
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
    T: Clone + PartialEq,
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
{
42611
2f760da140ee rust-utils: remove buggy assertion
Raphaël Gomès <rgomes@octobus.net>
parents: 42610
diff changeset
    54
    if buf.len() < from.len() || from.len() != to.len() {
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
        return;
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
    }
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    for i in 0..=buf.len() - from.len() {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
        if buf[i..].starts_with(from) {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
            buf[i..(i + from.len())].clone_from_slice(to);
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
        }
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    }
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
}
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
pub trait SliceExt {
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    65
    fn trim_end(&self) -> &Self;
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    66
    fn trim_start(&self) -> &Self;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
    fn trim(&self) -> &Self;
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
    68
    fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
}
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
fn is_not_whitespace(c: &u8) -> bool {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
    !(*c as char).is_whitespace()
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
}
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
impl SliceExt for [u8] {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    fn trim_end(&self) -> &[u8] {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
        if let Some(last) = self.iter().rposition(is_not_whitespace) {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
            &self[..last + 1]
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
        } else {
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
            &[]
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
        }
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
    }
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    83
    fn trim_start(&self) -> &[u8] {
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    84
        if let Some(first) = self.iter().position(is_not_whitespace) {
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    85
            &self[first..]
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    86
        } else {
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    87
            &[]
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    88
        }
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    89
    }
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    90
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    91
    /// ```
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    92
    /// use hg::utils::SliceExt;
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    93
    /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    94
    ///     b"  to trim  ".trim(),
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    95
    ///     b"to trim"
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    96
    /// );
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    97
    /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    98
    ///     b"to trim  ".trim(),
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    99
    ///     b"to trim"
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   100
    /// );
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   101
    /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   102
    ///     b"  to trim".trim(),
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   103
    ///     b"to trim"
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   104
    /// );
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   105
    /// ```
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   106
    fn trim(&self) -> &[u8] {
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   107
        self.trim_start().trim_end()
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   108
    }
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   109
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   110
    fn drop_prefix(&self, needle: &Self) -> Option<&Self> {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   111
        if self.starts_with(needle) {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   112
            Some(&self[needle.len()..])
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   113
        } else {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   114
            None
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   115
        }
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
   116
    }
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
}
44268
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   118
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   119
pub trait Escaped {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   120
    /// Return bytes escaped for display to the user
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   121
    fn escaped_bytes(&self) -> Vec<u8>;
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   122
}
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   123
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   124
impl Escaped for u8 {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   125
    fn escaped_bytes(&self) -> Vec<u8> {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   126
        let mut acc = vec![];
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   127
        match self {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   128
            c @ b'\'' | c @ b'\\' => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   129
                acc.push(b'\\');
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   130
                acc.push(*c);
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   131
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   132
            b'\t' => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   133
                acc.extend(br"\\t");
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   134
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   135
            b'\n' => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   136
                acc.extend(br"\\n");
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   137
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   138
            b'\r' => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   139
                acc.extend(br"\\r");
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   140
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   141
            c if (*c < b' ' || *c >= 127) => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   142
                write!(acc, "\\x{:x}", self).unwrap();
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   143
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   144
            c => {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   145
                acc.push(*c);
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   146
            }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   147
        }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   148
        acc
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   149
    }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   150
}
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   151
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   152
impl<'a, T: Escaped> Escaped for &'a [T] {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   153
    fn escaped_bytes(&self) -> Vec<u8> {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   154
        self.iter().flat_map(|item| item.escaped_bytes()).collect()
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   155
    }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   156
}
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   157
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   158
impl<T: Escaped> Escaped for Vec<T> {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   159
    fn escaped_bytes(&self) -> Vec<u8> {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   160
        self.deref().escaped_bytes()
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   161
    }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   162
}
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   163
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   164
impl<'a> Escaped for &'a HgPath {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   165
    fn escaped_bytes(&self) -> Vec<u8> {
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   166
        self.as_bytes().escaped_bytes()
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   167
    }
aa0fc32ece9e rust-utils: add `Escaped` trait
Raphaël Gomès <rgomes@octobus.net>
parents: 44265
diff changeset
   168
}