summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
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