-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlib.rs
More file actions
89 lines (80 loc) · 2.25 KB
/
lib.rs
File metadata and controls
89 lines (80 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
pub mod chan;
mod cow;
pub mod decrypt;
pub mod lock;
mod mem;
//pub mod queue;
pub mod rand;
pub mod utf8;
mod waker;
pub mod vec;
pub use vec::*;
pub use cow::*;
pub use mem::*;
mod switcher;
pub use switcher::Switcher;
pub use utf8::*;
pub use waker::AtomicWaker;
pub mod time;
mod io;
pub use io::*;
mod asserts;
mod bits;
pub use bits::*;
pub trait BufWriter {
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()>;
#[inline]
fn write_all_hint(&mut self, buf: &[u8], _next: bool) -> std::io::Result<()> {
self.write_all(buf)
}
#[inline]
fn write_seg_all(&mut self, buf0: &[u8], buf1: &[u8]) -> std::io::Result<()> {
self.write_all(buf0)?;
self.write_all(buf1)
}
}
pub trait NumStr {
fn with_str<O>(&self, f: impl FnMut(&[u8]) -> O) -> O;
}
impl NumStr for usize {
#[inline]
fn with_str<O>(&self, mut f: impl FnMut(&[u8]) -> O) -> O {
match *self {
0..=9 => f(&[b'0' + *self as u8]),
10..=99 => {
let mut buf = [0u8; 2];
buf[0] = b'0' + (*self / 10) as u8;
buf[1] = b'0' + (*self % 10) as u8;
f(&buf)
}
100..=999 => {
let mut buf = [0u8; 3];
buf[0] = b'0' + (*self / 100) as u8;
buf[1] = b'0' + (*self / 10 % 10) as u8;
buf[2] = b'0' + (*self % 10) as u8;
f(&buf)
}
1000..=9999 => {
let mut buf = [0u8; 4];
buf[0] = b'0' + (*self / 1000) as u8;
buf[1] = b'0' + (*self / 100 % 10) as u8;
buf[2] = b'0' + (*self / 10 % 10) as u8;
buf[3] = b'0' + (*self % 10) as u8;
f(&buf)
}
_ => {
let mut buf = [0u8; 32];
let mut left = *self;
let mut idx = buf.len() - 1;
while left > 0 {
buf[idx] = b'0' + (left % 10) as u8;
left /= 10;
idx -= 1;
}
// 因为进入到这个分支,self一定是大于等于10000的
debug_assert!(idx <= buf.len() - 1);
f(&buf[idx + 1..])
}
}
}
}