libobs_window_helper\util/
string_conv.rs

1pub trait ToUtf8String {
2    fn to_utf8(&self) -> String;
3}
4
5pub trait StrLen<T> {
6    fn strlen(&self) -> usize;
7    fn strslice(&self) -> &[T];
8}
9
10impl<T: Default + PartialEq> StrLen<T> for [T] {
11    fn strlen(&self) -> usize {
12        let zero = &Default::default();
13        match self.iter().position(|x| x == zero) {
14            None => self.len(),
15            Some(x) => x,
16        }
17    }
18
19    fn strslice(&self) -> &[T] {
20        &self[0..self.strlen()]
21    }
22}
23
24impl<T: AsRef<[u16]>> ToUtf8String for T {
25    fn to_utf8(&self) -> String {
26        String::from_utf16_lossy(self.as_ref().strslice())
27            .trim_end_matches("\0")
28            .to_string()
29    }
30}