##// END OF EJS Templates
rhg: in path_encode, split Dest into VecDest and MeasureDest...
Arseniy Alekseyev -
r51058:11661326 default
parent child Browse files
Show More
@@ -77,45 +77,44 b' impl<const N: usize> Sink for DestArr<N>'
77 }
77 }
78 }
78 }
79
79
80 struct Dest<'a> {
80 struct VecDest {
81 dest: Option<&'a mut [u8]>,
81 buf: Vec<u8>,
82 }
83
84 struct MeasureDest {
82 pub len: usize,
85 pub len: usize,
83 }
86 }
84
87
85 impl<'a> Dest<'a> {
88 impl VecDest {
86 pub fn create(buf: &'a mut [u8]) -> Dest<'a> {
89 pub fn create(capacity : usize) -> Self {
87 Dest {
90 Self {
88 dest: Some(buf),
91 buf: Vec::with_capacity(capacity),
89 len: 0,
90 }
92 }
91 }
93 }
92
93 pub fn create_measure() -> Dest<'a> {
94 Dest { dest: None, len: 0 }
95 }
96 }
94 }
97
95
98 fn rewrap_option<'a, 'b: 'a>(
96 impl Sink for VecDest {
99 x: &'a mut Option<&'b mut [u8]>,
97 fn write_byte(&mut self, c: u8) {
100 ) -> Option<&'a mut [u8]> {
98 self.buf.push(c)
101 match x {
99 }
102 None => None,
100
103 Some(y) => Some(y),
101 fn write_bytes(&mut self, src: &[u8]) {
102 self.buf.extend_from_slice(src)
104 }
103 }
105 }
104 }
106
105
107 impl<'a> Sink for Dest<'a> {
106 impl MeasureDest {
108 fn write_byte(&mut self, c: u8) {
107 fn create() -> Self {
109 if let Some(slice) = rewrap_option(&mut self.dest) {
108 Self { len: 0 }
110 slice[self.len] = c
109 }
111 }
110 }
112 self.len += 1
111
112 impl Sink for MeasureDest {
113 fn write_byte(&mut self, _c: u8) {
114 self.len += 1;
113 }
115 }
114
116
115 fn write_bytes(&mut self, src: &[u8]) {
117 fn write_bytes(&mut self, src: &[u8]) {
116 if let Some(slice) = rewrap_option(&mut self.dest) {
117 slice[self.len..self.len + src.len()].copy_from_slice(src)
118 }
119 self.len += src.len();
118 self.len += src.len();
120 }
119 }
121 }
120 }
@@ -545,13 +544,13 b' fn hash_mangle(src: &[u8], sha: &[u8]) -'
545 src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s)
544 src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s)
546 };
545 };
547
546
548 let mut dest : DestArr<MAXSTOREPATHLEN> = DestArr::create();
547 let mut dest : VecDest = VecDest::create(MAXSTOREPATHLEN);
549 dest.write_bytes(b"dh/");
548 dest.write_bytes(b"dh/");
550
549
551 if let Some(last_slash) = last_slash {
550 if let Some(last_slash) = last_slash {
552 for slice in src[..last_slash].split(|b| *b == b'/') {
551 for slice in src[..last_slash].split(|b| *b == b'/') {
553 let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)];
552 let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)];
554 if dest.len + slice.len() > maxshortdirslen + 3 {
553 if dest.buf.len() + slice.len() > maxshortdirslen + 3 {
555 break;
554 break;
556 } else {
555 } else {
557 dest.write_bytes(slice);
556 dest.write_bytes(slice);
@@ -560,7 +559,7 b' fn hash_mangle(src: &[u8], sha: &[u8]) -'
560 }
559 }
561 }
560 }
562
561
563 let used = dest.len + 40 + {
562 let used = dest.buf.len() + 40 + {
564 if let Some(l) = last_dot {
563 if let Some(l) = last_dot {
565 src.len() - l
564 src.len() - l
566 } else {
565 } else {
@@ -589,7 +588,7 b' fn hash_mangle(src: &[u8], sha: &[u8]) -'
589 if let Some(l) = last_dot {
588 if let Some(l) = last_dot {
590 dest.write_bytes(&src[l..]);
589 dest.write_bytes(&src[l..]);
591 }
590 }
592 dest.contents().to_vec()
591 dest.buf
593 }
592 }
594
593
595 fn hash_encode(src: &[u8]) -> Vec<u8> {
594 fn hash_encode(src: &[u8]) -> Vec<u8> {
@@ -609,7 +608,7 b' fn hash_encode(src: &[u8]) -> Vec<u8> {'
609
608
610 pub fn path_encode(path: &[u8]) -> Vec<u8> {
609 pub fn path_encode(path: &[u8]) -> Vec<u8> {
611 let newlen = if path.len() <= MAXSTOREPATHLEN {
610 let newlen = if path.len() <= MAXSTOREPATHLEN {
612 let mut measure = Dest::create_measure();
611 let mut measure = MeasureDest::create();
613 basic_encode(&mut measure, path);
612 basic_encode(&mut measure, path);
614 measure.len
613 measure.len
615 } else {
614 } else {
@@ -619,11 +618,10 b' pub fn path_encode(path: &[u8]) -> Vec<u'
619 if newlen == path.len() {
618 if newlen == path.len() {
620 path.to_vec()
619 path.to_vec()
621 } else {
620 } else {
622 let mut res = vec![0; newlen];
621 let mut dest = VecDest::create(newlen);
623 let mut dest = Dest::create(&mut res);
624 basic_encode(&mut dest, path);
622 basic_encode(&mut dest, path);
625 assert!(dest.len == newlen);
623 assert!(dest.buf.len() == newlen);
626 res
624 dest.buf
627 }
625 }
628 } else {
626 } else {
629 hash_encode(path)
627 hash_encode(path)
General Comments 0
You need to be logged in to leave comments. Login now