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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! provides utilities for parser, compiler, and vm crate.
use std::fmt;
use std::path::PathBuf;

pub mod cache;
pub mod config;
pub mod consts;
pub mod datetime;
pub mod dict;
pub mod env;
pub mod erg_util;
pub mod error;
pub mod fresh;
pub mod fxhash;
pub mod help_messages;
pub mod io;
pub mod lang;
pub mod levenshtein;
pub mod macros;
pub mod opcode;
pub mod opcode308;
pub mod opcode309;
pub mod opcode310;
pub mod opcode311;
pub mod pathutil;
pub mod python_util;
pub mod random;
pub mod serialize;
pub mod set;
pub mod shared;
pub mod spawn;
pub mod stdin;
pub mod str;
pub mod style;
pub mod traits;
pub mod triple;
pub mod tsort;
pub mod vfs;

use consts::CASE_SENSITIVE;

use crate::set::Set;
pub use crate::str::Str;
pub use crate::triple::Triple;

pub type ArcArray<T> = std::sync::Arc<[T]>;

pub fn open_read(filename: &str) -> std::io::Result<String> {
    let f = std::fs::File::open(filename)?;
    read_file(f)
}

pub fn read_file(mut f: std::fs::File) -> std::io::Result<String> {
    let mut s = "".to_string();
    std::io::Read::read_to_string(&mut f, &mut s)?;
    Ok(s)
}

pub fn fmt_vec<T: fmt::Display>(v: &[T]) -> String {
    fmt_iter(v.iter())
}

pub fn fmt_slice<T: fmt::Display>(v: &[T]) -> String {
    fmt_iter(v.iter())
}

pub fn fmt_vec_split_with<T: fmt::Display>(v: &[T], splitter: &str) -> String {
    fmt_iter_split_with(v.iter(), splitter)
}

pub fn fmt_set_split_with<T: fmt::Display + std::hash::Hash>(s: &Set<T>, splitter: &str) -> String {
    fmt_iter_split_with(s.iter(), splitter)
}

pub fn debug_fmt_iter<T: fmt::Debug, I: Iterator<Item = T>>(iter: I) -> String {
    let mut s = iter.fold("".to_string(), |sum, elem| format!("{sum}{elem:?}, "));
    s.pop();
    s.pop();
    s
}

pub fn fmt_iter<T: fmt::Display, I: Iterator<Item = T>>(iter: I) -> String {
    let mut s = iter.fold("".to_string(), |sum, elem| sum + &elem.to_string() + ", ");
    s.pop();
    s.pop();
    s
}

pub fn fmt_iter_split_with<T: fmt::Display, I: Iterator<Item = T>>(i: I, splitter: &str) -> String {
    let mut s = i.fold("".to_string(), |sum, elem| {
        sum + &elem.to_string() + splitter
    });
    for _ in 0..splitter.len() {
        s.pop();
    }
    s
}

pub fn fmt_indent(s: String, depth: usize) -> String {
    let indent = " ".repeat(depth);
    s.split('\n').map(|s| indent.clone() + s).collect()
}

/// If you want to get a hash consisting of multiple objects, pass it as a tuple or array
pub fn get_hash<T: std::hash::Hash>(t: &T) -> usize {
    let mut s = fxhash::FxHasher::default();
    t.hash(&mut s);
    let res = std::hash::Hasher::finish(&s);
    if cfg!(target_pointer_width = "64") {
        res as usize
    } else {
        (res % usize::MAX as u64) as usize
    }
}

/// \r\n (Windows), \r (old MacOS) -> \n (Unix)
#[inline]
pub fn normalize_newline(src: &str) -> String {
    src.replace("\r\n", "\n").replace('\r', "\n")
}

/// cut \n
#[inline]
pub fn chomp(src: &str) -> String {
    normalize_newline(src).replace('\n', "")
}

pub fn try_map<T, U, E, F, I>(i: I, f: F) -> Result<Vec<U>, E>
where
    F: Fn(T) -> Result<U, E>,
    I: Iterator<Item = T>,
{
    let mut v = vec![];
    for x in i {
        let y = f(x)?;
        v.push(y);
    }
    Ok(v)
}

pub fn try_map_mut<T, U, E, F, I>(i: I, mut f: F) -> Result<Vec<U>, E>
where
    F: FnMut(T) -> Result<U, E>,
    I: Iterator<Item = T>,
{
    let mut v = vec![];
    for x in i {
        let y = f(x)?;
        v.push(y);
    }
    Ok(v)
}

pub fn failable_map_mut<T, U, E, F, I>(i: I, mut f: F) -> Result<Vec<U>, (Vec<U>, Vec<E>)>
where
    F: FnMut(T) -> Result<U, (U, E)>,
    I: Iterator<Item = T>,
{
    let mut v = vec![];
    let mut errs = vec![];
    for x in i {
        match f(x) {
            Ok(y) => {
                v.push(y);
            }
            Err((y, e)) => {
                v.push(y);
                errs.push(e);
            }
        }
    }
    if errs.is_empty() {
        Ok(v)
    } else {
        Err((v, errs))
    }
}

pub fn unique_in_place<T: Eq + std::hash::Hash + Clone>(v: &mut Vec<T>) {
    let mut uniques = Set::new();
    v.retain(|e| uniques.insert(e.clone()));
}

/// at least, this is necessary for Windows and macOS
pub fn normalize_path(path: PathBuf) -> PathBuf {
    let verbatim_replaced = path.to_string_lossy().replace("\\\\?\\", "");
    let lower = if !CASE_SENSITIVE {
        verbatim_replaced.to_lowercase()
    } else {
        verbatim_replaced
    };
    PathBuf::from(lower)
}

/// ```
/// use erg_common::trim_eliminate_top_indent;
/// let code = r#"
///     def foo():
///         pass
/// "#;
/// let expected = r#"def foo():
///     pass"#;
/// assert_eq!(trim_eliminate_top_indent(code.to_string()), expected);
/// ```
pub fn trim_eliminate_top_indent(code: String) -> String {
    let code = code.trim_matches(|c| c == '\n' || c == '\r');
    if !code.starts_with(' ') {
        return code.to_string();
    }
    let indent = code.chars().take_while(|c| *c == ' ').count();
    let mut result = String::new();
    for line in code.lines() {
        if line.len() > indent {
            result.push_str(line[indent..].trim_end());
        } else {
            result.push_str(line.trim_end());
        }
        result.push('\n');
    }
    if !result.is_empty() {
        result.pop();
    }
    result
}

pub fn deepen_indent(code: String) -> String {
    let mut result = String::new();
    for line in code.lines() {
        result.push_str("    ");
        result.push_str(line);
        result.push('\n');
    }
    if !result.is_empty() {
        result.pop();
    }
    result
}