1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::ffi::CStr;
use std::ops::Range;
use std::slice;
use widestring::U32String;
#[derive(Debug, Clone)]
#[repr(C)]
pub enum StringBoxOrigin {
Byte(Vec<u8>),
String,
Wide(U32String),
}
#[repr(u8)]
#[derive(Debug, Clone)]
pub enum StringBoxOriginType {
Byte,
UTF8,
Wide,
}
impl From<StringBoxOrigin> for StringBoxOriginType {
fn from(origin: StringBoxOrigin) -> StringBoxOriginType {
match origin {
StringBoxOrigin::Byte(_) => StringBoxOriginType::Byte,
StringBoxOrigin::String => StringBoxOriginType::UTF8,
StringBoxOrigin::Wide(_) => StringBoxOriginType::Wide,
}
}
}
#[derive(Debug, Clone)]
pub struct StringBox {
origin: StringBoxOrigin,
string: String,
}
impl StringBox {
pub fn new() -> Self {
Self::from_string(String::new())
}
pub fn from_string(string: String) -> Self {
Self {
origin: StringBoxOrigin::String,
string,
}
}
pub unsafe fn from_wide_string_data(data: *const u32, length: usize) -> Self {
let wide_string = slice::from_raw_parts(data, length).to_vec();
Self::from_wide_string(wide_string)
}
pub fn from_wide_string(data: Vec<u32>) -> Self {
let wide_string = U32String::from_vec(data);
let string = wide_string.to_string_lossy();
Self {
origin: StringBoxOrigin::Wide(wide_string),
string,
}
}
pub unsafe fn from_byte_string_data(data: *const u8, length: usize) -> Self {
let byte_string = slice::from_raw_parts(data, length).to_vec();
Self::from_byte_string(byte_string)
}
pub fn from_byte_string(data: Vec<u8>) -> Self {
let string = data.iter().map(|&c| c as char).collect::<String>();
Self {
origin: StringBoxOrigin::Byte(data),
string,
}
}
pub unsafe fn from_utf8_string_data(data: *const u8, length: usize) -> Self {
Self::from_utf8_string(slice::from_raw_parts(data, length + 1))
}
pub fn from_utf8_string(data: &[u8]) -> Self {
let string = unsafe {
CStr::from_bytes_with_nul_unchecked(data)
.to_string_lossy()
.into_owned()
};
Self {
origin: StringBoxOrigin::String,
string,
}
}
pub fn set_string(&mut self, string: String) {
self.origin = StringBoxOrigin::String;
self.string = string;
}
pub fn len(&self) -> usize {
self.string.len()
}
pub fn char_count(&self) -> usize {
self.string.chars().count()
}
pub fn to_string(&self) -> String {
self.string.clone()
}
pub fn as_str(&self) -> &str {
self.string.as_str()
}
pub fn as_bytes(&self) -> &[u8] {
self.string.as_bytes()
}
pub fn as_ptr(&self) -> *const u8 {
self.string.as_ptr()
}
pub fn char_index_to_byte_range(&self, index: usize) -> Range<usize> {
let mut current_char_index = 0 as usize;
let mut previous_byte_offset = 0 as usize;
for (current_byte_offset, _) in self.string.char_indices() {
if current_char_index == (index + 1) {
return previous_byte_offset..current_byte_offset;
}
current_char_index = current_char_index + 1;
previous_byte_offset = current_byte_offset;
}
previous_byte_offset..self.len()
}
pub fn char_index_to_utf16_range(&self, index: usize) -> Range<usize> {
let mut current_char_index = 0 as usize;
let mut previous_byte_offset = 0 as usize;
let mut previous_utf16_offset = 0 as usize;
for (current_byte_offset, _) in self.string.char_indices() {
let delta = ((current_byte_offset - previous_byte_offset) + 1) / 2;
if current_char_index == (index + 1) {
return previous_utf16_offset..(previous_utf16_offset + delta);
}
current_char_index = current_char_index + 1;
previous_byte_offset = current_byte_offset;
previous_utf16_offset = previous_utf16_offset + delta;
}
let delta = ((self.len() - previous_byte_offset) + 1) / 2;
previous_utf16_offset..(previous_utf16_offset + delta)
}
pub fn utf16_position_to_char_index(&self, index: usize) -> usize {
let mut current_char_index = 0 as usize;
let mut previous_byte_offset = 0 as usize;
let mut previous_utf16_offset = 0 as usize;
for (current_byte_offset, _) in self.string.char_indices() {
let delta = ((current_byte_offset - previous_byte_offset) + 1) / 2;
let current_utf16_offset = previous_utf16_offset + delta;
if current_utf16_offset >= index {
return current_char_index;
}
current_char_index = current_char_index + 1;
previous_byte_offset = current_byte_offset;
previous_utf16_offset = current_utf16_offset;
}
current_char_index
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn test_from_wide_string() {
let wide_string = vec![1087u32, 1088, 1080, 1074, 1077, 1090];
let string = StringBox::from_wide_string(wide_string);
assert_eq!(string.to_string(), String::from("привет"));
}
#[test]
pub fn test_from_byte_string() {
let byte_string = vec![104u8, 101, 108, 108, 111];
let string = StringBox::from_byte_string(byte_string);
assert_eq!(string.to_string(), String::from("hello"));
}
#[test]
pub fn test_from_utf8_string() {
let utf8_string = vec![104u8, 101, 108, 108, 111, 0];
let string = StringBox::from_utf8_string(utf8_string.as_slice());
assert_eq!(string.to_string(), String::from("hello"));
}
#[test]
pub fn sparkle() {
let sparkle = String::from("💖");
let string_box = StringBox::from_string(sparkle.clone());
assert_eq!(sparkle.len(), 4);
assert_eq!(string_box.len(), 4);
assert_eq!(string_box.char_count(), 1);
for char in sparkle.char_indices() {
println!("{:?}", char);
}
println!("{:?}", sparkle.bytes());
}
}