summaryrefslogtreecommitdiff
path: root/support/pkgcheck/src/recode.rs
blob: 9a6b3d426d2ccb0ffb6ac7515b0c857b355ef111 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// the code here is taken (and slightly modified) from https://github.com/XadillaX/crlf2lf

//
// convert CRLF (0D 0A) to LF ( 0A )
//

use std::fs::File;
use std::io::prelude::*;
use std::io::{self, Read};
use std::path::Path;

struct Bytes {
    bytes: Vec<u8>,
    len: usize,
    index: usize,
}

impl Bytes {
    fn new(bytes: Vec<u8>) -> Bytes {
        let l = bytes.len();
        Bytes {
            bytes,
            len: l,
            index: 0,
        }
    }
}

#[derive(Debug)]
enum Item {
    Pair(u8, u8),
    Single(u8),
}

impl Iterator for Bytes {
    type Item = Item;

    fn next(&mut self) -> Option<Self::Item> {
        let cur_index = self.index;
        self.index += 1;

        if cur_index == self.len {
            return None;
        }

        if cur_index + 1 == self.len {
            Some(Item::Single(self.bytes[cur_index]))
        } else {
            // if self.index < self.len {
            Some(Item::Pair(self.bytes[cur_index], self.bytes[cur_index + 1]))
        }
    }
}

// The caller has to make sure that crlf2lf is only
// used for text files
pub fn wrong_line_endings2crlf(fname: &str) -> Result<(), io::Error> {
    let path = Path::new(fname);

    let mut hdl_in;
    match File::open(path) {
        Ok(f) => {
            hdl_in = f;
        }
        Err(e) => return Err(e),
    };
    let mut buffer: Vec<u8> = Vec::new();

    // read the whole file
    if let Err(e) = hdl_in.read_to_end(&mut buffer) {
        return Err(e);
    };

    let another_vec: Vec<u8> = fix_line_endings2crlf(&buffer);

    let mut hdl_out;
    match File::create(path) {
        Ok(f) => {
            hdl_out = f;
        }
        Err(e) => return Err(e),
    };

    // write back
    match hdl_out.write(&another_vec) {
        Ok(_) => Ok(()),
        Err(e) => Err(e),
    }
}

// The caller has to make sure that crlf2lf is only
// used for text files
pub fn wrong_line_endings2lf(fname: &str) -> Result<(), io::Error> {
    let path = Path::new(fname);

    let mut hdl_in;
    match File::open(path) {
        Ok(f) => {
            hdl_in = f;
        }
        Err(e) => return Err(e),
    };
    let mut buffer: Vec<u8> = Vec::new();

    // read the whole file
    if let Err(e) = hdl_in.read_to_end(&mut buffer) {
        return Err(e);
    };

    let another_vec: Vec<u8> = fix_line_endings2lf(&buffer);

    let mut hdl_out;
    match File::create(path) {
        Ok(f) => {
            hdl_out = f;
        }
        Err(e) => return Err(e),
    };

    // write back
    match hdl_out.write(&another_vec) {
        Ok(_) => Ok(()),
        Err(e) => Err(e),
    }
}

pub fn fix_line_endings2lf(buffer: &[u8]) -> Vec<u8> {
    let mut another_vec: Vec<u8> = Vec::new();

    const CR: u8 = 0x0d; // 13
    const LF: u8 = 0x0a; // 10

    let b = Bytes::new(buffer.to_vec());

    let mut skip = false;
    for v in b {
        if skip {
            skip = false;
            continue;
        }
        match v {
            Item::Pair(CR, LF) | Item::Pair(LF, CR) => {
                another_vec.push(LF);
                skip = true;
            }
            Item::Pair(CR, _) | Item::Pair(LF, _) | Item::Single(CR) | Item::Single(LF) => {
                another_vec.push(LF);
            }
            Item::Pair(c, _) | Item::Single(c) => {
                another_vec.push(c);
            }
        }
    }

    // if there is no line ending at the end of a fill add it
    if another_vec[another_vec.len() - 1] != LF {
        another_vec.push(LF);
    }
    another_vec
}

pub fn fix_line_endings2crlf(buffer: &[u8]) -> Vec<u8> {
    let mut another_vec: Vec<u8> = Vec::new();

    const CR: u8 = 0x0d; // 13 \r
    const LF: u8 = 0x0a; // 10 \n

    let b = Bytes::new(buffer.to_vec());

    let mut skip = false;
    for v in b {
        if skip {
            skip = false;
            continue;
        }
        match v {
            Item::Pair(CR, LF) | Item::Pair(LF, CR) => {
                another_vec.push(CR);
                another_vec.push(LF);
                skip = true;
            }
            Item::Pair(CR, _) | Item::Pair(LF, _) | Item::Single(CR) | Item::Single(LF) => {
                another_vec.push(CR);
                another_vec.push(LF);
            }
            Item::Pair(c, _) | Item::Single(c) => {
                another_vec.push(c);
            }
        }
    }

    let avlen = another_vec.len();

    if avlen == 1 || (another_vec[avlen - 1] != LF && another_vec[avlen - 2] != CR) {
        another_vec.push(CR);
        another_vec.push(LF);
    }

    another_vec
}

#[test]
fn test_fix_line_endings2crlf() {
    let testfiles = vec![
        ("a", "a\r\n"),
        ("no line ending at all", "no line ending at all\r\n"),
        ("\rbla", "\r\nbla\r\n"),
        ("\r\r", "\r\n\r\n"),
        ("\r\nbla", "\r\nbla\r\n"),
        ("\r\n\r\nbla", "\r\n\r\nbla\r\n"),
        ("bla\r\nbla", "bla\r\nbla\r\n"),
        ("bla\r\n\r\nbla", "bla\r\n\r\nbla\r\n"),
        ("bla\r\nbla\r\nbla", "bla\r\nbla\r\nbla\r\n"),
        ("\n\r", "\r\n"),
        ("\n\r\n\r", "\r\n\r\n"),
        ("\n\rbla\n\r", "\r\nbla\r\n"),
    ];

    for (s, rs) in testfiles {
        let bs = s.as_bytes().to_vec();
        let brs = rs.as_bytes().to_vec();
        assert_eq!(fix_line_endings2crlf(&bs), brs);
    }
}

#[test]
fn test_fix_line_endings() {
    let testfiles = vec![
        ("no line ending at all", "no line ending at all\n"),
        ("\r", "\n"),
        ("\n\r", "\n"),
        ("\r\n", "\n"),
        ("\r\n\n", "\n\n"),
        ("CR at end of file\r", "CR at end of file\n"),
        ("LF at end of file\n", "LF at end of file\n"),
        ("CRLF at end of file\r\n", "CRLF at end of file\n"),
        ("Two CR at end of file\r\r", "Two CR at end of file\n\n"),
        ("Two LF at end of file\n\n", "Two LF at end of file\n\n"),
        (
            "Two CRLF at end of file\r\n\r\n",
            "Two CRLF at end of file\n\n",
        ),
        (
            "Two CR in the middle\r\r of file",
            "Two CR in the middle\n\n of file\n",
        ),
        (
            "Two LF in the middle\n\n of file",
            "Two LF in the middle\n\n of file\n",
        ),
        (
            "Two CRLF in the middle\r\n\r\n of file",
            "Two CRLF in the middle\n\n of file\n",
        ),
        (
            "Mixed CR, CRLF\rline2\r\nline3\r\n",
            "Mixed CR, CRLF\nline2\nline3\n",
        ),
        (
            "Mixed CR, CRLF\r\r\nline2\r\nline3\r\n",
            "Mixed CR, CRLF\n\nline2\nline3\n",
        ),
        //        ("Mixed CR, CRLF\n\r\r\nline2\r\nline3\r\n",   "Mixed CR, CRLF\n\n\nline2\nline3\n"),
        (
            "Mixed CR, LFCR\rline2\n\rline3\r\n",
            "Mixed CR, LFCR\nline2\nline3\n",
        ),
        //("line1\nebla", "line1\rebla"),
        //("mor\r\nebla", "mor\rebla"),
    ];

    for (s, rs) in testfiles {
        let bs = s.as_bytes().to_vec();
        let brs = rs.as_bytes().to_vec();
        assert_eq!(fix_line_endings2lf(&bs), brs);
    }
}