Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'lib/line-index/src/lib.rs')
| -rw-r--r-- | lib/line-index/src/lib.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 61f907f060..40815bdcf1 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -20,6 +20,7 @@ pub struct LineCol { /// A kind of wide character encoding. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] pub enum WideEncoding { /// UTF-16. Utf16, @@ -27,6 +28,16 @@ pub enum WideEncoding { Utf32, } +impl WideEncoding { + /// Returns the number of units it takes to encode `text` in this encoding. + pub fn measure(&self, text: &str) -> usize { + match self { + WideEncoding::Utf16 => text.encode_utf16().count(), + WideEncoding::Utf32 => text.chars().count(), + } + } +} + /// Line/Column information in legacy encodings. // // Deliberately not a generic type and different from `LineCol`. |