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
//! Facilities for customizing grapheme cluster segmentation.
//!
//! (**Note:** the functionality in this module is niche.  Unless you
//! have a specific use-case you can safely ignore it and just use
//! the default `Rope` configuration.)
//!
//! Although the default grapheme cluster segmentation in Ropey works
//! for the vast majority of use-cases, there are some niche cases that
//! may require a custom grapheme cluster definition.  This module
//! allows such customization through the
//! [`GraphemeSegmenter`](trait.GraphemeSegmenter.html) trait.
//!
//! For example, if you want a grapheme cluster definition that considers
//! the "ch" pair and _only_ the "ch" pair to be a grapheme cluster, you
//! can implement it like this:
//!
//! ```
//! use ropey::segmentation::GraphemeSegmenter;
//!
//! #[derive(Debug, Copy, Clone)]
//! struct ChSegmenter {}
//!
//! impl GraphemeSegmenter for ChSegmenter {
//!     fn seam_is_break(left: &str, right: &str) -> bool {
//!         let left_ends_in_c
//!             = *left.as_bytes().last().unwrap() == 'c' as u8;
//!         let right_starts_with_h
//!             = right.as_bytes()[0] == 'h' as u8;
//!         !(left_ends_in_c && right_starts_with_h)
//!     }
//! }
//! ```
//!
//! (Note: types implementing `GraphemeSegmenter` are never actually
//! instantiated by Ropey.  They're just a way to statically pass the needed
//! functions to `Rope` and ensure that grapheme-incompatible `Rope`s aren't
//! mixed in incorrect ways.)
//!
//! To create a `Rope` that uses our "ch" grapheme segmenter we can use one
//! of the `*_with_segmenter()` methods on `Rope` or `RopeBuilder`.  For
//! example:
//!
//! ```
//! # use ropey::segmentation::GraphemeSegmenter;
//! #
//! # #[derive(Debug, Copy, Clone)]
//! # struct ChSegmenter {}
//! #
//! # impl GraphemeSegmenter for ChSegmenter {
//! #     fn seam_is_break(left: &str, right: &str) -> bool {
//! #         let left_ends_in_c = *left.as_bytes().last().unwrap() == 'c' as u8;
//! #         let right_starts_with_h = right.as_bytes()[0] == 'h' as u8;
//! #         !(left_ends_in_c && right_starts_with_h)
//! #     }
//! # }
//! #
//! use ropey::Rope;
//!
//! // Create a rope with our custom "ch" segmenter.
//! let mut rope = Rope::<ChSegmenter>::from_str_with_segmenter("Hi chap!");
//!
//! // Verify that it works.
//! let mut itr = rope.graphemes();
//! assert_eq!(itr.next(), Some("H"));
//! assert_eq!(itr.next(), Some("i"));
//! assert_eq!(itr.next(), Some(" "));
//! assert_eq!(itr.next(), Some("ch"));  // <- Grapheme!
//! assert_eq!(itr.next(), Some("a"));
//! assert_eq!(itr.next(), Some("p"));
//! assert_eq!(itr.next(), Some("!"));
//! assert_eq!(itr.next(), None);
//! ```
//!
//! If rather than completely replacing the default grapheme segmenter you
//! instead want to add more graphemes on top of it, you can this (using
//! "ch" as an example again):
//!
//! ```
//! use ropey::segmentation::{GraphemeSegmenter, DefaultSegmenter};
//!
//! #[derive(Debug, Copy, Clone)]
//! struct ChSegmenter {}
//!
//! impl GraphemeSegmenter for ChSegmenter {
//!     fn seam_is_break(left: &str, right: &str) -> bool {
//!         let default
//!             = DefaultSegmenter::seam_is_break(left, right);
//!         let left_ends_in_c
//!             = *left.as_bytes().last().unwrap() == 'c' as u8;
//!         let right_starts_with_h
//!             = right.as_bytes()[0] == 'h' as u8;
//!
//!         default && !(left_ends_in_c && right_starts_with_h)
//!     }
//! }
//! ```

use std::fmt::Debug;
use std::marker::PhantomData;

use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};

/// Trait for implementing grapheme segmentation strategies for [`Rope`](../struct.Rope.html).
///
/// See the documentation of the [`segmentation`](index.html) module for details.
pub trait GraphemeSegmenter: Debug + Copy + Clone {
    /// Returns true if the boundary between the two given strings is a
    /// grapheme cluster boundary, and false otherwise.
    ///
    /// Ropey only calls this with valid and non-empty left and right
    /// strings.
    fn seam_is_break(left: &str, right: &str) -> bool;

    /// Returns whether the given byte index in the given string is a
    /// grapheme cluster boundary.
    ///
    /// Ropey only calls this on valid utf8 code point boundaries, and never
    /// on the start or end of the string.
    ///
    /// This function's default implementation simply calls
    /// `seam_is_break()`, passing it the text split at `char_idx`.  If you
    /// have a more efficient approach, go ahead and implement this manually.
    fn is_break(byte_idx: usize, text: &str) -> bool {
        Self::seam_is_break(&text[..byte_idx], &text[byte_idx..])
    }
}

/// Additional functions for GraphemeSegmenters.
pub(crate) trait SegmenterUtils: GraphemeSegmenter {
    /// Makes sure that special cases are handled correctly.
    #[inline]
    fn is_break_checked(byte_idx: usize, text: &str) -> bool {
        if !text.is_char_boundary(byte_idx) {
            false
        } else if byte_idx == 0 || byte_idx == text.len() {
            true
        } else {
            Self::is_break(byte_idx, text)
        }
    }

    /// Makes sure that special cases are handled correctly.
    #[inline]
    fn seam_is_break_checked(left: &str, right: &str) -> bool {
        debug_assert!(!left.is_empty() && !right.is_empty());
        Self::seam_is_break(left, right)
    }

    /// Returns the segment break before (but not including) the given byte
    /// boundary.
    ///
    /// This will return back the passed byte boundary if it is at the start
    /// of the string.
    #[inline]
    fn prev_break(byte_idx: usize, text: &str) -> usize {
        // Bounds check
        debug_assert!(byte_idx <= text.len());

        let mut boundary_idx = byte_idx;
        while boundary_idx > 0 {
            // Find previous codepoint boundary
            boundary_idx -= 1;
            while !text.is_char_boundary(boundary_idx) {
                boundary_idx -= 1;
            }

            // Check if it's a segment break
            if Self::is_break_checked(boundary_idx, text) {
                break;
            }
        }

        boundary_idx
    }

    /// Returns the segment break after (but not including) the given byte
    /// boundary.
    ///
    /// This will return back the passed byte boundary if it is at the end of
    /// the string.
    #[inline]
    fn next_break(byte_idx: usize, text: &str) -> usize {
        // Bounds check
        debug_assert!(byte_idx <= text.len());

        let mut boundary_idx = byte_idx;
        while boundary_idx < text.len() {
            // Find next codepoint boundary
            boundary_idx += 1;
            while !text.is_char_boundary(boundary_idx) {
                boundary_idx += 1;
            }

            // Check if it's a segment break
            if Self::is_break_checked(boundary_idx, text) {
                break;
            }
        }

        boundary_idx
    }

    /// Finds the segment break nearest to the given byte that is not the
    /// left or right edge of the text.
    ///
    /// There is only one circumstance where the left or right edge will be
    /// returned: if the entire text is a single unbroken segment, then the
    /// right edge of the text is returned.
    #[inline]
    fn nearest_internal_break(byte_idx: usize, text: &str) -> usize {
        // Bounds check
        debug_assert!(byte_idx <= text.len());

        // Find codepoint boundary
        let mut boundary_idx = byte_idx;
        while !text.is_char_boundary(boundary_idx) {
            boundary_idx -= 1;
        }

        // Find the two nearest segment boundaries
        let left = if Self::is_break_checked(boundary_idx, text) && boundary_idx != text.len() {
            boundary_idx
        } else {
            Self::prev_break(boundary_idx, text)
        };
        let right = Self::next_break(boundary_idx, text);

        // Otherwise, return the closest of left and right that isn't the
        // start or end of the string
        if left == 0 || (right != text.len() && (byte_idx - left) >= (right - byte_idx)) {
            return right;
        } else {
            return left;
        }
    }

    #[inline]
    fn find_good_split(byte_idx: usize, text: &str, bias_left: bool) -> usize {
        if Self::is_break_checked(byte_idx, text) {
            byte_idx
        } else {
            let prev = Self::prev_break(byte_idx, text);
            let next = Self::next_break(byte_idx, text);
            if bias_left {
                if prev > 0 {
                    prev
                } else {
                    next
                }
            } else {
                if next < text.len() {
                    next
                } else {
                    prev
                }
            }
        }
    }
}

impl<T: GraphemeSegmenter> SegmenterUtils for T {}

//===========================================================================

/// Internal-only segmenter that takes another segmenter and adds on top of
/// its segmentation that CRLF should never be broken.
/// Used by Ropey to ensure that CRLF is never broken regardless of the
/// segmenter passed.
#[derive(Debug, Copy, Clone)]
pub(crate) struct CRLFSegmenter<Seg: GraphemeSegmenter> {
    _seg: PhantomData<Seg>,
}

impl<S: GraphemeSegmenter> GraphemeSegmenter for CRLFSegmenter<S> {
    #[inline]
    fn is_break(byte_idx: usize, text: &str) -> bool {
        debug_assert!(byte_idx <= text.len());

        let bytes = text.as_bytes();
        let crlf_break = (bytes[byte_idx - 1] != 0x0D) | (bytes[byte_idx] != 0x0A);
        crlf_break && S::is_break(byte_idx, text)
    }

    #[inline]
    fn seam_is_break(left: &str, right: &str) -> bool {
        let crlf_break = (left.as_bytes()[left.len() - 1] != 0x0D) | (right.as_bytes()[0] != 0x0A);
        crlf_break && S::seam_is_break(left, right)
    }
}

//===========================================================================

/// Ropey's default grapheme segmenter.
///
/// Uses the extended grapheme cluster rules specified in
/// [Unicode Standard Annex #29](https://www.unicode.org/reports/tr29/)
#[derive(Debug, Copy, Clone)]
pub struct DefaultSegmenter {}

impl GraphemeSegmenter for DefaultSegmenter {
    #[inline]
    fn is_break(byte_idx: usize, text: &str) -> bool {
        GraphemeCursor::new(byte_idx, text.len(), true)
            .is_boundary(text, 0)
            .unwrap()
    }

    #[inline]
    fn seam_is_break(left: &str, right: &str) -> bool {
        let tot_len = left.len() + right.len();
        let mut gc = GraphemeCursor::new(left.len(), tot_len, true);

        gc.next_boundary(right, left.len()).unwrap();
        let prev = {
            match gc.prev_boundary(right, left.len()) {
                Ok(pos) => pos,
                Err(GraphemeIncomplete::PrevChunk) => gc.prev_boundary(left, 0).unwrap(),
                _ => unreachable!(),
            }
        };

        if let Some(a) = prev {
            if a == left.len() {
                return true;
            }
        }

        return false;
    }
}

/// Grapheme segmenter that ignores graphemes completely and treats each
/// code point as an individual segment.
#[derive(Debug, Copy, Clone)]
pub struct NullSegmenter {}

impl GraphemeSegmenter for NullSegmenter {
    #[inline]
    fn is_break(_byte_idx: usize, _text: &str) -> bool {
        true
    }

    #[inline]
    fn seam_is_break(_left: &str, _right: &str) -> bool {
        true
    }
}

//===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    type MSeg = CRLFSegmenter<DefaultSegmenter>;

    #[test]
    fn crlf_segmenter_01() {
        let text = "Hello world!\r\nHow's it going?";

        assert!(CRLFSegmenter::<NullSegmenter>::is_break_checked(0, ""));
        assert!(CRLFSegmenter::<NullSegmenter>::is_break_checked(0, text));
        assert!(CRLFSegmenter::<NullSegmenter>::is_break_checked(12, text));
        assert!(!CRLFSegmenter::<NullSegmenter>::is_break_checked(13, text));
        assert!(CRLFSegmenter::<NullSegmenter>::is_break_checked(14, text));
        assert!(CRLFSegmenter::<NullSegmenter>::is_break_checked(19, text));
    }

    #[test]
    fn crlf_segmenter_02() {
        let l = "Hello world!\r";
        let r = "\nHow's it going?";

        assert!(!CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(l, r));
        assert!(!CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(
            l,
            "\n"
        ));
        assert!(!CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(
            "\r",
            r
        ));
        assert!(!CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(
            "\r",
            "\n"
        ));
        assert!(CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(r, l));
        assert!(CRLFSegmenter::<NullSegmenter>::seam_is_break_checked(
            "\n",
            "\r"
        ));
    }

    #[test]
    fn nearest_internal_break_01() {
        let text = "Hello world!";
        assert_eq!(1, MSeg::nearest_internal_break(0, text));
        assert_eq!(6, MSeg::nearest_internal_break(6, text));
        assert_eq!(11, MSeg::nearest_internal_break(12, text));
    }

    #[test]
    fn nearest_internal_break_02() {
        let text = "Hello\r\n world!";
        assert_eq!(5, MSeg::nearest_internal_break(5, text));
        assert_eq!(7, MSeg::nearest_internal_break(6, text));
        assert_eq!(7, MSeg::nearest_internal_break(7, text));
    }

    #[test]
    fn nearest_internal_break_03() {
        let text = "\r\nHello world!\r\n";
        assert_eq!(2, MSeg::nearest_internal_break(0, text));
        assert_eq!(2, MSeg::nearest_internal_break(1, text));
        assert_eq!(2, MSeg::nearest_internal_break(2, text));
        assert_eq!(14, MSeg::nearest_internal_break(14, text));
        assert_eq!(14, MSeg::nearest_internal_break(15, text));
        assert_eq!(14, MSeg::nearest_internal_break(16, text));
    }

    #[test]
    fn nearest_internal_break_04() {
        let text = "\r\n";
        assert_eq!(2, MSeg::nearest_internal_break(0, text));
        assert_eq!(2, MSeg::nearest_internal_break(1, text));
        assert_eq!(2, MSeg::nearest_internal_break(2, text));
    }

    #[test]
    fn is_break_01() {
        let text = "\n\r\n\r\n\r\n\r\n\r\n\r";

        assert!(MSeg::is_break_checked(0, text));
        assert!(MSeg::is_break_checked(12, text));
        assert!(MSeg::is_break_checked(3, text));
        assert!(!MSeg::is_break_checked(6, text));
    }

    #[test]
    fn seam_is_break_01() {
        let text1 = "\r\n\r\n\r\n";
        let text2 = "\r\n\r\n";

        assert!(MSeg::seam_is_break(text1, text2));
    }

    #[test]
    fn seam_is_break_02() {
        let text1 = "\r\n\r\n\r";
        let text2 = "\n\r\n\r\n";

        assert!(!MSeg::seam_is_break(text1, text2));
    }
}