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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#[cfg(test)]
mod test;
mod index;
mod iter;
mod error;
pub use self::{
error::{
DiacriticAtStart,
InvalidControl,
NotAGrapheme,
TermGraphemeError,
TermStringError,
},
index::Index,
iter::{TermStringIndices, TermStringIter},
};
use lazy_static::lazy_static;
use std::{
cmp::Ordering,
convert::TryFrom,
fmt,
hash::{Hash, Hasher},
iter::FromIterator,
ops::{Deref, Range},
path::Path,
sync::Arc,
};
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Clone)]
pub struct TermString {
alloc: Arc<str>,
range: Range<usize>,
}
impl TermString {
pub fn new<S>(string: S) -> Result<Self, TermStringError>
where
S: AsRef<str>,
{
for (position, ch) in string.as_ref().char_indices() {
if ch.is_control() {
Err(InvalidControl { position })?;
}
}
let mut new_string = string.as_ref().to_owned();
new_string.replace_range(0 .. 0, "a");
let mut iter = new_string.grapheme_indices(true);
iter.next();
let index = iter.next().map_or(new_string.len(), |(index, _)| index);
if index != 1 {
Err(DiacriticAtStart)?;
}
new_string.replace_range(0 .. 1, "");
let range = 0 .. new_string.len();
Ok(TermString { alloc: new_string.into(), range })
}
pub fn new_lossy<S>(string: S) -> Self
where
S: AsRef<str>,
{
let mut new_string = String::new();
for ch in string.as_ref().chars() {
new_string.push(if ch.is_control() { '�' } else { ch });
}
new_string.replace_range(0 .. 0, "a");
let mut iter = new_string.grapheme_indices(true);
iter.next();
let index = iter.next().map_or(new_string.len(), |(index, _)| index);
let replacement = if index != 1 { "�" } else { "" };
new_string.replace_range(0 .. 1, replacement);
let range = 0 .. new_string.len();
TermString { alloc: new_string.into(), range }
}
pub fn count_graphemes(&self) -> usize {
self.as_str().graphemes(true).count()
}
pub fn as_str(&self) -> &str {
&self.alloc[self.range.clone()]
}
pub fn get<I>(&self, index: I) -> Option<I::Output>
where
I: Index,
{
index.get(self)
}
pub fn index<I>(&self, index: I) -> I::Output
where
I: Index,
{
index.index(self)
}
pub fn indices(&self) -> TermStringIndices {
TermStringIndices::new(self)
}
pub fn iter(&self) -> TermStringIter {
self.into_iter()
}
pub fn full_buf(self) -> Self {
Self { alloc: self.alloc.clone(), range: 0 .. self.alloc.len() }
}
}
impl FromIterator<TermGrapheme> for TermString {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = TermGrapheme>,
{
let mut buf = String::new();
for grapheme in iter {
buf.push_str(grapheme.as_str());
}
let range = 0 .. buf.len();
Self { alloc: buf.into(), range }
}
}
impl FromIterator<TermString> for TermString {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = TermString>,
{
let mut buf = String::new();
for gstr in iter {
buf.push_str(gstr.as_str());
}
let range = 0 .. buf.len();
Self { alloc: buf.into(), range }
}
}
impl<'buf> FromIterator<&'buf TermGrapheme> for TermString {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = &'buf TermGrapheme>,
{
let mut buf = String::new();
for grapheme in iter {
buf.push_str(grapheme.as_str());
}
let range = 0 .. buf.len();
Self { alloc: buf.into(), range }
}
}
impl<'buf> FromIterator<&'buf TermString> for TermString {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = &'buf TermString>,
{
let mut buf = String::new();
for gstr in iter {
buf.push_str(gstr.as_str());
}
let range = 0 .. buf.len();
Self { alloc: buf.into(), range }
}
}
impl<'buf> FromIterator<StringOrGraphm<'buf>> for TermString {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = StringOrGraphm<'buf>>,
{
let mut buf = String::with_capacity(80);
for gstr in iter {
buf.push_str(gstr.as_str());
}
let range = 0 .. buf.len();
Self { alloc: buf.into(), range }
}
}
impl Default for TermString {
fn default() -> Self {
Self { alloc: Arc::from(""), range: 0 .. 0 }
}
}
impl AsRef<str> for TermString {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<Path> for TermString {
fn as_ref(&self) -> &Path {
self.as_str().as_ref()
}
}
impl Deref for TermString {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl PartialEq for TermString {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.alloc, &other.alloc) && self.range == other.range
|| self.as_str() == other.as_str()
}
}
impl Eq for TermString {}
impl PartialOrd for TermString {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl Ord for TermString {
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl Hash for TermString {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.as_str().hash(state)
}
}
impl fmt::Display for TermString {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.as_str())
}
}
impl<'buf> TryFrom<&'buf str> for TermString {
type Error = TermStringError;
fn try_from(buf: &'buf str) -> Result<Self, Self::Error> {
Self::new(buf)
}
}
impl TryFrom<String> for TermString {
type Error = TermStringError;
fn try_from(buf: String) -> Result<Self, Self::Error> {
Self::new(buf)
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TermGrapheme {
tstring: TermString,
}
impl Default for TermGrapheme {
fn default() -> Self {
lazy_static! {
static ref DEFAULT_GRAPHM: Arc<str> = Arc::from(" ");
}
let alloc = DEFAULT_GRAPHM.clone();
let range = 0 .. alloc.len();
Self { tstring: TermString { alloc, range } }
}
}
impl fmt::Display for TermGrapheme {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.tstring)
}
}
impl Deref for TermGrapheme {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl TermGrapheme {
pub fn new<S>(string: S) -> Result<Self, TermGraphemeError>
where
S: AsRef<str> + Into<String>,
{
let first = string
.as_ref()
.graphemes(true)
.next()
.ok_or_else(|| NotAGrapheme)?;
if first.len() != string.as_ref().len() {
Err(NotAGrapheme)?;
}
for (position, ch) in string.as_ref().char_indices() {
if ch.is_control() {
Err(InvalidControl { position })?;
}
}
let mut new_string = string.into();
new_string.replace_range(0 .. 0, "a");
let count = new_string.graphemes(true).count();
if count == 1 {
Err(DiacriticAtStart)?;
}
new_string.replace_range(0 .. 1, "");
let range = 0 .. new_string.len();
let tstring = TermString { alloc: new_string.into(), range };
Ok(Self { tstring })
}
pub fn new_lossy<S>(string: S) -> Self
where
S: Into<String> + AsRef<str>,
{
let actual_str = string.as_ref().graphemes(true).next().unwrap_or("�");
let mut new_string = String::new();
for ch in actual_str.chars() {
new_string.push(if ch.is_control() { '�' } else { ch });
}
new_string.replace_range(0 .. 0, "a");
let mut iter = new_string.grapheme_indices(true);
iter.next();
let index = iter.next().map_or(new_string.len(), |(index, _)| index);
let replacement = if index != 1 { "�" } else { "" };
new_string.replace_range(0 .. 1, replacement);
let range = 0 .. new_string.len();
let tstring = TermString { alloc: new_string.into(), range };
Self { tstring }
}
pub fn space() -> TermGrapheme {
Self::default()
}
pub fn as_str(&self) -> &str {
&self.tstring
}
pub fn as_tstring(&self) -> &TermString {
&self.tstring
}
pub fn into_tstring(self) -> TermString {
self.tstring
}
}
impl<'buf> TryFrom<&'buf str> for TermGrapheme {
type Error = TermGraphemeError;
fn try_from(buf: &'buf str) -> Result<Self, Self::Error> {
Self::new(buf)
}
}
impl TryFrom<String> for TermGrapheme {
type Error = TermGraphemeError;
fn try_from(buf: String) -> Result<Self, Self::Error> {
Self::new(buf)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StringOrGraphm<'buf> {
Graphm(&'buf TermGrapheme),
String(&'buf TermString),
}
impl<'buf> StringOrGraphm<'buf> {
pub fn as_str(self) -> &'buf str {
match self {
StringOrGraphm::Graphm(grapheme) => grapheme.as_str(),
StringOrGraphm::String(gstr) => gstr.as_str(),
}
}
pub fn as_tstring(self) -> &'buf TermString {
match self {
StringOrGraphm::Graphm(grapheme) => grapheme.as_tstring(),
StringOrGraphm::String(gstr) => gstr,
}
}
}
impl<'buf> AsRef<str> for StringOrGraphm<'buf> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<'buf> AsRef<TermString> for StringOrGraphm<'buf> {
fn as_ref(&self) -> &TermString {
self.as_tstring()
}
}
impl<'buf> Deref for StringOrGraphm<'buf> {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.as_tstring()
}
}
impl<'buf> From<&'buf TermGrapheme> for StringOrGraphm<'buf> {
fn from(grapheme: &'buf TermGrapheme) -> StringOrGraphm<'buf> {
StringOrGraphm::Graphm(grapheme)
}
}
impl<'buf> From<&'buf TermString> for StringOrGraphm<'buf> {
fn from(tstring: &'buf TermString) -> StringOrGraphm<'buf> {
StringOrGraphm::String(tstring)
}
}