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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! Report on or fix beginning of line spacing
//!
//! To find out the line beginnings given a [`Read`] trait object use [`read_bol_info()`]:
//!
//! ```
//! use std::error::Error;
//! use std::fs::File;
//! use whitespace_rs::spacer;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!   let mut reader = "abc\n\r\r\n".as_bytes();
//!   let bol_info = spacer::read_bol_info(&mut reader)?;
//!
//!   println!("{:?}", bol_info);
//!   Ok(())
//! }
//! ```
//!
//! To normalize line beginnings given a [`Read`] trait object, create a [`Write`] trait object and use [`write_new_bols()`]:
//!
//! ```
//! use std::error::Error;
//! use std::fs::File;
//! use whitespace_rs::spacer;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//!   let mut reader = "abc\n\r\r\n".as_bytes();
//!   let mut writer = Vec::new();
//!   let bol_info = spacer::write_new_bols(&mut reader, &mut writer, spacer::BeginningOfLine::Tabs(2, true))?;
//!
//!   println!("{:?}", bol_info);
//!   Ok(())
//! }
//! ```

use std::cmp::max;
use std::error::Error;
use std::io::{Read, Write};
use utf8_decode::UnsafeDecoder;

// {grcov-excl-start}
#[derive(Debug, PartialEq)]
/// Types of line beginnings
pub enum BeginningOfLine {
  /// Tabs (and spaces if not rounding down extra spaces)
  Tabs(usize, bool),
  /// Spaces
  Spaces(usize),
}
// {grcov-excl-end}

#[derive(Debug, PartialEq)]
/// Information about line beginnings in the file
pub struct BolInfo {
  /// Number of lines that have no whitespace at the beginning
  pub none: usize,
  /// Number of all space line beginnings
  pub spaces: usize,
  /// Number of all tab line beginnings
  pub tabs: usize,
  /// Number of mixed space/tab line beginnings
  pub mixed: usize,
}

impl Eq for BolInfo {}

impl BolInfo {
  /// Get the most common beginning of line type in the file
  pub fn get_common_bol(self: &Self, tab_size: usize, round_down: bool) -> BeginningOfLine {
    if self.tabs > self.spaces {
      BeginningOfLine::Tabs(tab_size, round_down)
    } else {
      BeginningOfLine::Spaces(tab_size)
    }
  }
}

/// Read beginning of line information
pub fn read_bol_info(reader: &mut dyn Read) -> Result<BolInfo, Box<dyn Error>> {
  let mut bol_info = BolInfo {
    none: 0,
    spaces: 0,
    tabs: 0,
    mixed: 0,
  };
  let mut decoder = UnsafeDecoder::new(reader.bytes()).peekable();
  let mut at_bol = true;
  let (mut num_spaces, mut num_tabs) = (0, 0);

  loop {
    let c;
    match decoder.next() {
      Some(value) => c = value?,
      None => break,
    };

    if at_bol {
      if c == ' ' {
        num_spaces += 1;
      } else if c == '\t' {
        num_tabs += 1;
      } else {
        if num_spaces == 0 && num_tabs == 0 {
          bol_info.none += 1;
        } else if num_spaces > 0 && num_tabs > 0 {
          bol_info.mixed += 1;
        } else if num_spaces > 0 {
          bol_info.spaces += 1;
        } else {
          bol_info.tabs += 1;
        }
        at_bol = false;
      }
    } else if c == '\n' {
      num_spaces = 0;
      num_tabs = 0;
      at_bol = true;
    }
  }

  Ok(bol_info)
}

/// Write input file out with new beginning-of-lines
pub fn write_new_bols(
  reader: &mut dyn Read,
  writer: &mut dyn Write,
  new_bol: BeginningOfLine,
) -> Result<BolInfo, Box<dyn Error>> {
  let (tab_size, round_down) = match new_bol {
    BeginningOfLine::Spaces(tab_size) => (max(1, tab_size), false),
    BeginningOfLine::Tabs(tab_size, round_down) => (max(1, tab_size), round_down),
  };
  let mut bol_info = BolInfo {
    none: 0,
    spaces: 0,
    tabs: 0,
    mixed: 0,
  };
  let mut decoder = UnsafeDecoder::new(reader.bytes()).peekable();
  let mut buf = [0u8; 4];
  let mut s = String::new();
  let mut at_bol = true;
  let untabify = |s: &str| -> String {
    let mut t = String::new();

    for c in s.chars() {
      if c == '\t' {
        t.push_str(&" ".repeat(tab_size - (t.len() % tab_size)));
      } else {
        t.push(c);
      }
    }

    t
  };
  let tabify = |s: &str| -> (_, _) {
    let mut num_spaces = 0;
    let mut t = String::new();

    for c in s.chars() {
      if c == ' ' {
        num_spaces += 1;
      }

      if num_spaces % tab_size == 0 {
        t.push('\t');
        num_spaces = 0
      }
    }

    if num_spaces > 0 {
      if !round_down {
        t.push_str(&" ".repeat(num_spaces));
      } else {
        num_spaces = 0;
      }
    }

    (t, num_spaces)
  };

  loop {
    let c;

    match decoder.next() {
      Some(value) => c = value?,
      None => break,
    };
    if at_bol {
      if c == ' ' || c == '\t' {
        s.push(c);
      } else {
        if s.len() == 0 {
          bol_info.none += 1
        } else {
          s = untabify(&s);

          if let BeginningOfLine::Tabs(_, _) = new_bol {
            let (t, num_spaces) = tabify(&s);

            s = t;
            if num_spaces > 0 {
              bol_info.mixed += 1;
            } else {
              bol_info.tabs += 1;
            }
          } else {
            bol_info.spaces += 1;
          }

          writer.write(s.as_bytes())?;
        }

        writer.write(c.encode_utf8(&mut buf).as_bytes())?;

        if c == '\n' {
          s.clear();
        } else {
          at_bol = false;
        }
      }
    } else {
      writer.write(c.encode_utf8(&mut buf).as_bytes())?;

      if c == '\n' {
        s.clear();
        at_bol = true;
      }
    }
  }
  writer.flush()?;

  Ok(bol_info)
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_read_bol_info() {
    let bol_info = read_bol_info(&mut "a\n\tb\n  c\n \td\n".as_bytes()).unwrap();

    assert_eq!(
      bol_info,
      BolInfo {
        none: 1,
        spaces: 1,
        tabs: 1,
        mixed: 1,
      }
    );
  }

  #[test]
  fn test_write_new_file_tabs_round_down() {
    let mut input = "\na\n  b\n     c\n".as_bytes();
    let mut output = Vec::new();
    let bol_info = write_new_bols(&mut input, &mut output, BeginningOfLine::Tabs(2, true)).unwrap();

    assert_eq!(
      bol_info,
      BolInfo {
        none: 2,
        spaces: 0,
        tabs: 2,
        mixed: 0
      }
    );
    assert_eq!(String::from_utf8(output).unwrap(), "\na\n\tb\n\t\tc\n");
  }

  #[test]
  fn test_write_new_file_tabs_no_round_down() {
    let mut input = "\na\n  b\n     c\n".as_bytes();
    let mut output = Vec::new();
    let bol_info =
      write_new_bols(&mut input, &mut output, BeginningOfLine::Tabs(2, false)).unwrap();

    assert_eq!(
      bol_info,
      BolInfo {
        none: 2,
        spaces: 0,
        tabs: 1,
        mixed: 1
      }
    );
    assert_eq!(String::from_utf8(output).unwrap(), "\na\n\tb\n\t\t c\n");
  }

  #[test]
  fn test_write_new_file_spaces() {
    let mut input = "\ta\n \t x\n\t\t\n".as_bytes();
    let mut output = Vec::new();
    let bol_info = write_new_bols(&mut input, &mut output, BeginningOfLine::Spaces(2)).unwrap();

    assert_eq!(
      bol_info,
      BolInfo {
        none: 0,
        spaces: 3,
        tabs: 0,
        mixed: 0
      }
    );
    assert_eq!(String::from_utf8(output).unwrap(), "  a\n   x\n    \n");
  }
}