From 4abdafcd158ab15d9d2ae18553067578ae559d33 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 13 Jan 2022 19:49:52 +0100 Subject: Faster byte/utf-16 offset conversions --- src/util/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/util') diff --git a/src/util/mod.rs b/src/util/mod.rs index 694c90c8..61e7131d 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -13,6 +13,19 @@ use std::ops::Range; use std::path::{Component, Path, PathBuf}; use std::rc::Rc; +/// Additional methods for strings. +pub trait StrExt { + /// The number of code units this string would use if it was encoded in + /// UTF16. This runs in linear time. + fn len_utf16(&self) -> usize; +} + +impl StrExt for str { + fn len_utf16(&self) -> usize { + self.chars().map(char::len_utf16).sum() + } +} + /// Additional methods for options. pub trait OptionExt { /// Sets `other` as the value if `self` is `None` or if it contains a value -- cgit v1.2.3