rust/hg-core/src/revlog/path_encode.rs
author Arseniy Alekseyev <aalekseyev@janestreet.com>
Thu, 16 Feb 2023 18:41:06 +0000
changeset 50160 5ce53ff6133a
parent 50159 96d31efd21f7
child 50161 5d7ba99da3b8
permissions -rw-r--r--
rhg: in path_encode add a DestArr type This is an implementation of Sink trait that writes into a fixed-size buffer on the stack, so identical to what was done before, but it makes the code of [hash_encode] easier to understand by dropping all these slice manipulations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47380
fad504cfc94b rust: Use a maintained crate for SHA-1 hashing
Simon Sapin <simon.sapin@octobus.net>
parents: 45539
diff changeset
     1
use sha1::{Digest, Sha1};
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     2
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     3
#[derive(PartialEq, Debug)]
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     4
#[allow(non_camel_case_types)]
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 47380
diff changeset
     5
#[allow(clippy::upper_case_acronyms)]
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     6
enum path_state {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     7
    START, /* first byte of a path component */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     8
    A,     /* "AUX" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     9
    AU,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    10
    THIRD, /* third of a 3-byte sequence, e.g. "AUX", "NUL" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    11
    C,     /* "CON" or "COMn" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    12
    CO,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    13
    COMLPT, /* "COM" or "LPT" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    14
    COMLPTn,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    15
    L,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    16
    LP,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    17
    N,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    18
    NU,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    19
    P, /* "PRN" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    20
    PR,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    21
    LDOT, /* leading '.' */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    22
    DOT,  /* '.' in a non-leading position */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    23
    H,    /* ".h" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
    HGDI, /* ".hg", ".d", or ".i" */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    25
    SPACE,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    26
    DEFAULT, /* byte of a path component after the first */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    27
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    28
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    29
/* state machine for dir-encoding */
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    30
#[allow(non_camel_case_types)]
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 47380
diff changeset
    31
#[allow(clippy::upper_case_acronyms)]
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    32
enum dir_state {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    33
    DDOT,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    34
    DH,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    35
    DHGDI,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    36
    DDEFAULT,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    37
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    38
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    39
trait Sink {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    40
    fn write_byte(&mut self, c: u8);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    41
    fn write_bytes(&mut self, c: &[u8]);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    42
}
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    43
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    44
fn inset(bitset: &[u32; 8], c: u8) -> bool {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    45
    bitset[(c as usize) >> 5] & (1 << (c & 31)) != 0
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    46
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    47
50160
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    48
const MAXENCODE: usize = 4096 * 4;
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    49
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    50
struct DestArr {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    51
    buf: [u8; MAXENCODE],
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    52
    pub len: usize,
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    53
}
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    54
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    55
impl DestArr {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    56
    pub fn create() -> Self {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    57
        DestArr {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    58
            buf: [0; MAXENCODE],
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    59
            len: 0,
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    60
        }
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    61
    }
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    62
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    63
    pub fn contents(&self) -> &[u8] {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    64
        &self.buf[..self.len]
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    65
    }
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    66
}
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    67
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    68
impl Sink for DestArr {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    69
    fn write_byte(&mut self, c: u8) {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    70
        self.buf[self.len] = c;
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    71
        self.len += 1;
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    72
    }
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    73
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    74
    fn write_bytes(&mut self, src: &[u8]) {
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    75
        self.buf[self.len..self.len + src.len()].copy_from_slice(src);
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    76
        self.len += src.len();
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    77
    }
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    78
}
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
    79
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    80
struct Dest<'a> {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    81
    dest: Option<&'a mut [u8]>,
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    82
    pub len: usize,
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    83
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    84
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    85
impl<'a> Dest<'a> {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    86
    pub fn create(buf: &'a mut [u8]) -> Dest<'a> {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    87
        Dest {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    88
            dest: Some(buf),
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    89
            len: 0,
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    90
        }
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    91
    }
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    92
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    93
    pub fn create_measure() -> Dest<'a> {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    94
        Dest { dest: None, len: 0 }
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
    95
    }
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    96
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    97
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    98
fn rewrap_option<'a, 'b: 'a>(
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    99
    x: &'a mut Option<&'b mut [u8]>,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   100
) -> Option<&'a mut [u8]> {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   101
    match x {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   102
        None => None,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   103
        Some(y) => Some(y),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   104
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   105
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   106
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   107
impl<'a> Sink for Dest<'a> {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   108
    fn write_byte(&mut self, c: u8) {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   109
        if let Some(slice) = rewrap_option(&mut self.dest) {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   110
            slice[self.len] = c
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   111
        }
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   112
        self.len += 1
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   113
    }
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   114
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   115
    fn write_bytes(&mut self, src: &[u8]) {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   116
        if let Some(slice) = rewrap_option(&mut self.dest) {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   117
            slice[self.len..self.len + src.len()].copy_from_slice(src)
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   118
        }
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   119
        self.len += src.len();
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   120
    }
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   121
}
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   122
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   123
fn hexencode(dest: &mut impl Sink, c: u8) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   124
    let hexdigit = b"0123456789abcdef";
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   125
    dest.write_byte(hexdigit[(c as usize) >> 4]);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   126
    dest.write_byte(hexdigit[(c as usize) & 15]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   127
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   128
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   129
/* 3-byte escape: tilde followed by two hex digits */
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   130
fn escape3(dest: &mut impl Sink, c: u8) {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   131
    dest.write_byte(b'~');
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   132
    hexencode(dest, c);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   133
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   134
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   135
fn encode_dir(dest: &mut impl Sink, src: &[u8]) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   136
    let mut state = dir_state::DDEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   137
    let mut i = 0;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   138
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   139
    while i < src.len() {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   140
        match state {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   141
            dir_state::DDOT => match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   142
                b'd' | b'i' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   143
                    state = dir_state::DHGDI;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   144
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   145
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   146
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   147
                b'h' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   148
                    state = dir_state::DH;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   149
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   150
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   151
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   152
                _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   153
                    state = dir_state::DDEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   154
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   155
            },
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   156
            dir_state::DH => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   157
                if src[i] == b'g' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   158
                    state = dir_state::DHGDI;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   159
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   160
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   161
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   162
                    state = dir_state::DDEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   163
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   164
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   165
            dir_state::DHGDI => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   166
                if src[i] == b'/' {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   167
                    dest.write_bytes(b".hg");
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   168
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   169
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   170
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   171
                state = dir_state::DDEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   172
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   173
            dir_state::DDEFAULT => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   174
                if src[i] == b'.' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   175
                    state = dir_state::DDOT
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   176
                }
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   177
                dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   178
                i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   179
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   180
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   181
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   182
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   183
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   184
fn _encode(
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   185
    twobytes: &[u32; 8],
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   186
    onebyte: &[u32; 8],
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   187
    dest: &mut impl Sink,
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   188
    src: &[u8],
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   189
    encodedir: bool,
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   190
) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   191
    let mut state = path_state::START;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   192
    let mut i = 0;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   193
    let len = src.len();
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   194
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   195
    while i < len {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   196
        match state {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   197
            path_state::START => match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   198
                b'/' => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   199
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   200
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   201
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   202
                b'.' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   203
                    state = path_state::LDOT;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   204
                    escape3(dest, src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   205
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   206
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   207
                b' ' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   208
                    state = path_state::DEFAULT;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   209
                    escape3(dest, src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   210
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   211
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   212
                b'a' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   213
                    state = path_state::A;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   214
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   215
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   216
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   217
                b'c' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   218
                    state = path_state::C;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   219
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   220
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   221
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   222
                b'l' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   223
                    state = path_state::L;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   224
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   225
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   226
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   227
                b'n' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   228
                    state = path_state::N;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   229
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   230
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   231
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   232
                b'p' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   233
                    state = path_state::P;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   234
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   235
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   236
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   237
                _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   238
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   239
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   240
            },
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   241
            path_state::A => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   242
                if src[i] == b'u' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   243
                    state = path_state::AU;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   244
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   245
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   246
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   247
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   248
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   249
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   250
            path_state::AU => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   251
                if src[i] == b'x' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   252
                    state = path_state::THIRD;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   253
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   254
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   255
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   256
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   257
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   258
            path_state::THIRD => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   259
                state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   260
                match src[i] {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   261
                    b'.' | b'/' | b'\0' => escape3(dest, src[i - 1]),
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   262
                    _ => i -= 1,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   263
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   264
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   265
            path_state::C => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   266
                if src[i] == b'o' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   267
                    state = path_state::CO;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   268
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   269
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   270
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   271
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   272
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   273
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   274
            path_state::CO => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   275
                if src[i] == b'm' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   276
                    state = path_state::COMLPT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   277
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   278
                } else if src[i] == b'n' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   279
                    state = path_state::THIRD;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   280
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   281
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   282
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   283
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   284
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   285
            path_state::COMLPT => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   286
                if src[i] >= b'1' && src[i] <= b'9' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   287
                    state = path_state::COMLPTn;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   288
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   289
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   290
                    state = path_state::DEFAULT;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   291
                    dest.write_byte(src[i - 1]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   292
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   293
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   294
            path_state::COMLPTn => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   295
                state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   296
                match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   297
                    b'.' | b'/' | b'\0' => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   298
                        escape3(dest, src[i - 2]);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   299
                        dest.write_byte(src[i - 1]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   300
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   301
                    _ => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   302
                        dest.write_bytes(&src[i - 2..i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   303
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   304
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   305
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   306
            path_state::L => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   307
                if src[i] == b'p' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   308
                    state = path_state::LP;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   309
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   310
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   311
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   312
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   313
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   314
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   315
            path_state::LP => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   316
                if src[i] == b't' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   317
                    state = path_state::COMLPT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   318
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   319
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   320
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   321
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   322
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   323
            path_state::N => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   324
                if src[i] == b'u' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   325
                    state = path_state::NU;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   326
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   327
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   328
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   329
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   330
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   331
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   332
            path_state::NU => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   333
                if src[i] == b'l' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   334
                    state = path_state::THIRD;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   335
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   336
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   337
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   338
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   339
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   340
            path_state::P => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   341
                if src[i] == b'r' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   342
                    state = path_state::PR;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   343
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   344
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   345
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   346
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   347
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   348
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   349
            path_state::PR => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   350
                if src[i] == b'n' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   351
                    state = path_state::THIRD;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   352
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   353
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   354
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   355
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   356
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   357
            path_state::LDOT => match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   358
                b'd' | b'i' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   359
                    state = path_state::HGDI;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   360
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   361
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   362
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   363
                b'h' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   364
                    state = path_state::H;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   365
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   366
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   367
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   368
                _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   369
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   370
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   371
            },
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   372
            path_state::DOT => match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   373
                b'/' | b'\0' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   374
                    state = path_state::START;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   375
                    dest.write_bytes(b"~2e");
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   376
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   377
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   378
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   379
                b'd' | b'i' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   380
                    state = path_state::HGDI;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   381
                    dest.write_byte(b'.');
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   382
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   383
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   384
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   385
                b'h' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   386
                    state = path_state::H;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   387
                    dest.write_bytes(b".h");
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   388
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   389
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   390
                _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   391
                    state = path_state::DEFAULT;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   392
                    dest.write_byte(b'.');
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   393
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   394
            },
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   395
            path_state::H => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   396
                if src[i] == b'g' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   397
                    state = path_state::HGDI;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   398
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   399
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   400
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   401
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   402
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   403
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   404
            path_state::HGDI => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   405
                if src[i] == b'/' {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   406
                    state = path_state::START;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   407
                    if encodedir {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   408
                        dest.write_bytes(b".hg");
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   409
                    }
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   410
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   411
                    i += 1
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   412
                } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   413
                    state = path_state::DEFAULT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   414
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   415
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   416
            path_state::SPACE => match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   417
                b'/' | b'\0' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   418
                    state = path_state::START;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   419
                    dest.write_bytes(b"~20");
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   420
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   421
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   422
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   423
                _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   424
                    state = path_state::DEFAULT;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   425
                    dest.write_byte(b' ');
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   426
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   427
            },
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   428
            path_state::DEFAULT => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   429
                while i != len && inset(onebyte, src[i]) {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   430
                    dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   431
                    i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   432
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   433
                if i == len {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   434
                    break;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   435
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   436
                match src[i] {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   437
                    b'.' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   438
                        state = path_state::DOT;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   439
                        i += 1
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   440
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   441
                    b' ' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   442
                        state = path_state::SPACE;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   443
                        i += 1
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   444
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   445
                    b'/' => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   446
                        state = path_state::START;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   447
                        dest.write_byte(b'/');
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   448
                        i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   449
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   450
                    _ => {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   451
                        if inset(onebyte, src[i]) {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   452
                            loop {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   453
                                dest.write_byte(src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   454
                                i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   455
                                if !(i < len && inset(onebyte, src[i])) {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   456
                                    break;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   457
                                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   458
                            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   459
                        } else if inset(twobytes, src[i]) {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   460
                            let c = src[i];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   461
                            i += 1;
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   462
                            dest.write_byte(b'_');
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   463
                            dest.write_byte(if c == b'_' {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   464
                                b'_'
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   465
                            } else {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   466
                                c + 32
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   467
                            });
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   468
                        } else {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   469
                            escape3(dest, src[i]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   470
                            i += 1;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   471
                        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   472
                    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   473
                }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   474
            }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   475
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   476
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   477
    match state {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   478
        path_state::START => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   479
        path_state::A => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   480
        path_state::AU => (),
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   481
        path_state::THIRD => escape3(dest, src[i - 1]),
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   482
        path_state::C => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   483
        path_state::CO => (),
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   484
        path_state::COMLPT => dest.write_byte(src[i - 1]),
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   485
        path_state::COMLPTn => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   486
            escape3(dest, src[i - 2]);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   487
            dest.write_byte(src[i - 1]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   488
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   489
        path_state::L => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   490
        path_state::LP => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   491
        path_state::N => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   492
        path_state::NU => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   493
        path_state::P => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   494
        path_state::PR => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   495
        path_state::LDOT => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   496
        path_state::DOT => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   497
            dest.write_bytes(b"~2e");
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   498
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   499
        path_state::H => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   500
        path_state::HGDI => (),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   501
        path_state::SPACE => {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   502
            dest.write_bytes(b"~20");
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   503
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   504
        path_state::DEFAULT => (),
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   505
    }
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   506
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   507
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   508
fn basic_encode(dest: &mut impl Sink, src: &[u8]) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   509
    let twobytes: [u32; 8] = [0, 0, 0x87ff_fffe, 0, 0, 0, 0, 0];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   510
    let onebyte: [u32; 8] =
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   511
        [1, 0x2bff_3bfa, 0x6800_0001, 0x2fff_ffff, 0, 0, 0, 0];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   512
    _encode(&twobytes, &onebyte, dest, src, true)
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   513
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   514
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   515
const MAXSTOREPATHLEN: usize = 120;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   516
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   517
fn lower_encode(dest: &mut impl Sink, src: &[u8]) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   518
    let onebyte: [u32; 8] =
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   519
        [1, 0x2bff_fbfb, 0xe800_0001, 0x2fff_ffff, 0, 0, 0, 0];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   520
    let lower: [u32; 8] = [0, 0, 0x07ff_fffe, 0, 0, 0, 0, 0];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   521
    for c in src {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   522
        if inset(&onebyte, *c) {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   523
            dest.write_byte(*c)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   524
        } else if inset(&lower, *c) {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   525
            dest.write_byte(*c + 32)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   526
        } else {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   527
            escape3(dest, *c)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   528
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   529
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   530
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   531
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   532
fn aux_encode(dest: &mut impl Sink, src: &[u8]) {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   533
    let twobytes = [0; 8];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   534
    let onebyte: [u32; 8] = [!0, 0xffff_3ffe, !0, !0, !0, !0, !0, !0];
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   535
    _encode(&twobytes, &onebyte, dest, src, false)
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   536
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   537
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   538
fn hash_mangle(src: &[u8], sha: &[u8]) -> Vec<u8> {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   539
    let dirprefixlen = 8;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   540
    let maxshortdirslen = 68;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   541
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   542
    let last_slash = src.iter().rposition(|b| *b == b'/');
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   543
    let last_dot: Option<usize> = {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   544
        let s = last_slash.unwrap_or(0);
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 47380
diff changeset
   545
        src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   546
    };
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   547
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   548
    let mut dest_vec = vec![0; MAXSTOREPATHLEN];
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   549
    let mut dest = Dest::create(&mut dest_vec);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   550
    dest.write_bytes(b"dh/");
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   551
50096
06659dea51b0 rhg: fix a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50095
diff changeset
   552
    if let Some(last_slash) = last_slash {
06659dea51b0 rhg: fix a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50095
diff changeset
   553
        for slice in src[..last_slash].split(|b| *b == b'/') {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   554
            let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)];
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   555
            if dest.len + slice.len() > maxshortdirslen + 3 {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   556
                break;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   557
            } else {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   558
                dest.write_bytes(slice);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   559
            }
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   560
            dest.write_byte(b'/');
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   561
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   562
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   563
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   564
    let used = dest.len + 40 + {
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   565
        if let Some(l) = last_dot {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   566
            src.len() - l
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   567
        } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   568
            0
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   569
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   570
    };
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   571
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   572
    if MAXSTOREPATHLEN > used {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   573
        let slop = MAXSTOREPATHLEN - used;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   574
        let basenamelen = match last_slash {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   575
            Some(l) => src.len() - l - 1,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   576
            None => src.len(),
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   577
        };
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   578
        let basenamelen = std::cmp::min(basenamelen, slop);
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   579
        if basenamelen > 0 {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   580
            let start = match last_slash {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   581
                Some(l) => l + 1,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   582
                None => 0,
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   583
            };
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   584
            dest.write_bytes(&src[start..][..basenamelen])
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   585
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   586
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   587
    for c in sha {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   588
        hexencode(&mut dest, *c);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   589
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   590
    if let Some(l) = last_dot {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   591
        dest.write_bytes(&src[l..]);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   592
    }
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   593
    let destlen = dest.len;
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   594
    if destlen == dest_vec.len() {
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   595
        dest_vec
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   596
    } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   597
        // sometimes the path are shorter than MAXSTOREPATHLEN
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   598
        dest_vec[..destlen].to_vec()
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   599
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   600
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   601
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   602
fn hash_encode(src: &[u8]) -> Vec<u8> {
50160
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   603
    let mut dired = DestArr::create();
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   604
    let mut lowered = DestArr::create();
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   605
    let mut auxed = DestArr::create();
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   606
    let baselen = (src.len() - 5) * 3;
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   607
    if baselen >= MAXENCODE {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   608
        panic!("path_encode::hash_encore: string too long: {}", baselen)
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   609
    };
50160
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   610
    encode_dir(&mut dired, src);
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   611
    let sha = Sha1::digest(dired.contents());
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   612
    lower_encode(&mut lowered, &dired.contents()[5..]);
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   613
    aux_encode(&mut auxed, lowered.contents());
5ce53ff6133a rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50159
diff changeset
   614
    hash_mangle(auxed.contents(), &sha)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   615
}
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   616
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   617
pub fn path_encode(path: &[u8]) -> Vec<u8> {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   618
    let newlen = if path.len() <= MAXSTOREPATHLEN {
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   619
        let mut measure = Dest::create_measure();
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   620
        basic_encode(&mut measure, path);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   621
        measure.len
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   622
    } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   623
        MAXSTOREPATHLEN + 1
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   624
    };
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   625
    if newlen <= MAXSTOREPATHLEN {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   626
        if newlen == path.len() {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   627
            path.to_vec()
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   628
        } else {
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   629
            let mut res = vec![0; newlen];
50159
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   630
            let mut dest = Dest::create(&mut res);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   631
            basic_encode(&mut dest, path);
96d31efd21f7 rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50158
diff changeset
   632
            assert!(dest.len == newlen);
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   633
            res
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   634
        }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   635
    } else {
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 47380
diff changeset
   636
        hash_encode(path)
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   637
    }
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   638
}
50095
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   639
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   640
#[cfg(test)]
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   641
mod tests {
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   642
    use super::*;
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   643
    use crate::utils::hg_path::HgPathBuf;
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   644
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   645
    #[test]
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   646
    fn test_long_filename_at_root() {
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   647
        let input = b"data/ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ.i";
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   648
        let expected = b"dh/abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij.i708243a2237a7afae259ea3545a72a2ef11c247b.i";
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   649
        let res = path_encode(input);
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   650
        assert_eq!(
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   651
            HgPathBuf::from_bytes(&res),
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   652
            HgPathBuf::from_bytes(expected)
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   653
        );
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   654
    }
362fe34702d5 rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49930
diff changeset
   655
}