Skip to content

Commit 3a413aa

Browse files
committed
core: add str::as_bytes function
This pattern is used in a couple places, so it'd be nice to abstract it away.
1 parent 526e73d commit 3a413aa

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/libcore/str.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export
9595
char_at,
9696
substr_all,
9797
escape_char,
98+
as_bytes,
9899
as_buf,
99100
//buf,
100101
sbuf,
@@ -390,10 +391,7 @@ Converts a string to a vector of bytes. The result vector is not
390391
null-terminated.
391392
*/
392393
fn bytes(s: str) -> [u8] unsafe {
393-
let v = ::unsafe::reinterpret_cast(s);
394-
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
395-
::unsafe::leak(v);
396-
ret vcopy;
394+
as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) }
397395
}
398396

399397
/*
@@ -1026,12 +1024,12 @@ Returns the length in bytes of a string
10261024
FIXME: rename to 'len_bytes'
10271025
*/
10281026
pure fn byte_len(s: str) -> uint unsafe {
1029-
let v: [u8] = ::unsafe::reinterpret_cast(s);
1030-
let vlen = vec::len(v);
1031-
::unsafe::leak(v);
1032-
// There should always be a null terminator
1033-
assert (vlen > 0u);
1034-
ret vlen - 1u;
1027+
as_bytes(s) { |v|
1028+
let vlen = vec::len(v);
1029+
// There should always be a null terminator
1030+
assert (vlen > 0u);
1031+
vlen - 1u
1032+
}
10351033
}
10361034

10371035
/*
@@ -1299,23 +1297,38 @@ const max_five_b: uint = 67108864u;
12991297
const tag_six_b: uint = 252u;
13001298

13011299
/*
1302-
Function: as_buf
1300+
Function: as_bytes
13031301
13041302
Work with the byte buffer of a string. Allows for unsafe manipulation
13051303
of strings, which is useful for native interop.
13061304
13071305
Example:
13081306
1309-
> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
1307+
> let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
13101308
13111309
*/
1312-
fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
1310+
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
13131311
let v: [u8] = ::unsafe::reinterpret_cast(s);
1314-
let r = vec::as_buf(v, f);
1312+
let r = f(v);
13151313
::unsafe::leak(v);
13161314
r
13171315
}
13181316

1317+
/*
1318+
Function: as_buf
1319+
1320+
Work with the byte buffer of a string. Allows for unsafe manipulation
1321+
of strings, which is useful for native interop.
1322+
1323+
Example:
1324+
1325+
> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
1326+
1327+
*/
1328+
fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
1329+
as_bytes(s) { |v| vec::as_buf(v, f) }
1330+
}
1331+
13191332
/*
13201333
Type: sbuf
13211334
@@ -1373,13 +1386,11 @@ mod unsafe {
13731386
assert (begin <= end);
13741387
assert (end <= byte_len(s));
13751388

1376-
let v: [u8] = ::unsafe::reinterpret_cast(s);
1377-
let v2 = vec::slice(v, begin, end);
1389+
let v = as_bytes(s) { |v| vec::slice(v, begin, end) };
1390+
v += [0u8];
1391+
let s: str = ::unsafe::reinterpret_cast(v);
13781392
::unsafe::leak(v);
1379-
v2 += [0u8];
1380-
let s2: str = ::unsafe::reinterpret_cast(v2);
1381-
::unsafe::leak(v2);
1382-
ret s2;
1393+
ret s;
13831394
}
13841395

13851396
/*

0 commit comments

Comments
 (0)