ncursesw/wide/
string.rs

1/*
2    src/wide/string.rs
3
4    Copyright (c) 2019-2022 Stephen Whittle  All rights reserved.
5
6    Permission is hereby granted, free of charge, to any person obtaining a copy
7    of this software and associated documentation files (the "Software"),
8    to deal in the Software without restriction, including without limitation
9    the rights to use, copy, modify, merge, publish, distribute, sublicense,
10    and/or sell copies of the Software, and to permit persons to whom
11    the Software is furnished to do so, subject to the following conditions:
12    The above copyright notice and this permission notice shall be included
13    in all copies or substantial portions of the Software.
14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20    IN THE SOFTWARE.
21*/
22
23#![allow(clippy::should_implement_trait)]
24#![allow(clippy::from_over_into)]
25
26use crate::{WideChar, RawWithNul, shims::ncurses::wchar_t};
27
28/// Wide character string (UTF-8).
29#[derive(Clone, Debug, PartialEq, Eq, Hash)]
30pub struct WideString {
31    inner: Vec<wchar_t>
32}
33
34impl WideString {
35    pub fn new() -> Self {
36        Self { inner: vec!() }
37    }
38
39    #[deprecated(since = "0.6.4", note = "use `From` trait instead!")]
40    pub fn from_str<S: Into<String>>(str: S) -> Self {
41        Self { inner: str.into().chars().map(|c| u32::from(c) as wchar_t).collect() }
42    }
43
44    pub fn with_capacity(capacity: usize) -> Self {
45        Self { inner: Vec::with_capacity(capacity) }
46    }
47
48    //pub unsafe fn from_raw_parts(buf: *mut ChtypeChar, length: usize, capacity: usize) -> Self { }
49    //pub unsafe fn from_chtype_unchecked<B>(bytes: B) -> Self where B: Into<Vec<chtype>> { }
50    //pub fn from_ascii<B>(bytes: B) -> Result<ChtypeString, FromAsciiError<B>> where B: Into<Vec<u8>> + AsRef<[u8]> { }
51
52    pub fn push_str(&mut self, rhs: &Self) {
53        self.inner.append(&mut Self::into(rhs.to_owned()));
54    }
55
56    pub fn capacity(&self) -> usize {
57        self.inner.capacity()
58    }
59
60    pub fn reserve(&mut self, additional: usize) {
61        self.inner.reserve(additional)
62    }
63
64    pub fn reserve_exact(&mut self, additional: usize) {
65        self.inner.reserve_exact(additional)
66    }
67
68    pub fn shrink_to_fit(&mut self) {
69        self.inner.shrink_to_fit()
70    }
71
72    pub fn push(&mut self, rhs: WideChar) {
73        self.inner.push(WideChar::into(rhs.to_owned()));
74    }
75
76    pub fn truncate(&mut self, new_len: usize) {
77        self.inner.truncate(new_len)
78    }
79
80    pub fn pop(&mut self) -> Option<WideChar> {
81        self.inner.pop().map(WideChar::from)
82    }
83
84    pub fn remove(&mut self, idx: usize) -> WideChar {
85        WideChar::from(self.inner.remove(idx))
86    }
87
88    pub fn insert(&mut self, idx: usize, ch: WideChar) {
89        self.inner.insert(idx, WideChar::into(ch))
90    }
91
92    pub fn len(&self) -> usize {
93        self.inner.len()
94    }
95
96    pub fn is_empty(&self) -> bool {
97        self.len() == 0
98    }
99
100    pub fn clear(&mut self) {
101        self.inner.clear()
102    }
103}
104
105impl Default for WideString {
106    fn default() -> Self {
107        Self::new()
108    }
109}
110
111impl <'a>From<&'a Vec<WideChar>> for WideString {
112    fn from(vwch: &'a Vec<WideChar>) -> Self {
113        Self { inner: vwch.iter().map(|wch| WideChar::into(*wch)).collect() }
114    }
115}
116
117impl <'a>From<&'a [wchar_t]> for WideString {
118    fn from(slice: &'a [wchar_t]) -> Self {
119        Self { inner : slice.to_vec() }
120    }
121}
122
123impl Into<Vec<wchar_t>> for WideString {
124    fn into(self) -> Vec<wchar_t> {
125        self.inner
126    }
127}
128
129impl From<&str> for WideString {
130    fn from(value: &str) -> Self {
131        Self { inner: value.chars().map(|chr| u32::from(chr) as wchar_t).collect() }
132    }
133}
134
135impl From<String> for WideString {
136    fn from(value: String) -> Self {
137        Self::from(value.as_str())
138    }
139}
140
141impl RawWithNul<Vec<wchar_t>> for WideString {
142    fn raw_with_nul(self) -> Vec<wchar_t> {
143        let mut raw: Vec<wchar_t> = Self::into(self);
144
145        raw.push(0x00);
146
147        raw
148    }
149}
150
151impl AsRef<WideString> for WideString {
152    fn as_ref(&self) -> &Self {
153        self
154    }
155}
156
157impl AsMut<WideString> for WideString {
158    fn as_mut(&mut self) -> &mut Self {
159        self
160    }
161}