ext_std/
lib.rs

1#![allow(non_upper_case_globals)]
2
3use std::*;
4use error::Error;
5use io;
6use std::str::FromStr;
7
8pub type Er = anyhow::Error;
9pub type Res<T> = anyhow::Result<T>;
10pub type Maybe = Res<()>;
11pub const ok: Maybe = Ok(());
12
13pub fn cin<T>() -> Res<T> 
14where T: FromStr, T::Err: Error + Send + Sync + 'static {
15    let mut input = String::new();
16    io::stdin().read_line(&mut input)?;
17    Ok(input.trim().parse()?)
18}
19
20pub fn print<T: fmt::Display>(output: T) {
21    println!("{output}");
22}
23
24pub fn debug<T: fmt::Debug>(output: T) {
25    println!("{output:?}");
26}
27
28pub trait Utf8Container {
29    fn slice(&self, start: usize, end: usize) -> &str;
30    fn from(&self, start: usize) -> &str;
31    fn to(&self, end: usize) -> &str;
32}
33
34impl Utf8Container for str {
35    fn slice(&self, start: usize, end: usize) -> &str {
36        let start = self.char_indices().nth(start);
37        let end  = self.char_indices().nth(end);
38        if let (Some(start), Some(end)) = (start, end) {
39            &self[start.0..end.0]
40        }
41        else if let Some(start) = start {
42            &self[start.0..]
43        }
44        else {
45            ""
46        }
47    }
48
49    fn from(&self, start: usize) -> &str {
50        let start = self.char_indices().nth(start);
51        if let Some(start) = start {
52            &self[start.0..]
53        }
54        else {
55            ""
56        }
57    }
58
59    fn to(&self, end: usize) -> &str {
60        let end  = self.char_indices().nth(end);
61        if let Some(end) = end {
62            &self[..end.0]
63        }
64        else {
65            &self
66        }
67    }
68}
69
70impl Utf8Container for String {
71    fn slice(&self, start: usize, end: usize) -> &str {
72        let start = self.char_indices().nth(start);
73        let end  = self.char_indices().nth(end);
74        if let (Some(start), Some(end)) = (start, end) {
75            &self[start.0..end.0]
76        }
77        else if let Some(start) = start {
78            &self[start.0..]
79        }
80        else {
81            ""
82        }
83    }
84
85    fn from(&self, start: usize) -> &str {
86        let start = self.char_indices().nth(start);
87        if let Some(start) = start {
88            &self[start.0..]
89        }
90        else {
91            ""
92        }
93    }
94
95    fn to(&self, end: usize) -> &str {
96        let end  = self.char_indices().nth(end);
97        if let Some(end) = end {
98            &self[..end.0]
99        }
100        else {
101            &self
102        }
103    }
104}