-
Notifications
You must be signed in to change notification settings - Fork 44
/
string.rs
221 lines (186 loc) · 5.08 KB
/
string.rs
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
use std::borrow::Cow;
use std::ffi::OsStr;
#[cfg(target_family = "unix")]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_family = "windows")]
use std::os::windows::ffi::OsStrExt;
use std::path::PathBuf;
use std::string::{self, String as StdString};
use std::{fmt, slice, str};
use libc::{c_char, size_t};
/// Neovim's `String`s:
/// - are null-terminated;
/// - *can* contain null bytes (confirmed by bfredl on matrix);
/// - they store a `size` field just like Rust strings, which *doesn't*
/// include the last `\0`;
/// - unlike Rust strings, they are *not* guaranteed to always contain valid
/// UTF-8 byte sequences;
///
/// See https://github.com/neovim/neovim/blob/master/src/nvim/api/private/helpers.c#L478
/// for how a C string gets converted into a Neovim string.
///
/// https://github.com/neovim/neovim/blob/master/src/nvim/api/private/defs.h#L77
#[derive(Eq)]
#[repr(C)]
pub struct String {
pub data: *mut c_char,
pub size: size_t,
}
impl String {
/// TODO: docs
#[inline]
pub fn from_bytes(mut vec: Vec<u8>) -> Self {
vec.reserve_exact(1);
vec.push(0);
let size = vec.len() - 1;
let data = vec.leak().as_mut_ptr() as *mut c_char;
Self { data, size }
}
/// TODO: docs
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
/// TODO: docs
#[inline]
pub const fn len(&self) -> usize {
self.size
}
/// TODO: docs
#[inline]
pub fn as_bytes(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.data as *const u8, self.size) }
}
/// TODO: docs
#[inline]
pub fn as_str(&self) -> Result<&str, str::Utf8Error> {
str::from_utf8(self.as_bytes())
}
/// TODO: docs
#[inline]
pub fn to_string_lossy(&self) -> Cow<'_, str> {
StdString::from_utf8_lossy(self.as_bytes())
}
/// Converts an `NvimString` into a byte vector, consuming the string.
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
unsafe {
Vec::from_raw_parts(self.data.cast::<u8>(), self.size, self.size)
}
}
/// TODO: docs
#[inline]
pub fn into_string(self) -> Result<StdString, string::FromUtf8Error> {
StdString::from_utf8(self.into_bytes())
}
}
impl fmt::Debug for String {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("NvimString")
.field("data", &self.to_string_lossy())
.field("size", &self.size)
.finish()
}
}
impl Clone for String {
fn clone(&self) -> Self {
Self::from_bytes(self.as_bytes().to_owned())
}
}
impl From<StdString> for String {
#[inline]
fn from(string: StdString) -> Self {
Self::from_bytes(string.into_bytes())
}
}
impl<'a> From<&'a str> for String {
#[inline]
fn from(str: &'a str) -> Self {
Self::from_bytes(str.as_bytes().to_owned())
}
}
impl From<char> for String {
#[inline]
fn from(ch: char) -> Self {
Self { data: *(Box::new(ch as c_char)) as *mut c_char, size: 1 }
}
}
#[cfg(not(windows))]
impl From<String> for PathBuf {
#[inline]
fn from(nstr: String) -> Self {
OsStr::from_bytes(nstr.as_bytes()).to_owned().into()
}
}
#[cfg(windows)]
impl From<String> for PathBuf {
#[inline]
fn from(nstr: String) -> Self {
StdString::from_utf8_lossy(nstr.as_bytes()).into_owned().into()
}
}
impl PartialEq<Self> for String {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<str> for String {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl<'a> PartialEq<&'a str> for String {
#[inline]
fn eq(&self, other: &&'a str) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<StdString> for String {
#[inline]
fn eq(&self, other: &StdString) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl TryFrom<String> for StdString {
type Error = std::string::FromUtf8Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
StdString::from_utf8(s.into_bytes())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn partial_eq() {
let lhs = String::from("foo bar baz");
let rhs = String::from("foo bar baz");
assert_eq!(lhs, rhs);
let lhs = String::from("foo bar baz");
let rhs = String::from("bar foo baz");
assert_ne!(lhs, rhs);
let lhs = String::from("€");
let rhs = "€";
assert_eq!(lhs, rhs);
}
#[test]
fn clone() {
let lhs = String::from("abc");
let rhs = lhs.clone();
assert_eq!(lhs, rhs);
}
#[test]
fn from_string() {
let foo = StdString::from("foo bar baz");
let lhs = String::from(foo.as_ref());
let rhs = String::from(foo);
assert_eq!(lhs, rhs);
}
#[test]
fn to_bytes() {
let s = String::from("hello");
let bytes = s.into_bytes();
assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);
}
}