1
Fork 0

(core::str) move push_byte, push_bytes, pop_byte, and shift_byte into str::unsafe

This commit is contained in:
Kevin Cantu 2012-02-11 23:49:03 -08:00
parent 5fb0906f43
commit 944f5a6598
7 changed files with 70 additions and 85 deletions

View file

@ -149,11 +149,11 @@ fn scan_exponent(rdr: reader) -> option<str> {
let c = rdr.curr;
let rslt = "";
if c == 'e' || c == 'E' {
str::push_byte(rslt, c as u8);
str::push_char(rslt, c);
rdr.bump();
c = rdr.curr;
if c == '-' || c == '+' {
str::push_byte(rslt, c as u8);
str::push_char(rslt, c);
rdr.bump();
}
let exponent = scan_digits(rdr, 10u);
@ -170,7 +170,7 @@ fn scan_digits(rdr: reader, radix: uint) -> str {
if c == '_' { rdr.bump(); cont; }
alt char::maybe_digit(c) {
some(d) if (d as uint) < radix {
str::push_byte(rslt, c as u8);
str::push_char(rslt, c);
rdr.bump();
}
_ { break; }
@ -472,11 +472,11 @@ fn next_token_inner(rdr: reader) -> token::token {
let escaped = rdr.curr;
rdr.bump();
alt escaped {
'n' { str::push_byte(accum_str, '\n' as u8); }
'r' { str::push_byte(accum_str, '\r' as u8); }
't' { str::push_byte(accum_str, '\t' as u8); }
'\\' { str::push_byte(accum_str, '\\' as u8); }
'"' { str::push_byte(accum_str, '"' as u8); }
'n' { str::push_char(accum_str, '\n'); }
'r' { str::push_char(accum_str, '\r'); }
't' { str::push_char(accum_str, '\t'); }
'\\' { str::push_char(accum_str, '\\'); }
'"' { str::push_char(accum_str, '"'); }
'\n' { consume_whitespace(rdr); }
'x' {
str::push_char(accum_str, scan_numeric_escape(rdr, 2u));

View file

@ -139,7 +139,7 @@ fn to_str(r: reader, t: token) -> str {
// FIXME: escape.
let tmp = "'";
str::push_char(tmp, c as char);
str::push_byte(tmp, '\'' as u8);
str::push_char(tmp, '\'');
ret tmp;
}
LIT_INT(i, t) {

View file

@ -26,10 +26,6 @@ export
pop_char,
shift_char,
unshift_char,
push_byte,
//push_bytes,
pop_byte,
shift_byte,
trim_left,
trim_right,
trim,
@ -137,7 +133,7 @@ fn from_byte(uu: u8) -> str {
from_bytes([uu])
}
fn push_utf8_bytes(&s: str, ch: char) {
fn push_utf8_bytes(&s: str, ch: char) unsafe {
let code = ch as uint;
let bytes =
if code < max_one_b {
@ -168,7 +164,7 @@ fn push_utf8_bytes(&s: str, ch: char) {
(code >> 6u & 63u | tag_cont) as u8,
(code & 63u | tag_cont) as u8]
};
push_bytes(s, bytes);
unsafe::push_bytes(s, bytes);
}
/*
@ -303,58 +299,6 @@ Prepend a char to a string
*/
fn unshift_char(&s: str, ch: char) { s = from_char(ch) + s; }
/*
Function: push_byte
Appends a byte to a string.
This function is not unicode-safe.
*/
fn push_byte(&s: str, b: u8) { rustrt::rust_str_push(s, b); }
/*
Function: push_bytes
Appends a vector of bytes to a string.
This function is not unicode-safe.
*/
fn push_bytes(&s: str, bytes: [u8]) {
for byte in bytes { rustrt::rust_str_push(s, byte); }
}
/*
Function: pop_byte
Removes the last byte from a string and returns it.
This function is not unicode-safe.
FIXME: move to unsafe?
*/
fn pop_byte(&s: str) -> u8 unsafe {
let len = byte_len(s);
assert (len > 0u);
let b = s[len - 1u];
s = unsafe::slice_bytes(s, 0u, len - 1u);
ret b;
}
/*
Function: shift_byte
Removes the first byte from a string and returns it.
This function is not unicode-safe.
FIXME: move to unsafe?
*/
fn shift_byte(&s: str) -> u8 unsafe {
let len = byte_len(s);
assert (len > 0u);
let b = s[0];
s = unsafe::slice_bytes(s, 1u, len);
ret b;
}
/*
Function: trim_left
@ -592,8 +536,6 @@ fn split(ss: str, sepfn: fn(cc: char)->bool) -> [str] {
Function: split_char
Splits a string into a vector of the substrings separated by a given character
FIXME: also add splitn_char
*/
fn split_char(ss: str, cc: char) -> [str] {
split(ss, {|kk| kk == cc})
@ -1409,7 +1351,11 @@ mod unsafe {
from_bytes,
from_byte,
slice_bytes,
slice_bytes_safe_range;
slice_bytes_safe_range,
push_byte,
push_bytes, // note: wasn't exported
pop_byte,
shift_byte;
// Function: unsafe::from_bytes
//
@ -1462,6 +1408,43 @@ mod unsafe {
assert (end <= byte_len(s));
ret slice_bytes(s, begin, end);
}
// Function: push_byte
//
// Appends a byte to a string. (Not UTF-8 safe).
unsafe fn push_byte(&s: str, b: u8) {
rustrt::rust_str_push(s, b);
}
// Function: push_bytes
//
// Appends a vector of bytes to a string. (Not UTF-8 safe).
unsafe fn push_bytes(&s: str, bytes: [u8]) {
for byte in bytes { rustrt::rust_str_push(s, byte); }
}
// Function: pop_byte
//
// Removes the last byte from a string and returns it. (Not UTF-8 safe).
unsafe fn pop_byte(&s: str) -> u8 unsafe {
let len = byte_len(s);
assert (len > 0u);
let b = s[len - 1u];
s = unsafe::slice_bytes(s, 0u, len - 1u);
ret b;
}
// Function: shift_byte
//
// Removes the first byte from a string and returns it. (Not UTF-8 safe).
unsafe fn shift_byte(&s: str) -> u8 unsafe {
let len = byte_len(s);
assert (len > 0u);
let b = s[0];
s = unsafe::slice_bytes(s, 1u, len);
ret b;
}
}
@ -1914,17 +1897,17 @@ mod tests {
}
#[test]
fn test_shift_byte() {
fn test_shift_byte() unsafe {
let s = "ABC";
let b = shift_byte(s);
let b = unsafe::shift_byte(s);
assert (s == "BC");
assert (b == 65u8);
}
#[test]
fn test_pop_byte() {
fn test_pop_byte() unsafe {
let s = "ABC";
let b = pop_byte(s);
let b = unsafe::pop_byte(s);
assert (s == "AB");
assert (b == 67u8);
}

View file

@ -89,14 +89,14 @@ any leading path separator on `post`, and returns the concatenation of the two
with a single path separator between them.
*/
fn connect(pre: path, post: path) -> path {
fn connect(pre: path, post: path) -> path unsafe {
let pre_ = pre;
let post_ = post;
let sep = os_fs::path_sep as u8;
let pre_len = str::byte_len(pre);
let post_len = str::byte_len(post);
if pre_len > 1u && pre[pre_len-1u] == sep { str::pop_byte(pre_); }
if post_len > 1u && post[0] == sep { str::shift_byte(post_); }
if pre_len > 1u && pre[pre_len-1u] == sep { str::unsafe::pop_byte(pre_); }
if post_len > 1u && post[0] == sep { str::unsafe::shift_byte(post_); }
ret pre_ + path_sep() + post_;
}

View file

@ -22,7 +22,7 @@ fn b8() -> str {
ret "Go to the store and buy some more, # of beer on the wall.";
}
fn sub(t: str, n: int) -> str {
fn sub(t: str, n: int) -> str unsafe {
let b: str = "";
let i: uint = 0u;
let ns: str;
@ -32,7 +32,8 @@ fn sub(t: str, n: int) -> str {
_ { ns = int::to_str(n, 10u) + " bottles"; }
}
while i < str::byte_len(t) {
if t[i] == '#' as u8 { b += ns; } else { str::push_byte(b, t[i]); }
if t[i] == '#' as u8 { b += ns; }
else { str::unsafe::push_byte(b, t[i]); }
i += 1u;
}
ret b;

View file

@ -22,7 +22,7 @@ fn b8() -> str {
ret "Go to the store and buy some more, # of beer on the wall.";
}
fn sub(t: str, n: int) -> str {
fn sub(t: str, n: int) -> str unsafe {
let b: str = "";
let i: uint = 0u;
let ns: str;
@ -32,7 +32,8 @@ fn sub(t: str, n: int) -> str {
_ { ns = int::to_str(n, 10u) + " bottles"; }
}
while i < str::byte_len(t) {
if t[i] == '#' as u8 { b += ns; } else { str::push_byte(b, t[i]); }
if t[i] == '#' as u8 { b += ns; }
else { str::unsafe::push_byte(b, t[i]); }
i += 1u;
}
ret b;

View file

@ -47,8 +47,8 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
let rng = @{mutable last: std::rand::mk_rng().next()};
let op: str = "";
uint::range(0u, n as uint) {|_i|
str::push_byte(op, select_random(myrandom_next(rng, 100u32),
genelist) as u8);
str::push_char(op, select_random(myrandom_next(rng, 100u32),
genelist));
if str::byte_len(op) >= LINE_LENGTH() {
log(debug, op);
op = "";
@ -57,12 +57,12 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
if str::byte_len(op) > 0u { log(debug, op); }
}
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) {
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) unsafe {
log(debug, ">" + id + " " + desc);
let op: str = "";
let sl: uint = str::byte_len(s);
uint::range(0u, n as uint) {|i|
str::push_byte(op, s[i % sl]);
str::unsafe::push_byte(op, s[i % sl]);
if str::byte_len(op) >= LINE_LENGTH() {
log(debug, op);
op = "";