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
use unicode_segmentation::UnicodeSegmentation;

pub trait TruncateToBoundary {
    fn truncate_to_boundary(&self, chars: usize) -> &Self;
    fn truncate_to_byte_offset(&self, count: usize) -> &Self;
    fn slice_indices_at_boundary(&self, boundary: usize) -> (&Self, usize);
    fn slice_indices_at_offset(&self, offset: usize) -> (&Self, usize);

}

pub trait SplitToBoundary {
    fn split_to_boundary(&self, boundary: usize) -> Vec<&str>;
    fn split_to_offset(&self, offset: usize) -> Vec<&str>;
    fn split_all_to_boundary(&self, boundary: usize) -> Vec<&str>;
}

pub trait SplitInplaceToBoundary {
    fn split_to_offset_inplace(&mut self, offset: usize) -> &mut Self;
    fn split_to_boundary_inplace(&mut self, boundary: usize) -> &mut Self;
}
impl TruncateToBoundary for str {
    /// Truncates a given string to a set numerical boundary.
    /// If the boundary splits a grapheme (e.g., when a character is a resultant mix of more than 1 utf-8 character, like some emojis)
    /// the truncation will scale back to the previous character.
    /// If the truncation ends with white space - this will be trimmed.
    /// Should the truncation boundary exceed the string's size - the original string will return (including whitespace).
    ///
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let s = "🤚🏾a🤚🏾 ";
    ///
    /// assert_eq!(s.truncate_to_boundary(1), "");
    /// assert_eq!(s.truncate_to_boundary(2), "🤚🏾");
    /// assert_eq!(s.truncate_to_boundary(3), "🤚🏾a");
    /// assert_eq!(s.truncate_to_boundary(4), "🤚🏾a");
    /// assert_eq!(s.truncate_to_boundary(5), "🤚🏾a🤚🏾");
    /// assert_eq!(s.truncate_to_boundary(10), s);
    ///```
    fn truncate_to_boundary(&self, chars: usize) -> &Self {
        if chars == 0 {
            return &self[..0];
        }

        let result = match self.char_indices().nth(chars) {
            None => self,
            Some((boundary, _)) => self.truncate_to_byte_offset(boundary)
        };
        result
    }
    /// Truncates a given string based on the provided byte-offset.
    /// If the offset splits a grapheme the truncation will scale back to the previous character.
    /// If the truncation ends with white space - this will be trimmed.
    /// Should the offset exceed the strings size - the original string will return (including whitespace).
    ///
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let s = "🤚🏾a🤚 ";
    ///  // where "🤚🏾" = 8 bytes
    /// assert_eq!(s.truncate_to_byte_offset(0), "");
    /// assert_eq!(s.truncate_to_byte_offset(7), "");
    /// assert_eq!(s.truncate_to_byte_offset(8), "🤚🏾");
    /// assert_eq!(s.truncate_to_byte_offset(9), "🤚🏾a");
    /// assert_eq!(s.truncate_to_byte_offset(10), "🤚🏾a");
    /// assert_eq!(s.truncate_to_byte_offset(18), s);
    /// ```
    fn truncate_to_byte_offset(&self, boundary: usize) -> &Self {
        if boundary > self.len() {
            return &self
        }
        let mut grapheme_iter = self
                .grapheme_indices(true)
                .rev()
                .skip_while(move |(n, _)| *n > boundary);
        let mut bytecount = boundary;
        if let Some((grapheme_boundary, _)) = grapheme_iter.next() {
            bytecount = grapheme_boundary;
        }

        &self[..bytecount].trim_end()
    }

    /// The same as 'truncate_to_boundary' but returns a tuple with the desired slice along with the byte-offset.
    ///
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let s = "🤚🏾a🤚🏾 ";
    /// assert_eq!(s.slice_indices_at_boundary(2), ("🤚🏾", 8));
    /// ```
    fn slice_indices_at_boundary(&self, boundary: usize) -> (&Self, usize) {
        if boundary == 0 {
            return (&self[..0], 0);
        }

        let (result, offset) = match self.char_indices().nth(boundary) {
            None => (self, self.len()),
            Some((b, _char)) => self.slice_indices_at_offset(b)
        };
        (result, offset)
    }

    /// The same as 'truncate_to_byte_offset' but returns a tuple with the desired slice along with the byte-offset.
    /// assert_eq!(s.truncate_to_byte_offset(8), "🤚🏾");
    ///
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let s = "🤚🏾🤚🏾 ";
    /// assert_eq!(s.slice_indices_at_offset(9), ("🤚🏾", 8));
    /// ```
    fn slice_indices_at_offset(&self, boundary: usize) -> (&Self, usize) {
        if boundary > self.len() {
            return (&self, self.len())
        }
        let mut grapheme_iter = self
                .grapheme_indices(true)
                .rev()
                .skip_while(move |(n, _)| *n > boundary);
        let mut bytecount = boundary;
        if let Some((grapheme_boundary, _)) = grapheme_iter.next() {
            bytecount = grapheme_boundary;
        }

        (&self[..bytecount].trim_end(), bytecount)
    }
}

impl SplitToBoundary for str {
    /// performs a 'truncate_to_boundary' and produces a vector with the rest of the string on the right side.
    /// This can be regarded as a left-side-split with unicode awareness trimming.
    fn split_to_boundary(&self, boundary: usize) -> Vec<&str> {
        let (head, offset) = self.slice_indices_at_boundary(boundary);
        if offset == self.len() {
             return vec!(&self)
        }
        vec!(head, &self[offset..])
    }

    /// performs a 'truncate_to_byte_offset' and produces a vector with the rest of the string on the right side.
    /// This can be regarded as a left-side-split with unicode awareness and trimming.
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let s = "🤚🏾a🤚 ";
    /// assert_eq!(s.split_to_offset(7), vec!("", "🤚🏾a🤚 "));
    /// assert_eq!(s.split_to_offset(8), vec!("🤚🏾", "a🤚 "));
    /// ```
    fn split_to_offset(&self, offset: usize) -> Vec<&str> {
        if offset > self.len() {
            return vec!(&self)
        }
        let (head, offset) = self.slice_indices_at_offset(offset);
        vec!(head, &self[offset..])
    }

    /// performs a 'split_to_boundary' until all of the string has been split properly.
    /// Also removes needless spaces and empty strings.
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let mut s = "🤚🏾a🤚 ";
    /// assert_eq!(s.split_all_to_boundary(1), vec!("a", "🤚"));
    /// assert_eq!(s.split_all_to_boundary(2), vec!("🤚🏾", "a🤚",));
    /// ```
    fn split_all_to_boundary(&self, boundary: usize) -> Vec<&str> {
        let mut offset = 0usize;
        let mut result = Vec::new();
        while offset < self.len() {
            let (head, byteoffset) = self[offset..].slice_indices_at_boundary(boundary);
            if byteoffset == 0  {
                 let (_, b) = self[offset..].slice_indices_at_boundary(boundary+1); {
                    offset = offset+b;
                }
            continue
            }
            else if !(head.trim().as_bytes() == b"") {
                result.push(head);
            }
            offset = offset+byteoffset;
        }
        result
    }
}


impl SplitInplaceToBoundary for Vec<&str> {

    /// an adaptor-like function (i.e., chainable) that performs 'split_to_offset' on the vector in place.
    ///
    /// # Examples:
    ///
    /// ```
    /// use truncrate::*;
    /// use truncrate::SplitInplaceToBoundary;
    ///
    /// let mut s = vec!("a", "🤚🏾a🤚 ");
    ///
    /// s.split_to_offset_inplace(9).split_to_offset_inplace(9);
    /// assert_eq!(s, vec!("a", "🤚🏾a", "🤚 "));
    ///
    /// ```
    fn split_to_offset_inplace(&mut self, offset: usize) -> &mut Self {
        if let Some(string) = self.pop() {
            let mut new;
            match string  {
                "" | " " => new = vec!("", ""),
                _ => new = string.split_to_offset(offset)
            }
            self.append(&mut new);
        }
        self
    }

    /// an adaptor-like function (i.e., chainable) that performs 'split_to_boundary' on the vector in place.
    ///
    /// ```
    /// use truncrate::*;
    ///
    /// let mut s = vec!("🤚🏾a🤚 ", "🤚🏾🤚🏾 ");
    /// s.split_to_boundary_inplace(2);
    /// assert_eq!(s, vec!("🤚🏾a🤚 ","🤚🏾", "🤚🏾 "));
    /// s.split_to_boundary_inplace(2);
    /// assert_eq!(s, vec!("🤚🏾a🤚 ","🤚🏾", "🤚🏾"," "));
    ///
    /// let mut s2 = vec!("🤚🏾a🤚 ", "🤚🏾🤚🏾 ");
    /// s2.split_to_boundary_inplace(2).split_to_boundary_inplace(2);
    /// assert_eq!(s2, vec!("🤚🏾a🤚 ","🤚🏾", "🤚🏾"," "));
    ///
    /// ```
    fn split_to_boundary_inplace(&mut self, offset: usize) -> &mut Self {
        if let Some(string) = self.pop() {
            let mut new;
            match string  {
                "" | " " => new = vec!("", ""),
                _ => new = string.split_to_boundary(offset)
            }
            self.append(&mut new);
        }
        self
    }
}

impl SplitToBoundary for Vec<&str> {
    /// performs a 'split_to_boundary' on the last element of a Vec<&str> thus adding another item to the list with the truncated string based on a character boundary.
    fn split_to_boundary(&self, boundary: usize) -> Vec<&str> {
        let mut result = self.clone();
        if let Some(string) = result.pop(){
            let mut new = string.split_to_boundary(boundary);
            result.append(&mut new);
        }
        result
    }

    /// performs a 'split_to_offset' on the last element of a Vec<&str> thus adding another item to the list with the truncated string based byte limitation.
    fn split_to_offset(&self, offset: usize) -> Vec<&str> {
        let mut result = self.clone();
        if let Some(string) = result.pop(){
            let mut new = string.split_to_offset(offset);
            result.append(&mut new);
        }
        result
    }

    /// performs a split_all_to_boundary on the last element of a Vec<&str> thus performing a unicode aware split on of its strings, along with trimming and removal of empty strings.
    fn split_all_to_boundary(&self, boundary: usize) -> Vec<&str> {
        let mut result = self.clone();
        if let Some(string) = result.pop(){
            let mut new = string.split_all_to_boundary(boundary);
            result.append(&mut new);
        }
        result
    }
}

/// removes empty string ("" or " ") from a Vec<&str>.
pub fn sanitize_string_vec(list: Vec<&str>) -> Vec<&str> {
    list.iter().
    filter(|&&x| x.trim().as_bytes() != b"")
    .map(|x| *x)
    .collect()
}

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

    #[test]
    fn test() {
        let s = "🤚🏾a🤚🏾 🤚🏾\t 🤚🏾";

        assert_eq!(s.truncate_to_boundary(1), "");
        assert_eq!(s.truncate_to_boundary(2), "🤚🏾");
        assert_eq!(s.truncate_to_boundary(3), "🤚🏾a");
        assert_eq!(s.truncate_to_boundary(4), "🤚🏾a");
        assert_eq!(s.truncate_to_boundary(6), "🤚🏾a🤚🏾");
        assert_eq!(s.truncate_to_boundary(7), "🤚🏾a🤚🏾");
        assert_eq!(s.truncate_to_boundary(8), "🤚🏾a🤚🏾 🤚🏾");
        assert_eq!(s.truncate_to_boundary(9), "🤚🏾a🤚🏾 🤚🏾");
        assert_eq!(s.truncate_to_boundary(10), "🤚🏾a🤚🏾 🤚🏾");
        assert_eq!(s.truncate_to_boundary(11), "🤚🏾a🤚🏾 🤚🏾");
        assert_eq!(s.truncate_to_boundary(12), s);
    }

    #[test]
    fn test_non_trucated_string() {
        let s = "🤚🏾a🤚🏾 🤚🏾  🤚🏾";

        assert_eq!(s.truncate_to_boundary(100), s);
        assert_eq!(s.truncate_to_boundary(s.chars().count()), s);
        assert_eq!(s.truncate_to_boundary(0), "");
    }

    #[test]
    fn truncate_non_split_grapheme() {
        let s = "🤚🏾a🤚 🤚🏾\t 🤚    ";

        assert_eq!(s.truncate_to_boundary(4), "🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(5), "🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(6), "🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(7), "🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(8), "🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(9), "🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(10), "🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(11), "🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(12), "🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(20), s);
    }

    #[test]
    fn truncate_non_split_grapheme_with_whitespace() {
        let s = " 🤚🏾a🤚 🤚🏾\t 🤚    ";

        assert_eq!(s.truncate_to_boundary(5), " 🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(6), " 🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(7), " 🤚🏾a🤚");
        assert_eq!(s.truncate_to_boundary(8), " 🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(9), " 🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(10), " 🤚🏾a🤚 🤚🏾");
        assert_eq!(s.truncate_to_boundary(11), " 🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(12), " 🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(13), " 🤚🏾a🤚 🤚🏾\t 🤚");
        assert_eq!(s.truncate_to_boundary(21), s);
    }
    #[test]
    fn truncate_to_bytes(){
        let s = "🤚🏾a🤚 ";

        assert_eq!(s.truncate_to_byte_offset(1), "");
        assert_eq!(s.truncate_to_byte_offset(2), "");
        assert_eq!(s.truncate_to_byte_offset(13), "🤚🏾a🤚");
        assert_eq!(s.truncate_to_byte_offset(14), "🤚🏾a🤚");
        assert_eq!(s.truncate_to_byte_offset(18), s);
        assert_eq!(s.truncate_to_byte_offset(100), s);
    }

    #[test]
    fn test_split_bytes(){
        let s = "🤚🏾a🤚 ";
        assert_eq!(s.split_to_offset(7), vec!("", "🤚🏾a🤚 "));
        assert_eq!(s.split_to_offset(9), vec!("🤚🏾a", "🤚 "));
    }

    #[test]
    fn test_split_boundary(){
        let s = "🤚🏾a🤚 ";
        assert_eq!(s.split_to_boundary(1), vec!("", "🤚🏾a🤚 "));
        assert_eq!(s.split_to_boundary(2), vec!("🤚🏾", "a🤚 "));
        assert_eq!(s.split_to_boundary(3), vec!("🤚🏾a", "🤚 "));
        assert_eq!(s.split_to_boundary(4), vec!("🤚🏾a🤚", " "));
        assert_eq!(s.split_to_boundary(5), vec!(s));
        assert_eq!(s.split_to_boundary(6), vec!(s));
        assert_eq!(s.split_to_boundary(15), vec!(s));
    }


    #[test]
    fn test_split_all(){
        let s = "🤚🏾a🤚 ";
        assert_eq!(s.split_all_to_boundary(3), vec!("🤚🏾a", "🤚 "));
        assert_eq!(s.split_all_to_boundary(4), vec!("🤚🏾a🤚"));
        assert_eq!(s.split_all_to_boundary(14), vec!("🤚🏾a🤚 "));

        let s1 = "🤚🏾a🤚🏾 ";
        assert_eq!(s1.split_all_to_boundary(1), vec!("a"));
        assert_eq!(s1.split_all_to_boundary(2), vec!("🤚🏾", "a", "🤚🏾"));
    }

    #[test]
    fn test_inplace_vector_chaining_boundry(){
        let mut s = vec!("🤚🏾a🤚 ", "🤚🤚🤚 ");
        s
                .split_to_boundary_inplace(1)
                .split_to_boundary_inplace(1)
                .split_to_boundary_inplace(1);
        assert_eq!(s, vec!("🤚🏾a🤚 ", "🤚", "🤚", "🤚", " "));

        let mut s1 = vec!("🤚🏾a🤚 ", "🤚🏾 🤚 ");
        s1.split_to_boundary_inplace(1);
        assert_eq!(s1, vec!("🤚🏾a🤚 ", "", "🤚🏾 🤚 "));
        s1.split_to_boundary_inplace(3);
        assert_eq!(s1, vec!("🤚🏾a🤚 ", "", "🤚🏾", "🤚 "));
    }

  #[test]
  fn test_test_vector_chaining_offset() {
      let mut s = vec!("🤚🏾a🤚 ", "🤚🏾🤚🏾🤚🏾  ");
      s.split_to_offset_inplace(9)
              .split_to_offset_inplace(8)
              .split_to_offset_inplace(10);
      assert_eq!(s, vec!("🤚🏾a🤚 ", "🤚🏾", "🤚🏾", "🤚🏾", " "));
    }

  #[test]
  fn test_vector_split_all() {
    let s = vec!("🤚🏾a🤚 ", "🤚🤚🤚 ");
    assert_eq!(s.split_all_to_boundary(1), vec!("🤚🏾a🤚 ", "🤚", "🤚", "🤚"));
    let s1 = vec!("🤚🏾a🤚 ", "🤚🏾a🤚🏾 ");
    assert_eq!(s1.split_all_to_boundary(1), vec!("🤚🏾a🤚 ", "a"));
    assert_eq!(s1.split_all_to_boundary(2), vec!("🤚🏾a🤚 ", "🤚🏾", "a", "🤚🏾"));
    }
}