Skip to content

Commit 944f5a6

Browse files
committed
(core::str) move push_byte, push_bytes, pop_byte, and shift_byte into str::unsafe
1 parent 5fb0906 commit 944f5a6

File tree

7 files changed

+70
-85
lines changed

7 files changed

+70
-85
lines changed

src/comp/syntax/parse/lexer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ fn scan_exponent(rdr: reader) -> option<str> {
149149
let c = rdr.curr;
150150
let rslt = "";
151151
if c == 'e' || c == 'E' {
152-
str::push_byte(rslt, c as u8);
152+
str::push_char(rslt, c);
153153
rdr.bump();
154154
c = rdr.curr;
155155
if c == '-' || c == '+' {
156-
str::push_byte(rslt, c as u8);
156+
str::push_char(rslt, c);
157157
rdr.bump();
158158
}
159159
let exponent = scan_digits(rdr, 10u);
@@ -170,7 +170,7 @@ fn scan_digits(rdr: reader, radix: uint) -> str {
170170
if c == '_' { rdr.bump(); cont; }
171171
alt char::maybe_digit(c) {
172172
some(d) if (d as uint) < radix {
173-
str::push_byte(rslt, c as u8);
173+
str::push_char(rslt, c);
174174
rdr.bump();
175175
}
176176
_ { break; }
@@ -472,11 +472,11 @@ fn next_token_inner(rdr: reader) -> token::token {
472472
let escaped = rdr.curr;
473473
rdr.bump();
474474
alt escaped {
475-
'n' { str::push_byte(accum_str, '\n' as u8); }
476-
'r' { str::push_byte(accum_str, '\r' as u8); }
477-
't' { str::push_byte(accum_str, '\t' as u8); }
478-
'\\' { str::push_byte(accum_str, '\\' as u8); }
479-
'"' { str::push_byte(accum_str, '"' as u8); }
475+
'n' { str::push_char(accum_str, '\n'); }
476+
'r' { str::push_char(accum_str, '\r'); }
477+
't' { str::push_char(accum_str, '\t'); }
478+
'\\' { str::push_char(accum_str, '\\'); }
479+
'"' { str::push_char(accum_str, '"'); }
480480
'\n' { consume_whitespace(rdr); }
481481
'x' {
482482
str::push_char(accum_str, scan_numeric_escape(rdr, 2u));

src/comp/syntax/parse/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn to_str(r: reader, t: token) -> str {
139139
// FIXME: escape.
140140
let tmp = "'";
141141
str::push_char(tmp, c as char);
142-
str::push_byte(tmp, '\'' as u8);
142+
str::push_char(tmp, '\'');
143143
ret tmp;
144144
}
145145
LIT_INT(i, t) {

src/libcore/str.rs

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export
2626
pop_char,
2727
shift_char,
2828
unshift_char,
29-
push_byte,
30-
//push_bytes,
31-
pop_byte,
32-
shift_byte,
3329
trim_left,
3430
trim_right,
3531
trim,
@@ -137,7 +133,7 @@ fn from_byte(uu: u8) -> str {
137133
from_bytes([uu])
138134
}
139135

140-
fn push_utf8_bytes(&s: str, ch: char) {
136+
fn push_utf8_bytes(&s: str, ch: char) unsafe {
141137
let code = ch as uint;
142138
let bytes =
143139
if code < max_one_b {
@@ -168,7 +164,7 @@ fn push_utf8_bytes(&s: str, ch: char) {
168164
(code >> 6u & 63u | tag_cont) as u8,
169165
(code & 63u | tag_cont) as u8]
170166
};
171-
push_bytes(s, bytes);
167+
unsafe::push_bytes(s, bytes);
172168
}
173169

174170
/*
@@ -303,58 +299,6 @@ Prepend a char to a string
303299
*/
304300
fn unshift_char(&s: str, ch: char) { s = from_char(ch) + s; }
305301

306-
/*
307-
Function: push_byte
308-
309-
Appends a byte to a string.
310-
311-
This function is not unicode-safe.
312-
*/
313-
fn push_byte(&s: str, b: u8) { rustrt::rust_str_push(s, b); }
314-
315-
/*
316-
Function: push_bytes
317-
318-
Appends a vector of bytes to a string.
319-
320-
This function is not unicode-safe.
321-
*/
322-
fn push_bytes(&s: str, bytes: [u8]) {
323-
for byte in bytes { rustrt::rust_str_push(s, byte); }
324-
}
325-
326-
/*
327-
Function: pop_byte
328-
329-
Removes the last byte from a string and returns it.
330-
331-
This function is not unicode-safe.
332-
FIXME: move to unsafe?
333-
*/
334-
fn pop_byte(&s: str) -> u8 unsafe {
335-
let len = byte_len(s);
336-
assert (len > 0u);
337-
let b = s[len - 1u];
338-
s = unsafe::slice_bytes(s, 0u, len - 1u);
339-
ret b;
340-
}
341-
342-
/*
343-
Function: shift_byte
344-
345-
Removes the first byte from a string and returns it.
346-
347-
This function is not unicode-safe.
348-
FIXME: move to unsafe?
349-
*/
350-
fn shift_byte(&s: str) -> u8 unsafe {
351-
let len = byte_len(s);
352-
assert (len > 0u);
353-
let b = s[0];
354-
s = unsafe::slice_bytes(s, 1u, len);
355-
ret b;
356-
}
357-
358302
/*
359303
Function: trim_left
360304
@@ -592,8 +536,6 @@ fn split(ss: str, sepfn: fn(cc: char)->bool) -> [str] {
592536
Function: split_char
593537
594538
Splits a string into a vector of the substrings separated by a given character
595-
596-
FIXME: also add splitn_char
597539
*/
598540
fn split_char(ss: str, cc: char) -> [str] {
599541
split(ss, {|kk| kk == cc})
@@ -1409,7 +1351,11 @@ mod unsafe {
14091351
from_bytes,
14101352
from_byte,
14111353
slice_bytes,
1412-
slice_bytes_safe_range;
1354+
slice_bytes_safe_range,
1355+
push_byte,
1356+
push_bytes, // note: wasn't exported
1357+
pop_byte,
1358+
shift_byte;
14131359

14141360
// Function: unsafe::from_bytes
14151361
//
@@ -1462,6 +1408,43 @@ mod unsafe {
14621408
assert (end <= byte_len(s));
14631409
ret slice_bytes(s, begin, end);
14641410
}
1411+
1412+
// Function: push_byte
1413+
//
1414+
// Appends a byte to a string. (Not UTF-8 safe).
1415+
unsafe fn push_byte(&s: str, b: u8) {
1416+
rustrt::rust_str_push(s, b);
1417+
}
1418+
1419+
// Function: push_bytes
1420+
//
1421+
// Appends a vector of bytes to a string. (Not UTF-8 safe).
1422+
unsafe fn push_bytes(&s: str, bytes: [u8]) {
1423+
for byte in bytes { rustrt::rust_str_push(s, byte); }
1424+
}
1425+
1426+
// Function: pop_byte
1427+
//
1428+
// Removes the last byte from a string and returns it. (Not UTF-8 safe).
1429+
unsafe fn pop_byte(&s: str) -> u8 unsafe {
1430+
let len = byte_len(s);
1431+
assert (len > 0u);
1432+
let b = s[len - 1u];
1433+
s = unsafe::slice_bytes(s, 0u, len - 1u);
1434+
ret b;
1435+
}
1436+
1437+
// Function: shift_byte
1438+
//
1439+
// Removes the first byte from a string and returns it. (Not UTF-8 safe).
1440+
unsafe fn shift_byte(&s: str) -> u8 unsafe {
1441+
let len = byte_len(s);
1442+
assert (len > 0u);
1443+
let b = s[0];
1444+
s = unsafe::slice_bytes(s, 1u, len);
1445+
ret b;
1446+
}
1447+
14651448
}
14661449

14671450

@@ -1914,17 +1897,17 @@ mod tests {
19141897
}
19151898

19161899
#[test]
1917-
fn test_shift_byte() {
1900+
fn test_shift_byte() unsafe {
19181901
let s = "ABC";
1919-
let b = shift_byte(s);
1902+
let b = unsafe::shift_byte(s);
19201903
assert (s == "BC");
19211904
assert (b == 65u8);
19221905
}
19231906

19241907
#[test]
1925-
fn test_pop_byte() {
1908+
fn test_pop_byte() unsafe {
19261909
let s = "ABC";
1927-
let b = pop_byte(s);
1910+
let b = unsafe::pop_byte(s);
19281911
assert (s == "AB");
19291912
assert (b == 67u8);
19301913
}

src/libstd/fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ any leading path separator on `post`, and returns the concatenation of the two
8989
with a single path separator between them.
9090
*/
9191

92-
fn connect(pre: path, post: path) -> path {
92+
fn connect(pre: path, post: path) -> path unsafe {
9393
let pre_ = pre;
9494
let post_ = post;
9595
let sep = os_fs::path_sep as u8;
9696
let pre_len = str::byte_len(pre);
9797
let post_len = str::byte_len(post);
98-
if pre_len > 1u && pre[pre_len-1u] == sep { str::pop_byte(pre_); }
99-
if post_len > 1u && post[0] == sep { str::shift_byte(post_); }
98+
if pre_len > 1u && pre[pre_len-1u] == sep { str::unsafe::pop_byte(pre_); }
99+
if post_len > 1u && post[0] == sep { str::unsafe::shift_byte(post_); }
100100
ret pre_ + path_sep() + post_;
101101
}
102102

src/test/bench/99bob-iter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn b8() -> str {
2222
ret "Go to the store and buy some more, # of beer on the wall.";
2323
}
2424

25-
fn sub(t: str, n: int) -> str {
25+
fn sub(t: str, n: int) -> str unsafe {
2626
let b: str = "";
2727
let i: uint = 0u;
2828
let ns: str;
@@ -32,7 +32,8 @@ fn sub(t: str, n: int) -> str {
3232
_ { ns = int::to_str(n, 10u) + " bottles"; }
3333
}
3434
while i < str::byte_len(t) {
35-
if t[i] == '#' as u8 { b += ns; } else { str::push_byte(b, t[i]); }
35+
if t[i] == '#' as u8 { b += ns; }
36+
else { str::unsafe::push_byte(b, t[i]); }
3637
i += 1u;
3738
}
3839
ret b;

src/test/bench/99bob-simple.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn b8() -> str {
2222
ret "Go to the store and buy some more, # of beer on the wall.";
2323
}
2424

25-
fn sub(t: str, n: int) -> str {
25+
fn sub(t: str, n: int) -> str unsafe {
2626
let b: str = "";
2727
let i: uint = 0u;
2828
let ns: str;
@@ -32,7 +32,8 @@ fn sub(t: str, n: int) -> str {
3232
_ { ns = int::to_str(n, 10u) + " bottles"; }
3333
}
3434
while i < str::byte_len(t) {
35-
if t[i] == '#' as u8 { b += ns; } else { str::push_byte(b, t[i]); }
35+
if t[i] == '#' as u8 { b += ns; }
36+
else { str::unsafe::push_byte(b, t[i]); }
3637
i += 1u;
3738
}
3839
ret b;

src/test/bench/shootout-fasta.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
4747
let rng = @{mutable last: std::rand::mk_rng().next()};
4848
let op: str = "";
4949
uint::range(0u, n as uint) {|_i|
50-
str::push_byte(op, select_random(myrandom_next(rng, 100u32),
51-
genelist) as u8);
50+
str::push_char(op, select_random(myrandom_next(rng, 100u32),
51+
genelist));
5252
if str::byte_len(op) >= LINE_LENGTH() {
5353
log(debug, op);
5454
op = "";
@@ -57,12 +57,12 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
5757
if str::byte_len(op) > 0u { log(debug, op); }
5858
}
5959

60-
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) {
60+
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) unsafe {
6161
log(debug, ">" + id + " " + desc);
6262
let op: str = "";
6363
let sl: uint = str::byte_len(s);
6464
uint::range(0u, n as uint) {|i|
65-
str::push_byte(op, s[i % sl]);
65+
str::unsafe::push_byte(op, s[i % sl]);
6666
if str::byte_len(op) >= LINE_LENGTH() {
6767
log(debug, op);
6868
op = "";

0 commit comments

Comments
 (0)