Skip to content

Commit 45aa678

Browse files
committed
---
yaml --- r: 14119 b: refs/heads/try c: 3a413aa h: refs/heads/master i: 14117: 320f294 14115: 1b2b059 14111: 1041880 v: v3
1 parent 4a41d40 commit 45aa678

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 526e73d7f882bf9a88fe957661cc2e09291cef5b
5+
refs/heads/try: 3a413aabd4ccb0c0f8d6b3e5f380ac47164a1ee3
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/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)