-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from dfinance/windows-line-endings
add \r\n -> \n conversion with source map
- Loading branch information
Showing
4 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::shared::errors::FileSourceMap; | ||
use std::str::Chars; | ||
|
||
struct NewNormalized<'a> { | ||
chars: Chars<'a>, | ||
prev_was_carriage_return: bool, | ||
pos: usize, | ||
source_map: &'a mut FileSourceMap, | ||
} | ||
|
||
impl<'a> NewNormalized<'a> { | ||
fn inner_next(&mut self) -> Option<char> { | ||
self.pos += 1; | ||
self.chars.next() | ||
} | ||
} | ||
|
||
const PATTERN_LENGTH: usize = 2; | ||
|
||
impl Iterator for NewNormalized<'_> { | ||
type Item = char; | ||
|
||
fn next(&mut self) -> Option<char> { | ||
match self.inner_next() { | ||
Some('\n') if self.prev_was_carriage_return => { | ||
self.source_map.insert_layer(self.pos - PATTERN_LENGTH, 1); | ||
|
||
self.prev_was_carriage_return = false; | ||
match self.inner_next() { | ||
Some('\r') => { | ||
self.prev_was_carriage_return = true; | ||
Some('\n') | ||
} | ||
any => { | ||
self.prev_was_carriage_return = false; | ||
any | ||
} | ||
} | ||
} | ||
Some('\r') => { | ||
self.prev_was_carriage_return = true; | ||
Some('\n') | ||
} | ||
any => { | ||
self.prev_was_carriage_return = false; | ||
any | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn normalize(s: String) -> (String, FileSourceMap) { | ||
let mut source_map = FileSourceMap::default(); | ||
let normalized = NewNormalized { | ||
chars: s.chars(), | ||
prev_was_carriage_return: false, | ||
pos: 0, | ||
source_map: &mut source_map, | ||
}; | ||
let normalized_string = normalized.collect::<String>(); | ||
(normalized_string, source_map) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters