@@ -11,14 +11,21 @@ export
11
11
// Creating a string
12
12
from_bytes,
13
13
from_byte,
14
- push_char,
15
14
from_char,
16
15
from_chars,
17
16
from_buf,
18
17
from_buf_len,
18
+ from_c_str,
19
+ from_c_str_len,
20
+ push_char,
19
21
concat,
20
22
connect,
21
23
24
+ // Reinterpretation
25
+ as_bytes,
26
+ as_buf,
27
+ as_c_str,
28
+
22
29
// Adding things to and removing things from a string
23
30
push_char,
24
31
pop_char,
88
95
char_range_at,
89
96
is_char_boundary,
90
97
char_at,
91
- as_bytes,
92
- as_buf,
93
98
reserve,
94
99
95
100
unsafe ;
@@ -192,6 +197,11 @@ fn from_buf(buf: *u8) -> str unsafe {
192
197
ret from_buf_len ( buf, i) ;
193
198
}
194
199
200
+ #[ doc = "Create a Rust string from a null-terminated C string" ]
201
+ fn from_c_str ( c_str : * libc:: c_char ) -> str unsafe {
202
+ from_buf ( :: unsafe:: reinterpret_cast ( c_str) )
203
+ }
204
+
195
205
#[ doc = "Create a Rust string from a *u8 buffer of the given length" ]
196
206
fn from_buf_len ( buf : * u8 , len : uint ) -> str unsafe {
197
207
let mut v: [ u8 ] = [ ] ;
@@ -206,6 +216,11 @@ fn from_buf_len(buf: *u8, len: uint) -> str unsafe {
206
216
ret s;
207
217
}
208
218
219
+ #[ doc = "Create a Rust string from a `*c_char` buffer of the given length" ]
220
+ fn from_c_str_len ( c_str : * libc:: c_char , len : uint ) -> str unsafe {
221
+ from_buf_len ( :: unsafe:: reinterpret_cast ( c_str) , len)
222
+ }
223
+
209
224
#[ doc = "Concatenate a vector of strings" ]
210
225
fn concat ( v : [ str ] ) -> str {
211
226
let mut s: str = "" ;
@@ -1240,15 +1255,25 @@ Work with the byte buffer of a string.
1240
1255
1241
1256
Allows for unsafe manipulation of strings, which is useful for native
1242
1257
interop.
1258
+ " ]
1259
+ fn as_buf < T > ( s : str , f : fn ( * u8 ) -> T ) -> T unsafe {
1260
+ as_bytes ( s) { |v| vec:: as_buf ( v, f) }
1261
+ }
1262
+
1263
+ #[ doc = "
1264
+ Work with the byte buffer of a string as a null-terminated C string.
1265
+
1266
+ Allows for unsafe manipulation of strings, which is useful for native
1267
+ interop, without copying the original string.
1243
1268
1244
1269
# Example
1245
1270
1246
1271
```
1247
1272
let s = str::as_buf(\" PATH\" , { |path_buf| libc::getenv(path_buf) });
1248
1273
```
1249
1274
" ]
1250
- fn as_buf < T > ( s : str , f : fn ( * u8 ) -> T ) -> T unsafe {
1251
- as_bytes ( s) { |v| vec :: as_buf ( v , f ) }
1275
+ fn as_c_str < T > ( s : str , f : fn ( * libc :: c_char ) -> T ) -> T unsafe {
1276
+ as_buf ( s) { |buf| f ( buf as * libc :: c_char ) }
1252
1277
}
1253
1278
1254
1279
#[ doc = "Allocate more memory for a string, up to `nn` + 1 bytes" ]
0 commit comments