forked from pascalkuthe/imara-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources.rs
More file actions
179 lines (144 loc) · 4.88 KB
/
Copy pathsources.rs
File metadata and controls
179 lines (144 loc) · 4.88 KB
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
//! Utilities for creating token sources from common data types.
//!
//! This module provides implementations of [`TokenSource`] for
//! strings and byte slices, splitting them into lines by default.
use std::str::from_utf8_unchecked;
use memchr::memchr;
use crate::TokenSource;
/// Returns a [`TokenSource`] that uses the lines in `data` as Tokens. The newline
/// separator (`\r\n` or `\n`) is included in the emitted tokens. This means that changing
/// the newline separator from `\r\n` to `\n` (or omitting it fully on the last line) is
/// detected by [`Diff`](crate::Diff).
pub fn lines(data: &str) -> Lines<'_> {
Lines(ByteLines(data.as_bytes()))
}
/// Returns a [`TokenSource`] that uses the words in `data` as Tokens. A word is
/// a sequence of alphanumeric characters as determined by
/// `char::is_alphanumeric`, or a sequence of just the space character ' '. Any
/// other characters are their own word.
pub fn words(data: &str) -> Words<'_> {
Words(data)
}
/// Returns a [`TokenSource`] that uses the lines in `data` as Tokens. The newline
/// separator (`\r\n` or `\n`) is included in the emitted tokens. This means that changing
/// the newline separator from `\r\n` to `\n` (or omitting it fully on the last line) is
/// detected when computing a [`Diff`](crate::Diff).
pub fn byte_lines(data: &[u8]) -> ByteLines<'_> {
ByteLines(data)
}
/// By default, a line diff is produced for a string
impl<'a> TokenSource for &'a str {
type Token = &'a str;
type Tokenizer = Lines<'a>;
fn tokenize(&self) -> Self::Tokenizer {
lines(self)
}
fn estimate_tokens(&self) -> u32 {
lines(self).estimate_tokens()
}
}
/// By default, a line diff is produced for a bytes
impl<'a> TokenSource for &'a [u8] {
type Token = Self;
type Tokenizer = ByteLines<'a>;
fn tokenize(&self) -> Self::Tokenizer {
byte_lines(self)
}
fn estimate_tokens(&self) -> u32 {
byte_lines(self).estimate_tokens()
}
}
/// A [`TokenSource`] that returns the lines of a `str` as tokens. See [`lines`] for
/// details.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Lines<'a>(ByteLines<'a>);
impl<'a> Iterator for Lines<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
// safety invariant: this struct may only contain valid utf8
// dividing valid utf8 bytes by ascii characters always produces valid utf-8
self.0.next().map(|it| unsafe { from_utf8_unchecked(it) })
}
}
/// By default, a line diff is produced for a string
impl<'a> TokenSource for Lines<'a> {
type Token = &'a str;
type Tokenizer = Self;
fn tokenize(&self) -> Self::Tokenizer {
*self
}
fn estimate_tokens(&self) -> u32 {
self.0.estimate_tokens()
}
}
/// A [`TokenSource`] that returns the words of a string as tokens. See
/// [`words`] for details.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Words<'a>(&'a str);
impl<'a> Iterator for Words<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
return None;
}
let initial = self.0.chars().next().unwrap();
let word_len = if initial == ' ' {
self.0
.char_indices()
.find(|(_, c)| *c != ' ')
.map_or(self.0.len(), |(index, _)| index)
} else if initial.is_alphanumeric() {
self.0
.char_indices()
.find(|(_, c)| !c.is_alphanumeric() && *c != '_')
.map_or(self.0.len(), |(index, _)| index)
} else {
initial.len_utf8()
};
let (word, rem) = self.0.split_at(word_len);
self.0 = rem;
Some(word)
}
}
impl<'a> TokenSource for Words<'a> {
type Token = &'a str;
type Tokenizer = Self;
fn tokenize(&self) -> Self::Tokenizer {
*self
}
fn estimate_tokens(&self) -> u32 {
(self.0.len() / 3) as u32
}
}
/// A [`TokenSource`] that returns the lines of a byte slice as tokens. See [`byte_lines`]
/// for details.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ByteLines<'a>(&'a [u8]);
impl<'a> Iterator for ByteLines<'a> {
type Item = &'a [u8];
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
return None;
}
let line_len = memchr(b'\n', self.0).map_or(self.0.len(), |len| len + 1);
let (line, rem) = self.0.split_at(line_len);
self.0 = rem;
Some(line)
}
}
/// By default, a line diff is produced for a string
impl<'a> TokenSource for ByteLines<'a> {
type Token = &'a [u8];
type Tokenizer = Self;
fn tokenize(&self) -> Self::Tokenizer {
*self
}
fn estimate_tokens(&self) -> u32 {
let len: usize = self.take(20).map(|line| line.len()).sum();
if len == 0 {
100
} else {
(self.0.len() * 20 / len) as u32
}
}
}