Skip to content

str::from_bytes progress #1670

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

Merged
merged 12 commits into from
Jan 28, 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/comp/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn get_input_str(sess: session, infile: str) -> str {
}
}
} else { io::stdin() };
str::unsafe_from_bytes(stream.read_whole_stream())
str::from_bytes(stream.read_whole_stream())
}

fn time<T>(do_it: bool, what: str, thunk: fn@() -> T) -> T {
Expand Down
20 changes: 10 additions & 10 deletions src/comp/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn item_family(item: ebml::doc) -> u8 {

fn item_symbol(item: ebml::doc) -> str {
let sym = ebml::get_doc(item, tag_items_data_item_symbol);
ret str::unsafe_from_bytes(ebml::doc_data(sym));
ret str::from_bytes(ebml::doc_data(sym));
}

fn variant_tag_id(d: ebml::doc) -> ast::def_id {
Expand Down Expand Up @@ -161,7 +161,7 @@ fn tag_variant_ids(item: ebml::doc, cdata: cmd) -> [ast::def_id] {
// definition the path refers to.
fn resolve_path(path: [ast::ident], data: @[u8]) -> [ast::def_id] {
fn eq_item(data: [u8], s: str) -> bool {
ret str::eq(str::unsafe_from_bytes(data), s);
ret str::eq(str::from_bytes(data), s);
}
let s = str::connect(path, "::");
let md = ebml::new_doc(data);
Expand All @@ -177,7 +177,7 @@ fn resolve_path(path: [ast::ident], data: @[u8]) -> [ast::def_id] {

fn item_name(item: ebml::doc) -> ast::ident {
let name = ebml::get_doc(item, tag_paths_data_name);
str::unsafe_from_bytes(ebml::doc_data(name))
str::from_bytes(ebml::doc_data(name))
}

fn lookup_item_name(data: @[u8], id: ast::node_id) -> ast::ident {
Expand Down Expand Up @@ -326,7 +326,7 @@ fn read_path(d: ebml::doc) -> {path: str, pos: uint} {
let desc = ebml::doc_data(d);
let pos = ebml::be_uint_from_bytes(@desc, 0u, 4u);
let pathbytes = vec::slice::<u8>(desc, 4u, vec::len::<u8>(desc));
let path = str::unsafe_from_bytes(pathbytes);
let path = str::from_bytes(pathbytes);
ret {path: path, pos: pos};
}

Expand Down Expand Up @@ -359,21 +359,21 @@ fn get_meta_items(md: ebml::doc) -> [@ast::meta_item] {
let items: [@ast::meta_item] = [];
ebml::tagged_docs(md, tag_meta_item_word) {|meta_item_doc|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let n = str::unsafe_from_bytes(ebml::doc_data(nd));
let n = str::from_bytes(ebml::doc_data(nd));
items += [attr::mk_word_item(n)];
};
ebml::tagged_docs(md, tag_meta_item_name_value) {|meta_item_doc|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let vd = ebml::get_doc(meta_item_doc, tag_meta_item_value);
let n = str::unsafe_from_bytes(ebml::doc_data(nd));
let v = str::unsafe_from_bytes(ebml::doc_data(vd));
let n = str::from_bytes(ebml::doc_data(nd));
let v = str::from_bytes(ebml::doc_data(vd));
// FIXME (#611): Should be able to decode meta_name_value variants,
// but currently they can't be encoded
items += [attr::mk_name_value_item_str(n, v)];
};
ebml::tagged_docs(md, tag_meta_item_list) {|meta_item_doc|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let n = str::unsafe_from_bytes(ebml::doc_data(nd));
let n = str::from_bytes(ebml::doc_data(nd));
let subitems = get_meta_items(meta_item_doc);
items += [attr::mk_list_item(n, subitems)];
};
Expand Down Expand Up @@ -428,7 +428,7 @@ fn get_crate_deps(data: @[u8]) -> [crate_dep] {
let depsdoc = ebml::get_doc(cratedoc, tag_crate_deps);
let crate_num = 1;
ebml::tagged_docs(depsdoc, tag_crate_dep) {|depdoc|
let depname = str::unsafe_from_bytes(ebml::doc_data(depdoc));
let depname = str::from_bytes(ebml::doc_data(depdoc));
deps += [{cnum: crate_num, ident: depname}];
crate_num += 1;
};
Expand All @@ -448,7 +448,7 @@ fn list_crate_deps(data: @[u8], out: io::writer) {
fn get_crate_hash(data: @[u8]) -> str {
let cratedoc = ebml::new_doc(data);
let hashdoc = ebml::get_doc(cratedoc, tag_crate_hash);
ret str::unsafe_from_bytes(ebml::doc_data(hashdoc));
ret str::from_bytes(ebml::doc_data(hashdoc));
}

fn list_crate_items(bytes: @[u8], md: ebml::doc, out: io::writer) {
Expand Down
4 changes: 2 additions & 2 deletions src/comp/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ fn encode_hash(ebml_w: ebml::writer, hash: str) {
ebml::end_tag(ebml_w);
}

fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str {
fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> [u8] {

let abbrevs = ty::new_ty_hash();
let ecx = @{ccx: cx, type_abbrevs: abbrevs};
Expand Down Expand Up @@ -695,7 +695,7 @@ fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str {
// Pad this, since something (LLVM, presumably) is cutting off the
// remaining % 4 bytes.
buf_w.write([0u8, 0u8, 0u8, 0u8]);
io::mem_buffer_str(buf)
io::mem_buffer_buf(buf)
}

// Get the encoded string for a type
Expand Down
4 changes: 2 additions & 2 deletions src/comp/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn parse_ident_(st: @pstate, is_last: fn@(char) -> bool) ->
ast::ident {
let rslt = "";
while !is_last(peek(st) as char) {
rslt += str::unsafe_from_byte(next(st));
rslt += str::from_byte(next(st));
}
ret rslt;
}
Expand Down Expand Up @@ -226,7 +226,7 @@ fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
while peek(st) as char != ']' {
let name = "";
while peek(st) as char != '=' {
name += str::unsafe_from_byte(next(st));
name += str::from_byte(next(st));
}
st.pos = st.pos + 1u;
fields += [{ident: name, mt: parse_mt(st, conv)}];
Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn sanitize(s: str) -> str {
c != ' ' as u8 && c != '\t' as u8 && c != ';' as u8
{
let v = [c];
result += str::unsafe_from_bytes(v);
result += str::from_bytes(v);
}
}
}
Expand Down Expand Up @@ -5419,7 +5419,7 @@ fn fill_crate_map(ccx: @crate_ctxt, map: ValueRef) {

fn write_metadata(cx: @crate_ctxt, crate: @ast::crate) {
if !cx.sess.building_library { ret; }
let llmeta = C_postr(metadata::encoder::encode_metadata(cx, crate));
let llmeta = C_bytes(metadata::encoder::encode_metadata(cx, crate));
let llconst = trans_common::C_struct([llmeta]);
let llglobal =
str::as_buf("rust_metadata",
Expand Down
2 changes: 1 addition & 1 deletion src/comp/syntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ fn gather_comments_and_literals(cm: codemap::codemap,
path: str,
srdr: io::reader) ->
{cmnts: [cmnt], lits: [lit]} {
let src = str::unsafe_from_bytes(srdr.read_whole_stream());
let src = str::from_bytes(srdr.read_whole_stream());
let itr = @interner::mk::<str>(str::hash, str::eq);
let rdr = new_reader(cm, diagnostic, src,
codemap::new_filemap(path, 0u, 0u), itr);
Expand Down
2 changes: 1 addition & 1 deletion src/comp/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> str {
}
ty_var(v) { "<T" + int::str(v) + ">" }
ty_param(id, _) {
"'" + str::unsafe_from_bytes([('a' as u8) + (id as u8)])
"'" + str::from_bytes([('a' as u8) + (id as u8)])
}
_ { ty_to_short_str(cx, typ) }
}
Expand Down
8 changes: 4 additions & 4 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn readclose(fd: fd_t) -> str {
let buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::unsafe_from_bytes(bytes);
buf += str::from_bytes(bytes);
}
os::fclose(file);
ret buf;
Expand Down Expand Up @@ -114,8 +114,8 @@ fn worker(p: port<request>) {
// the alt discriminant are wrong.
alt recv(p) {
exec(lib_path, prog, args, respchan) {
{lib_path: str::unsafe_from_bytes(lib_path),
prog: str::unsafe_from_bytes(prog),
{lib_path: str::from_bytes(lib_path),
prog: str::from_bytes(prog),
args: clone_vecu8str(args),
respchan: respchan}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ fn clone_vecstr(v: [str]) -> [[u8]] {
fn clone_vecu8str(v: [[u8]]) -> [str] {
let r = [];
for t in vec::slice(v, 0u, vec::len(v)) {
r += [str::unsafe_from_bytes(t)];
r += [str::from_bytes(t)];
}
ret r;
}
5 changes: 3 additions & 2 deletions src/libcore/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ mod rt {
fn str_init_elt(n_elts: uint, c: char) -> str {
let svec = vec::init_elt::<u8>(n_elts, c as u8);

ret str::unsafe_from_bytes(svec);
ret str::from_bytes(svec);
}
enum pad_mode { pad_signed, pad_unsigned, pad_nozero, }
fn pad(cv: conv, s: str, mode: pad_mode) -> str {
Expand Down Expand Up @@ -439,7 +439,8 @@ mod rt {
if signed && zero_padding && str::byte_len(s) > 0u {
let head = s[0];
if head == '+' as u8 || head == '-' as u8 || head == ' ' as u8 {
let headstr = str::unsafe_from_bytes([head]);
let headstr = str::from_bytes([head]);
// FIXME: not UTF-8 safe
let bytelen = str::byte_len(s);
let numpart = str::substr(s, 1u, bytelen - 1u);
ret headstr + padstr + numpart;
Expand Down
69 changes: 47 additions & 22 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export
// Creating a string
from_bytes,
unsafe_from_bytes,
from_byte,
unsafe_from_byte,
//push_utf8_bytes,
from_char,
Expand Down Expand Up @@ -117,14 +118,11 @@ Section: Creating a string
/*
Function: from_bytes

Safely convert a vector of bytes to a UTF-8 string, or error
Convert a vector of bytes to a UTF-8 string. Fails if invalid UTF-8.
*/
fn from_bytes(vv: [u8]) -> result::t<str, str> {
if is_utf8(vv) {
ret result::ok(unsafe_from_bytes(vv));
} else {
ret result::err("vector doesn't contain valid UTF-8");
}
fn from_bytes(vv: [u8]) -> str {
assert is_utf8(vv);
ret unsafe_from_bytes(vv);
}

/*
Expand All @@ -133,7 +131,7 @@ Function: unsafe_from_bytes
Converts a vector of bytes to a string. Does not verify that the
vector contains valid UTF-8.

// FIXME: remove?
FIXME: stop exporting
*/
fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
let vcopy: [u8] = v + [0u8];
Expand All @@ -148,10 +146,20 @@ Function: unsafe_from_byte
Converts a byte to a string. Does not verify that the byte is
valid UTF-8.

FIXME: rename to 'from_byte'
FIXME: stop exporting
*/
fn unsafe_from_byte(u: u8) -> str { unsafe_from_bytes([u]) }


/*
Function: from_byte

Convert a byte to a UTF-8 string. Fails if invalid UTF-8.
*/
fn from_byte(uu: u8) -> str {
from_bytes([uu])
}

fn push_utf8_bytes(&s: str, ch: char) {
let code = ch as uint;
let bytes =
Expand Down Expand Up @@ -209,16 +217,16 @@ Function: from_cstr
Create a Rust string from a null-terminated C string
*/
unsafe fn from_cstr(cstr: sbuf) -> str {
let res = "";
let res = [];
let start = cstr;
let curr = start;
let i = 0u;
while *curr != 0u8 {
push_byte(res, *curr);
vec::push(res, *curr);
i += 1u;
curr = ptr::offset(start, i);
}
ret res;
ret from_bytes(res);
}

/*
Expand Down Expand Up @@ -526,7 +534,7 @@ fn split(s: str, sep: u8) -> [str] {
v += [accum];
accum = "";
ends_with_sep = true;
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
} else { accum += from_byte(c); ends_with_sep = false; }
}
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
ret v;
Expand Down Expand Up @@ -554,7 +562,7 @@ fn splitn(s: str, sep: u8, count: uint) -> [str] {
v += [accum];
accum = "";
ends_with_sep = true;
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
} else { accum += from_byte(c); ends_with_sep = false; }
}
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
ret v;
Expand All @@ -575,26 +583,26 @@ FIXME: should behave like split and split_char:
*/
fn split_str(s: str, sep: str) -> [str] {
assert byte_len(sep) > 0u;
let v: [str] = [], accum = "", sep_match = 0u, leading = true;
let v: [str] = [], accum = [], sep_match = 0u, leading = true;
for c: u8 in s {
// Did we match the entire separator?
if sep_match == byte_len(sep) {
if !leading { v += [accum]; }
accum = "";
if !leading { vec::push(v, from_bytes(accum)); }
accum = [];
sep_match = 0u;
}

if c == sep[sep_match] {
sep_match += 1u;
} else {
sep_match = 0u;
accum += unsafe_from_byte(c);
vec::push(accum, c);
leading = false;
}
}

if byte_len(accum) > 0u { v += [accum]; }
if sep_match == byte_len(sep) { v += [""]; }
if vec::len(accum) > 0u { vec::push(v, from_bytes(accum)); }
if sep_match == byte_len(sep) { vec::push(v, ""); }

ret v;
}
Expand Down Expand Up @@ -1783,7 +1791,24 @@ mod tests {
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];

assert ss == result::get(from_bytes(bb));
assert ss == from_bytes(bb);
}

#[test]
#[should_fail]
fn test_from_bytes_fail() {
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];

let _x = from_bytes(bb);
}

#[test]
Expand Down Expand Up @@ -1821,7 +1846,7 @@ mod tests {
let s1: str = "All mimsy were the borogoves";

let v: [u8] = bytes(s1);
let s2: str = unsafe_from_bytes(v);
let s2: str = from_bytes(v);
let i: uint = 0u;
let n1: uint = byte_len(s1);
let n2: uint = vec::len::<u8>(v);
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ fn to_str(num: uint, radix: uint) -> str {
if n == 0u { ret "0"; }
let s: str = "";
while n != 0u {
s += str::unsafe_from_byte(digit(n % radix) as u8);
s += str::from_byte(digit(n % radix) as u8);
n /= radix;
}
let s1: str = "";
let len: uint = str::byte_len(s);
while len != 0u { len -= 1u; s1 += str::unsafe_from_byte(s[len]); }
while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); }
ret s1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstd/freebsd_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
/// followed by a path separator
fn get_exe_path() -> option::t<fs::path> unsafe {
let bufsize = 1023u;
let path = str::unsafe_from_bytes(vec::init_elt(bufsize, 0u8));
// FIXME: path "strings" will likely need fixing...
let path = str::from_bytes(vec::init_elt(bufsize, 0u8));
let mib = [libc_constants::CTL_KERN,
libc_constants::KERN_PROC,
libc_constants::KERN_PROC_PATHNAME, -1i32];
Expand Down
Loading