Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variety of small cleanups #3805

Merged
merged 7 commits into from
Oct 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcore/dvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub enum DVec<A> {
}

/// Creates a new, empty dvec
pub fn DVec<A>() -> DVec<A> {
pub pure fn DVec<A>() -> DVec<A> {
DVec_({mut data: ~[]})
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ pub mod rt {
// For strings, precision is the maximum characters
// displayed
let mut unpadded = match cv.precision {
CountImplied => s.to_unique(),
CountImplied => s.to_owned(),
CountIs(max) => if max as uint < str::char_len(s) {
str::substr(s, 0u, max as uint)
} else {
s.to_unique()
s.to_owned()
}
};
return unsafe { pad(cv, move unpadded, PadNozero) };
Expand Down
142 changes: 76 additions & 66 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait Reader {
// FIXME (#2004): Seekable really should be orthogonal.

// FIXME (#2982): This should probably return an error.
fn read(buf: &[mut u8], len: uint) -> uint;
fn read(bytes: &[mut u8], len: uint) -> uint;
fn read_byte() -> int;
fn unread_byte(int);
fn eof() -> bool;
Expand Down Expand Up @@ -65,32 +65,32 @@ pub trait ReaderUtil {

impl<T: Reader> T : ReaderUtil {
fn read_bytes(len: uint) -> ~[u8] {
let mut buf = vec::with_capacity(len);
unsafe { vec::raw::set_len(&mut buf, len); }
let mut bytes = vec::with_capacity(len);
unsafe { vec::raw::set_len(&mut bytes, len); }

let count = self.read(buf, len);
let count = self.read(bytes, len);

unsafe { vec::raw::set_len(&mut buf, count); }
move buf
unsafe { vec::raw::set_len(&mut bytes, count); }
move bytes
}
fn read_line() -> ~str {
let mut buf = ~[];
let mut bytes = ~[];
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
buf.push(ch as u8);
bytes.push(ch as u8);
}
str::from_bytes(buf)
str::from_bytes(bytes)
}

fn read_chars(n: uint) -> ~[char] {
// returns the (consumed offset, n_req), appends characters to &chars
fn chars_from_bytes<T: Reader>(buf: &~[u8], chars: &mut ~[char])
fn chars_from_bytes<T: Reader>(bytes: &~[u8], chars: &mut ~[char])
-> (uint, uint) {
let mut i = 0;
let buf_len = buf.len();
while i < buf_len {
let b0 = buf[i];
let bytes_len = bytes.len();
while i < bytes_len {
let b0 = bytes[i];
let w = str::utf8_char_width(b0);
let end = i + w;
i += 1;
Expand All @@ -100,12 +100,12 @@ impl<T: Reader> T : ReaderUtil {
loop;
}
// can't satisfy this char with the existing data
if end > buf_len {
return (i - 1, end - buf_len);
if end > bytes_len {
return (i - 1, end - bytes_len);
}
let mut val = 0;
while i < end {
let next = buf[i] as int;
let next = bytes[i] as int;
i += 1;
assert (next > -1);
assert (next & 192 == 128);
Expand All @@ -119,8 +119,8 @@ impl<T: Reader> T : ReaderUtil {
}
return (i, 0);
}
let mut buf: ~[u8] = ~[];
let mut chars: ~[char] = ~[];
let mut bytes = ~[];
let mut chars = ~[];
// might need more bytes, but reading n will never over-read
let mut nbread = n;
while nbread > 0 {
Expand All @@ -130,15 +130,15 @@ impl<T: Reader> T : ReaderUtil {
// we're split in a unicode char?
break;
}
buf.push_all(data);
let (offset, nbreq) = chars_from_bytes::<T>(&buf, &mut chars);
bytes.push_all(data);
let (offset, nbreq) = chars_from_bytes::<T>(&bytes, &mut chars);
let ncreq = n - chars.len();
// again we either know we need a certain number of bytes
// to complete a character, or we make sure we don't
// over-read by reading 1-byte per char needed
nbread = if ncreq > nbreq { ncreq } else { nbreq };
if nbread > 0 {
buf = vec::slice(buf, offset, buf.len());
bytes = vec::slice(bytes, offset, bytes.len());
}
}
move chars
Expand All @@ -154,12 +154,12 @@ impl<T: Reader> T : ReaderUtil {
}

fn read_c_str() -> ~str {
let mut buf: ~[u8] = ~[];
let mut bytes: ~[u8] = ~[];
loop {
let ch = self.read_byte();
if ch < 1 { break; } else { buf.push(ch as u8); }
if ch < 1 { break; } else { bytes.push(ch as u8); }
}
str::from_bytes(buf)
str::from_bytes(bytes)
}

// FIXME deal with eof? // #2004
Expand Down Expand Up @@ -191,9 +191,9 @@ impl<T: Reader> T : ReaderUtil {
}

fn read_whole_stream() -> ~[u8] {
let mut buf: ~[u8] = ~[];
while !self.eof() { buf.push_all(self.read_bytes(2048u)); }
move buf
let mut bytes: ~[u8] = ~[];
while !self.eof() { bytes.push_all(self.read_bytes(2048u)); }
move bytes
}

fn each_byte(it: fn(int) -> bool) {
Expand Down Expand Up @@ -226,8 +226,8 @@ fn convert_whence(whence: SeekStyle) -> i32 {
}

impl *libc::FILE: Reader {
fn read(buf: &[mut u8], len: uint) -> uint {
do vec::as_mut_buf(buf) |buf_p, buf_len| {
fn read(bytes: &[mut u8], len: uint) -> uint {
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
assert buf_len <= len;

let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
Expand All @@ -250,7 +250,9 @@ impl *libc::FILE: Reader {
// duration of its lifetime.
// FIXME there really should be a better way to do this // #2004
impl<T: Reader, C> {base: T, cleanup: C}: Reader {
fn read(buf: &[mut u8], len: uint) -> uint { self.base.read(buf, len) }
fn read(bytes: &[mut u8], len: uint) -> uint {
self.base.read(bytes, len)
}
fn read_byte() -> int { self.base.read_byte() }
fn unread_byte(byte: int) { self.base.unread_byte(byte); }
fn eof() -> bool { self.base.eof() }
Expand Down Expand Up @@ -297,39 +299,41 @@ pub fn file_reader(path: &Path) -> Result<Reader, ~str> {
}


// Byte buffer readers

pub type ByteBuf = {buf: &[const u8], mut pos: uint};
// Byte readers
pub struct BytesReader {
bytes: &[u8],
mut pos: uint
}

impl ByteBuf: Reader {
fn read(buf: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.buf.len() - self.pos);
impl BytesReader: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.bytes.len() - self.pos);

let view = vec::const_view(self.buf, self.pos, self.buf.len());
vec::bytes::memcpy(buf, view, count);
let view = vec::view(self.bytes, self.pos, self.bytes.len());
vec::bytes::memcpy(bytes, view, count);

self.pos += count;

count
}
fn read_byte() -> int {
if self.pos == self.buf.len() { return -1; }
let b = self.buf[self.pos];
if self.pos == self.bytes.len() { return -1; }
let b = self.bytes[self.pos];
self.pos += 1u;
return b as int;
}
// FIXME (#2738): implement this
fn unread_byte(_byte: int) { error!("Unimplemented: unread_byte"); fail; }
fn eof() -> bool { self.pos == self.buf.len() }
fn eof() -> bool { self.pos == self.bytes.len() }
fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos;
self.pos = seek_in_buf(offset, pos, self.buf.len(), whence);
self.pos = seek_in_buf(offset, pos, self.bytes.len(), whence);
}
fn tell() -> uint { self.pos }
}

pub fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
f({buf: bytes, mut pos: 0u} as Reader)
pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
}

pub fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
Expand Down Expand Up @@ -602,10 +606,10 @@ impl<T: Writer> T : WriterUtil {
self.write_str(&"\n");
}
fn write_int(n: int) {
int::to_str_bytes(n, 10u, |buf| self.write(buf))
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
}
fn write_uint(n: uint) {
uint::to_str_bytes(false, n, 10u, |buf| self.write(buf))
uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
}
fn write_le_uint(n: uint) {
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
Expand Down Expand Up @@ -687,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
pub fn println(s: &str) { stdout().write_line(s); }

pub struct BytesWriter {
buf: DVec<u8>,
bytes: DVec<u8>,
mut pos: uint,
}

impl BytesWriter: Writer {
fn write(v: &[const u8]) {
do self.buf.swap |buf| {
let mut buf <- buf;
do self.bytes.swap |bytes| {
let mut bytes <- bytes;
let v_len = v.len();
let buf_len = buf.len();
let bytes_len = bytes.len();

let count = uint::max(buf_len, self.pos + v_len);
vec::reserve(&mut buf, count);
unsafe { vec::raw::set_len(&mut buf, count); }
let count = uint::max(bytes_len, self.pos + v_len);
vec::reserve(&mut bytes, count);
unsafe { vec::raw::set_len(&mut bytes, count); }

{
let view = vec::mut_view(buf, self.pos, count);
let view = vec::mut_view(bytes, self.pos, count);
vec::bytes::memcpy(view, v, v_len);
}

self.pos += v_len;

move buf
move bytes
}
}
fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos;
let len = self.buf.len();
let len = self.bytes.len();
self.pos = seek_in_buf(offset, pos, len, whence);
}
fn tell() -> uint { self.pos }
Expand All @@ -730,21 +734,25 @@ impl @BytesWriter : Writer {
fn get_type() -> WriterType { (*self).get_type() }
}

pub fn BytesWriter() -> BytesWriter {
BytesWriter { buf: DVec(), mut pos: 0u }
pub pure fn BytesWriter() -> BytesWriter {
BytesWriter { bytes: DVec(), mut pos: 0u }
}

pub fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
let wr = @BytesWriter();
f(wr as Writer);
wr.buf.check_out(|buf| move buf)
// FIXME (#3758): This should not be needed.
unsafe { wr.bytes.check_out(|bytes| move bytes) }
}

pub fn with_str_writer(f: fn(Writer)) -> ~str {
pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
let mut v = with_bytes_writer(f);

// Make sure the vector has a trailing null and is proper utf8.
v.push(0);
// FIXME (#3758): This should not be needed.
unsafe {
// Make sure the vector has a trailing null and is proper utf8.
v.push(0);
}
assert str::is_utf8(v);

unsafe { move ::cast::transmute(move v) }
Expand Down Expand Up @@ -975,15 +983,17 @@ mod tests {
fn bytes_buffer_overwrite() {
let wr = BytesWriter();
wr.write(~[0u8, 1u8, 2u8, 3u8]);
assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 2u8, 3u8]);
assert wr.bytes.borrow(|bytes| bytes == ~[0u8, 1u8, 2u8, 3u8]);
wr.seek(-2, SeekCur);
wr.write(~[4u8, 5u8, 6u8, 7u8]);
assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
assert wr.bytes.borrow(|bytes| bytes ==
~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
wr.seek(-2, SeekEnd);
wr.write(~[8u8]);
wr.seek(1, SeekSet);
wr.write(~[9u8]);
assert wr.buf.borrow(|buf| buf == ~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
assert wr.bytes.borrow(|bytes| bytes ==
~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn console_off() {
#[cfg(notest)]
#[lang="log_type"]
pub fn log_type<T>(level: u32, object: &T) {
let bytes = do io::with_bytes_writer() |writer| {
let bytes = do io::with_bytes_writer |writer| {
repr::write_repr(writer, object);
};
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T {

pub pure fn unwrap_expect<T>(opt: Option<T>, reason: &str) -> T {
//! As unwrap, but with a specified failure message.
if opt.is_none() { fail reason.to_unique(); }
if opt.is_none() { fail reason.to_owned(); }
unwrap(move opt)
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl ReprPrinter {
unsafe {
self.align(sys::min_align_of::<T>());
let value_addr: &T = transmute(copy self.ptr);
(*value_addr).write_repr(self.writer);
value_addr.write_repr(self.writer);
self.bump(sys::size_of::<T>());
true
}
Expand Down
9 changes: 4 additions & 5 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,7 @@ pub trait StrSlice {
pure fn trim() -> ~str;
pure fn trim_left() -> ~str;
pure fn trim_right() -> ~str;
pure fn to_unique() -> ~str;
pure fn to_owned() -> ~str;
pure fn to_managed() -> @str;
pure fn char_at(i: uint) -> char;
}
Expand Down Expand Up @@ -2258,13 +2258,12 @@ impl &str: StrSlice {
pure fn trim_right() -> ~str { trim_right(self) }

#[inline]
pure fn to_unique() -> ~str { self.slice(0, self.len()) }
pure fn to_owned() -> ~str { self.slice(0, self.len()) }

#[inline]
pure fn to_managed() -> @str {
let v = at_vec::from_fn(self.len() + 1, |i| {
if i == self.len() { 0 } else { self[i] }
});
let bytes = as_bytes_slice(self);
let v = at_vec::from_fn(bytes.len(), |i| bytes[i]);
unsafe { ::cast::transmute(v) }
}

Expand Down
Loading