summaryrefslogtreecommitdiffstats
path: root/vendor/snapbox/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/snapbox/src/utils')
-rw-r--r--vendor/snapbox/src/utils/lines.rs31
-rw-r--r--vendor/snapbox/src/utils/mod.rs30
2 files changed, 61 insertions, 0 deletions
diff --git a/vendor/snapbox/src/utils/lines.rs b/vendor/snapbox/src/utils/lines.rs
new file mode 100644
index 000000000..f56408483
--- /dev/null
+++ b/vendor/snapbox/src/utils/lines.rs
@@ -0,0 +1,31 @@
+#[derive(Clone, Debug)]
+pub struct LinesWithTerminator<'a> {
+ data: &'a str,
+}
+
+impl<'a> LinesWithTerminator<'a> {
+ pub fn new(data: &'a str) -> LinesWithTerminator<'a> {
+ LinesWithTerminator { data }
+ }
+}
+
+impl<'a> Iterator for LinesWithTerminator<'a> {
+ type Item = &'a str;
+
+ #[inline]
+ fn next(&mut self) -> Option<&'a str> {
+ match self.data.find('\n') {
+ None if self.data.is_empty() => None,
+ None => {
+ let line = self.data;
+ self.data = "";
+ Some(line)
+ }
+ Some(end) => {
+ let line = &self.data[..end + 1];
+ self.data = &self.data[end + 1..];
+ Some(line)
+ }
+ }
+ }
+}
diff --git a/vendor/snapbox/src/utils/mod.rs b/vendor/snapbox/src/utils/mod.rs
new file mode 100644
index 000000000..d51924196
--- /dev/null
+++ b/vendor/snapbox/src/utils/mod.rs
@@ -0,0 +1,30 @@
+mod lines;
+
+pub use lines::LinesWithTerminator;
+
+/// Normalize line endings
+pub fn normalize_lines(data: &str) -> String {
+ normalize_lines_chars(data.chars()).collect()
+}
+
+fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
+ normalize_line_endings::normalized(data)
+}
+
+/// Normalize path separators
+pub fn normalize_paths(data: &str) -> String {
+ normalize_paths_chars(data.chars()).collect()
+}
+
+fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
+ data.map(|c| if c == '\\' { '/' } else { c })
+}
+
+/// "Smart" text normalization
+///
+/// This includes
+/// - Line endings
+/// - Path separators
+pub fn normalize_text(data: &str) -> String {
+ normalize_paths_chars(normalize_lines_chars(data.chars())).collect()
+}