Mercurial > hg
annotate rust/hg-core/src/revlog/path_encode.rs @ 50163:11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Two separate types make the write semantics easier to understand
because we can consider the two sinks separately.
Having two independent compiled functions for size measurement
and for actual encoding seems likely to improve performance, too.
(and maybe we should get rid of measurement altogether)
Getting rid of [Dest] also removes the ugly option rewrapping
code, which is good.
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Thu, 16 Feb 2023 19:00:56 +0000 |
parents | 6baea276a985 |
children | 8e50aa0db347 |
rev | line source |
---|---|
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 |
50161
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
50 struct DestArr<const N: usize> { |
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
51 buf: [u8; N], |
50160
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 |
50161
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
55 impl<const N: usize> DestArr<N> { |
50160
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 { |
50161
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
58 buf: [0; N], |
50160
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 |
50161
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
68 impl<const N: usize> Sink for DestArr<N> { |
50160
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 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
80 struct VecDest { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
81 buf: Vec<u8>, |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
82 } |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
83 |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
84 struct MeasureDest { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
85 pub len: usize, |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
86 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
87 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
88 impl VecDest { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
89 pub fn create(capacity : usize) -> Self { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
90 Self { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
91 buf: Vec::with_capacity(capacity), |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
92 } |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
93 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
94 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
95 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
96 impl Sink for VecDest { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
97 fn write_byte(&mut self, c: u8) { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
98 self.buf.push(c) |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
99 } |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
100 |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
101 fn write_bytes(&mut self, src: &[u8]) { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
102 self.buf.extend_from_slice(src) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
103 } |
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 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
106 impl MeasureDest { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
107 fn create() -> Self { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
108 Self { len: 0 } |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
109 } |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
110 } |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
111 |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
112 impl Sink for MeasureDest { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
113 fn write_byte(&mut self, _c: u8) { |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
114 self.len += 1; |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
115 } |
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
116 |
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
117 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
|
118 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
|
119 } |
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 fn hexencode(dest: &mut impl Sink, c: u8) { |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
123 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
|
124 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
|
125 dest.write_byte(hexdigit[(c as usize) & 15]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
126 } |
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 /* 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
|
129 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
|
130 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
|
131 hexencode(dest, c); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
132 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
133 |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
134 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
|
135 let mut state = dir_state::DDEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
136 let mut i = 0; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
137 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
138 while i < src.len() { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
139 match state { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
140 dir_state::DDOT => match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
141 b'd' | b'i' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
142 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
|
143 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
144 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
145 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
146 b'h' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
147 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
|
148 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
149 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
150 } |
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 state = dir_state::DDEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
153 } |
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 dir_state::DH => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
156 if src[i] == b'g' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
157 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
|
158 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
159 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
160 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
161 state = dir_state::DDEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
162 } |
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 dir_state::DHGDI => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
165 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
|
166 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
|
167 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
168 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
169 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
170 state = dir_state::DDEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
171 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
172 dir_state::DDEFAULT => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
173 if src[i] == b'.' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
174 state = dir_state::DDOT |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
175 } |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
176 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
177 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
178 } |
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 fn _encode( |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
184 twobytes: &[u32; 8], |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
185 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
|
186 dest: &mut impl Sink, |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
187 src: &[u8], |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
188 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
|
189 ) { |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
190 let mut state = path_state::START; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
191 let mut i = 0; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
192 let len = src.len(); |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
193 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
194 while i < len { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
195 match state { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
196 path_state::START => match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
197 b'/' => { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
198 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
199 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
200 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
201 b'.' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
202 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
|
203 escape3(dest, src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
204 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
205 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
206 b' ' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
207 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
|
208 escape3(dest, src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
209 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
210 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
211 b'a' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
212 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
|
213 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
214 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
215 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
216 b'c' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
217 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
|
218 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
219 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
220 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
221 b'l' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
222 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
|
223 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
224 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
225 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
226 b'n' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
227 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
|
228 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
229 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
230 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
231 b'p' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
232 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
|
233 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
234 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
235 } |
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 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
238 } |
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 path_state::A => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
241 if src[i] == b'u' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
242 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
|
243 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
244 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
245 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
246 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
247 } |
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 path_state::AU => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
250 if src[i] == b'x' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
251 state = path_state::THIRD; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
252 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
253 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
254 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
255 } |
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 path_state::THIRD => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
258 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
259 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
|
260 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
|
261 _ => i -= 1, |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
262 } |
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 path_state::C => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
265 if src[i] == b'o' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
266 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
|
267 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
268 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
269 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
270 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
271 } |
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 path_state::CO => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
274 if src[i] == b'm' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
275 state = path_state::COMLPT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
276 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
277 } else if src[i] == b'n' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
278 state = path_state::THIRD; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
279 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
280 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
281 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
282 } |
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 path_state::COMLPT => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
285 if src[i] >= b'1' && src[i] <= b'9' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
286 state = path_state::COMLPTn; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
287 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
288 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
289 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
|
290 dest.write_byte(src[i - 1]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
291 } |
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 path_state::COMLPTn => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
294 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
295 match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
296 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
|
297 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
|
298 dest.write_byte(src[i - 1]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
299 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
300 _ => { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
301 dest.write_bytes(&src[i - 2..i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
302 } |
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 path_state::L => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
306 if src[i] == b'p' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
307 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
|
308 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
309 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
310 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
311 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
312 } |
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 path_state::LP => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
315 if src[i] == b't' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
316 state = path_state::COMLPT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
317 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
318 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
319 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
320 } |
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 path_state::N => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
323 if src[i] == b'u' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
324 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
|
325 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
326 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
327 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
328 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
329 } |
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 path_state::NU => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
332 if src[i] == b'l' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
333 state = path_state::THIRD; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
334 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
335 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
336 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
337 } |
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 path_state::P => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
340 if src[i] == b'r' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
341 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
|
342 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
343 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
344 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
345 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
346 } |
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 path_state::PR => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
349 if src[i] == b'n' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
350 state = path_state::THIRD; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
351 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
352 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
353 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
354 } |
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 path_state::LDOT => match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
357 b'd' | b'i' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
358 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
|
359 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
360 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
361 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
362 b'h' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
363 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
|
364 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
365 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
366 } |
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 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
369 } |
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 path_state::DOT => match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
372 b'/' | b'\0' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
373 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
|
374 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
|
375 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
376 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
377 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
378 b'd' | b'i' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
379 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
|
380 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
|
381 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
382 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
383 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
384 b'h' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
385 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
|
386 dest.write_bytes(b".h"); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
387 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
388 } |
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 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
|
391 dest.write_byte(b'.'); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
392 } |
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 path_state::H => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
395 if src[i] == b'g' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
396 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
|
397 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
398 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
399 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
400 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
401 } |
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 path_state::HGDI => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
404 if src[i] == b'/' { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
405 state = path_state::START; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
406 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
|
407 dest.write_bytes(b".hg"); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
408 } |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
409 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
410 i += 1 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
411 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
412 state = path_state::DEFAULT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
413 } |
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 path_state::SPACE => match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
416 b'/' | b'\0' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
417 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
|
418 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
|
419 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
420 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
421 } |
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 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
|
424 dest.write_byte(b' '); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
425 } |
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 path_state::DEFAULT => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
428 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
|
429 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
430 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
431 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
432 if i == len { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
433 break; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
434 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
435 match src[i] { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
436 b'.' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
437 state = path_state::DOT; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
438 i += 1 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
439 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
440 b' ' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
441 state = path_state::SPACE; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
442 i += 1 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
443 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
444 b'/' => { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
445 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
|
446 dest.write_byte(b'/'); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
447 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
448 } |
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 if inset(onebyte, src[i]) { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
451 loop { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
452 dest.write_byte(src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
453 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
454 if !(i < len && inset(onebyte, src[i])) { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
455 break; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
456 } |
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 } else if inset(twobytes, src[i]) { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
459 let c = src[i]; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
460 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
|
461 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
|
462 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
|
463 b'_' |
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
464 } else { |
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
465 c + 32 |
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
466 }); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
467 } else { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
468 escape3(dest, src[i]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
469 i += 1; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
470 } |
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 match state { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
477 path_state::START => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
478 path_state::A => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
479 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
|
480 path_state::THIRD => escape3(dest, src[i - 1]), |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
481 path_state::C => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
482 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
|
483 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
|
484 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
|
485 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
|
486 dest.write_byte(src[i - 1]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
487 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
488 path_state::L => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
489 path_state::LP => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
490 path_state::N => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
491 path_state::NU => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
492 path_state::P => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
493 path_state::PR => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
494 path_state::LDOT => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
495 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
|
496 dest.write_bytes(b"~2e"); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
497 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
498 path_state::H => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
499 path_state::HGDI => (), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
500 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
|
501 dest.write_bytes(b"~20"); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
502 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
503 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
|
504 } |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
505 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
506 |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
507 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
|
508 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
|
509 let onebyte: [u32; 8] = |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
510 [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
|
511 _encode(&twobytes, &onebyte, dest, src, true) |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
512 } |
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 const MAXSTOREPATHLEN: usize = 120; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
515 |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
516 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
|
517 let onebyte: [u32; 8] = |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
518 [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
|
519 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
|
520 for c in src { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
521 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
|
522 dest.write_byte(*c) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
523 } 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
|
524 dest.write_byte(*c + 32) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
525 } else { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
526 escape3(dest, *c) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
527 } |
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 |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
531 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
|
532 let twobytes = [0; 8]; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
533 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
|
534 _encode(&twobytes, &onebyte, dest, src, false) |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
535 } |
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 fn hash_mangle(src: &[u8], sha: &[u8]) -> Vec<u8> { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
538 let dirprefixlen = 8; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
539 let maxshortdirslen = 68; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
540 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
541 let last_slash = src.iter().rposition(|b| *b == b'/'); |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
542 let last_dot: Option<usize> = { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
543 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
|
544 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
|
545 }; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
546 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
547 let mut dest : VecDest = VecDest::create(MAXSTOREPATHLEN); |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
548 dest.write_bytes(b"dh/"); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
549 |
50096
06659dea51b0
rhg: fix a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50095
diff
changeset
|
550 if let Some(last_slash) = last_slash { |
06659dea51b0
rhg: fix a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50095
diff
changeset
|
551 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
|
552 let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)]; |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
553 if dest.buf.len() + slice.len() > maxshortdirslen + 3 { |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
554 break; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
555 } else { |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
556 dest.write_bytes(slice); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
557 } |
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_byte(b'/'); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
559 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
560 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
561 |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
562 let used = dest.buf.len() + 40 + { |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
563 if let Some(l) = last_dot { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
564 src.len() - l |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
565 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
566 0 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
567 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
568 }; |
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 if MAXSTOREPATHLEN > used { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
571 let slop = MAXSTOREPATHLEN - used; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
572 let basenamelen = match last_slash { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
573 Some(l) => src.len() - l - 1, |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
574 None => src.len(), |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
575 }; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
576 let basenamelen = std::cmp::min(basenamelen, slop); |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
577 if basenamelen > 0 { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
578 let start = match last_slash { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
579 Some(l) => l + 1, |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
580 None => 0, |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
581 }; |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
582 dest.write_bytes(&src[start..][..basenamelen]) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
583 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
584 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
585 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
|
586 hexencode(&mut dest, *c); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
587 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
588 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
|
589 dest.write_bytes(&src[l..]); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
590 } |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
591 dest.buf |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
592 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
593 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
594 fn hash_encode(src: &[u8]) -> Vec<u8> { |
50161
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
595 let mut dired: DestArr<MAXENCODE> = DestArr::create(); |
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
596 let mut lowered: DestArr<MAXENCODE> = DestArr::create(); |
5d7ba99da3b8
rhg: in path_encode, make DestArr generic over its size
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50160
diff
changeset
|
597 let mut auxed: DestArr<MAXENCODE> = DestArr::create(); |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
598 let baselen = (src.len() - 5) * 3; |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
599 if baselen >= MAXENCODE { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
600 panic!("path_encode::hash_encore: string too long: {}", baselen) |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
601 }; |
50160
5ce53ff6133a
rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50159
diff
changeset
|
602 encode_dir(&mut dired, src); |
5ce53ff6133a
rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50159
diff
changeset
|
603 let sha = Sha1::digest(dired.contents()); |
5ce53ff6133a
rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50159
diff
changeset
|
604 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
|
605 aux_encode(&mut auxed, lowered.contents()); |
5ce53ff6133a
rhg: in path_encode add a DestArr type
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50159
diff
changeset
|
606 hash_mangle(auxed.contents(), &sha) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
607 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
608 |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
609 pub fn path_encode(path: &[u8]) -> Vec<u8> { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
610 let newlen = if path.len() <= MAXSTOREPATHLEN { |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
611 let mut measure = MeasureDest::create(); |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
612 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
|
613 measure.len |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
614 } else { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
615 MAXSTOREPATHLEN + 1 |
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 if newlen <= MAXSTOREPATHLEN { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
618 if newlen == path.len() { |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
619 path.to_vec() |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
620 } else { |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
621 let mut dest = VecDest::create(newlen); |
50159
96d31efd21f7
rhg: reduce verbosity in path_encode by using a trait for writing
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50158
diff
changeset
|
622 basic_encode(&mut dest, path); |
50163
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
623 assert!(dest.buf.len() == newlen); |
11661326b410
rhg: in path_encode, split Dest into VecDest and MeasureDest
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
50162
diff
changeset
|
624 dest.buf |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
625 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
626 } else { |
49930
e98fd81bb151
rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents:
47380
diff
changeset
|
627 hash_encode(path) |
45539
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
628 } |
aebc976fd7d5
hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
629 } |
50095
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
630 |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
631 #[cfg(test)] |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
632 mod tests { |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
633 use super::*; |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
634 use crate::utils::hg_path::HgPathBuf; |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
635 |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
636 #[test] |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
637 fn test_long_filename_at_root() { |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
638 let input = b"data/ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ.i"; |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
639 let expected = b"dh/abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij.i708243a2237a7afae259ea3545a72a2ef11c247b.i"; |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
640 let res = path_encode(input); |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
641 assert_eq!( |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
642 HgPathBuf::from_bytes(&res), |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
643 HgPathBuf::from_bytes(expected) |
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 } |
362fe34702d5
rhg: demonstrate a bug in path_encode
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
49930
diff
changeset
|
646 } |