|
95 | 95 | char_at,
|
96 | 96 | substr_all,
|
97 | 97 | escape_char,
|
| 98 | + as_bytes, |
98 | 99 | as_buf,
|
99 | 100 | //buf,
|
100 | 101 | sbuf,
|
@@ -390,10 +391,7 @@ Converts a string to a vector of bytes. The result vector is not
|
390 | 391 | null-terminated.
|
391 | 392 | */
|
392 | 393 | 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) } |
397 | 395 | }
|
398 | 396 |
|
399 | 397 | /*
|
@@ -1026,12 +1024,12 @@ Returns the length in bytes of a string
|
1026 | 1024 | FIXME: rename to 'len_bytes'
|
1027 | 1025 | */
|
1028 | 1026 | 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 | + } |
1035 | 1033 | }
|
1036 | 1034 |
|
1037 | 1035 | /*
|
@@ -1299,23 +1297,38 @@ const max_five_b: uint = 67108864u;
|
1299 | 1297 | const tag_six_b: uint = 252u;
|
1300 | 1298 |
|
1301 | 1299 | /*
|
1302 |
| -Function: as_buf |
| 1300 | +Function: as_bytes |
1303 | 1301 |
|
1304 | 1302 | Work with the byte buffer of a string. Allows for unsafe manipulation
|
1305 | 1303 | of strings, which is useful for native interop.
|
1306 | 1304 |
|
1307 | 1305 | Example:
|
1308 | 1306 |
|
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) }; |
1310 | 1308 |
|
1311 | 1309 | */
|
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 { |
1313 | 1311 | let v: [u8] = ::unsafe::reinterpret_cast(s);
|
1314 |
| - let r = vec::as_buf(v, f); |
| 1312 | + let r = f(v); |
1315 | 1313 | ::unsafe::leak(v);
|
1316 | 1314 | r
|
1317 | 1315 | }
|
1318 | 1316 |
|
| 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 | + |
1319 | 1332 | /*
|
1320 | 1333 | Type: sbuf
|
1321 | 1334 |
|
@@ -1373,13 +1386,11 @@ mod unsafe {
|
1373 | 1386 | assert (begin <= end);
|
1374 | 1387 | assert (end <= byte_len(s));
|
1375 | 1388 |
|
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); |
1378 | 1392 | ::unsafe::leak(v);
|
1379 |
| - v2 += [0u8]; |
1380 |
| - let s2: str = ::unsafe::reinterpret_cast(v2); |
1381 |
| - ::unsafe::leak(v2); |
1382 |
| - ret s2; |
| 1393 | + ret s; |
1383 | 1394 | }
|
1384 | 1395 |
|
1385 | 1396 | /*
|
|
0 commit comments