summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-01-13 19:49:52 +0100
committerLaurenz <laurmaedje@gmail.com>2022-01-13 19:55:32 +0100
commit4abdafcd158ab15d9d2ae18553067578ae559d33 (patch)
treeb1081ad25b7984bd8504008cd971bc2ce20224b3 /src/util
parentb3062ee8804580f65d5232846f1ccd199b2ff1c7 (diff)
Faster byte/utf-16 offset conversions
Diffstat (limited to 'src/util')
-rw-r--r--src/util/mod.rs13
1 files changed, 13 insertions, 0 deletions
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<T> {
/// Sets `other` as the value if `self` is `None` or if it contains a value