Skip to content

Commit c2eafd2

Browse files
committed
Convert std::fs to istrs. Issue #855
1 parent 051f1ff commit c2eafd2

File tree

13 files changed

+121
-80
lines changed

13 files changed

+121
-80
lines changed

src/comp/back/link.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ fn llvm_err(sess: session::session, msg: str) {
4040
}
4141

4242
fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
43-
let path = fs::connect(sess.get_opts().sysroot, "lib/intrinsics.bc");
43+
let path = istr::to_estr(
44+
fs::connect(istr::from_estr(sess.get_opts().sysroot),
45+
~"lib/intrinsics.bc"));
4446
let membuf =
4547
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(str::buf(path));
4648
if membuf as uint == 0u {
@@ -360,10 +362,12 @@ fn build_link_meta(sess: &session::session, c: &ast::crate, output: &str,
360362
none. {
361363
let name =
362364
{
363-
let os = str::split(fs::basename(output), '.' as u8);
365+
let os = istr::split(
366+
fs::basename(istr::from_estr(output)),
367+
'.' as u8);
364368
assert (vec::len(os) >= 2u);
365369
vec::pop(os);
366-
str::connect(os, ".")
370+
istr::to_estr(istr::connect(os, ~"."))
367371
};
368372
warn_missing(sess, "name", name);
369373
name

src/comp/driver/rustc.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ fn get_arch(triple: str) -> session::arch {
312312
}
313313

314314
fn get_default_sysroot(binary: str) -> str {
315-
let dirname = fs::dirname(binary);
315+
let dirname = istr::to_estr(
316+
fs::dirname(istr::from_estr(binary)));
316317
if str::eq(dirname, binary) { ret "."; }
317318
ret dirname;
318319
}
@@ -443,7 +444,8 @@ fn opts() -> [getopts::opt] {
443444

444445
fn main(args: [str]) {
445446
let binary = vec::shift(args);
446-
let binary_dir = fs::dirname(binary);
447+
let binary_dir = istr::to_estr(
448+
fs::dirname(istr::from_estr(binary)));
447449
let match =
448450
alt getopts::getopts(args, opts()) {
449451
getopts::success(m) { m }
@@ -557,20 +559,20 @@ fn main(args: [str]) {
557559
} else { lib_cmd = "-shared"; }
558560

559561
// Converts a library file name into a gcc -l argument
560-
fn unlib(config: @session::config, filename: str) -> str {
562+
fn unlib(config: @session::config, filename: &istr) -> istr {
561563
let rmlib =
562-
bind fn (config: @session::config, filename: str) -> str {
563-
if config.os == session::os_macos ||
564-
config.os == session::os_linux &&
565-
str::find(filename, "lib") == 0 {
566-
ret str::slice(filename, 3u,
567-
str::byte_len(filename));
568-
} else { ret filename; }
569-
}(config, _);
570-
fn rmext(filename: str) -> str {
571-
let parts = str::split(filename, '.' as u8);
564+
bind fn (config: @session::config, filename: &istr) -> istr {
565+
if config.os == session::os_macos ||
566+
config.os == session::os_linux &&
567+
istr::find(filename, ~"lib") == 0 {
568+
ret istr::slice(filename, 3u,
569+
istr::byte_len(filename));
570+
} else { ret filename; }
571+
}(config, _);
572+
fn rmext(filename: &istr) -> istr {
573+
let parts = istr::split(filename, '.' as u8);
572574
vec::pop(parts);
573-
ret str::connect(parts, ".");
575+
ret istr::connect(parts, ~".");
574576
}
575577
ret alt config.os {
576578
session::os_macos. { rmext(rmlib(filename)) }
@@ -585,10 +587,11 @@ fn main(args: [str]) {
585587
gcc_args += [cratepath];
586588
cont;
587589
}
590+
let cratepath = istr::from_estr(cratepath);
588591
let dir = fs::dirname(cratepath);
589-
if dir != "" { gcc_args += ["-L" + dir]; }
592+
if dir != ~"" { gcc_args += ["-L" + istr::to_estr(dir)]; }
590593
let libarg = unlib(sess.get_targ_cfg(), fs::basename(cratepath));
591-
gcc_args += ["-l" + libarg];
594+
gcc_args += ["-l" + istr::to_estr(libarg)];
592595
}
593596

594597
let ula = cstore::get_used_link_args(cstore);

src/comp/metadata/creader.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import back::x86;
1515
import util::common;
1616
import std::vec;
1717
import std::str;
18+
import std::istr;
1819
import std::fs;
1920
import std::io;
2021
import std::option;
@@ -150,7 +151,8 @@ fn find_library_crate_aux(nn: &{prefix: str, suffix: str}, crate_name: str,
150151
metas: &[@ast::meta_item],
151152
library_search_paths: &[str]) ->
152153
option::t<{ident: str, data: @[u8]}> {
153-
let prefix: str = nn.prefix + crate_name;
154+
let prefix: istr = istr::from_estr(nn.prefix + crate_name);
155+
let suffix: istr = istr::from_estr(nn.suffix);
154156
// FIXME: we could probably use a 'glob' function in std::fs but it will
155157
// be much easier to write once the unsafe module knows more about FFI
156158
// tricks. Currently the glob(3) interface is a bit more than we can
@@ -159,13 +161,17 @@ fn find_library_crate_aux(nn: &{prefix: str, suffix: str}, crate_name: str,
159161

160162
for library_search_path: str in library_search_paths {
161163
log #fmt["searching %s", library_search_path];
162-
for path: str in fs::list_dir(library_search_path) {
163-
log #fmt["searching %s", path];
164-
let f: str = fs::basename(path);
165-
if !(str::starts_with(f, prefix) && str::ends_with(f, nn.suffix))
164+
let library_search_path = istr::from_estr(library_search_path);
165+
for path: istr in fs::list_dir(library_search_path) {
166+
log #fmt["searching %s", istr::to_estr(path)];
167+
let f: istr = fs::basename(path);
168+
let path = istr::to_estr(path);
169+
if !(istr::starts_with(f, prefix) && istr::ends_with(f, suffix))
166170
{
167-
log #fmt["skipping %s, doesn't look like %s*%s", path, prefix,
168-
nn.suffix];
171+
log #fmt["skipping %s, doesn't look like %s*%s",
172+
path,
173+
istr::to_estr(prefix),
174+
istr::to_estr(suffix)];
169175
cont;
170176
}
171177
alt get_metadata_section(path) {

src/comp/syntax/parse/eval.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import std::str;
3+
import std::istr;
34
import std::option;
45
import std::option::some;
56
import std::option::none;
@@ -48,10 +49,12 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
4849
ast::cdir_src_mod(id, file_opt, attrs) {
4950
let file_path = id + ".rs";
5051
alt file_opt { some(f) { file_path = f; } none. { } }
51-
let full_path =
52-
if std::fs::path_is_absolute(file_path) {
53-
file_path
54-
} else { prefix + std::fs::path_sep() + file_path };
52+
let full_path = if std::fs::path_is_absolute(
53+
istr::from_estr(file_path)) {
54+
file_path
55+
} else {
56+
prefix + istr::to_estr(std::fs::path_sep()) + file_path
57+
};
5558
if cx.mode == mode_depend { cx.deps += [full_path]; ret; }
5659
let p0 =
5760
new_parser_from_file(cx.sess, cx.cfg, full_path, cx.chpos,
@@ -73,9 +76,9 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
7376
let path = id;
7477
alt dir_opt { some(d) { path = d; } none. { } }
7578
let full_path =
76-
if std::fs::path_is_absolute(path) {
79+
if std::fs::path_is_absolute(istr::from_estr(path)) {
7780
path
78-
} else { prefix + std::fs::path_sep() + path };
81+
} else { prefix + istr::to_estr(std::fs::path_sep()) + path };
7982
let m0 = eval_crate_directives_to_mod(cx, cdirs, full_path);
8083
let i =
8184
@{ident: id,

src/comp/syntax/parse/parser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import std::io;
33
import std::vec;
44
import std::str;
5+
import std::istr;
56
import std::option;
67
import std::option::some;
78
import std::option::none;
@@ -2525,7 +2526,8 @@ fn parse_crate_from_crate_file(input: &str, cfg: &ast::crate_cfg,
25252526
sess: &parse_sess) -> @ast::crate {
25262527
let p = new_parser_from_file(sess, cfg, input, 0u, 0u, CRATE_FILE);
25272528
let lo = p.get_lo_pos();
2528-
let prefix = std::fs::dirname(p.get_filemap().name);
2529+
let prefix = istr::to_estr(
2530+
std::fs::dirname(istr::from_estr(p.get_filemap().name)));
25292531
let leading_attrs = parse_inner_attrs_and_next(p);
25302532
let crate_attrs = leading_attrs.inner;
25312533
let first_cdir_attr = leading_attrs.next;

src/fuzzer/fuzzer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import std::io;
1010
import std::io::stdout;
1111
import std::vec;
1212
import std::str;
13+
import std::istr;
1314
import std::uint;
1415
import std::option;
1516

@@ -40,8 +41,11 @@ fn find_rust_files(files: &mutable [str], path: str) {
4041
if file_contains(path, "xfail-stage1") {
4142
//log_err "Skipping " + path + " because it is marked as xfail-stage1";
4243
} else { files += [path]; }
43-
} else if fs::file_is_dir(path) && str::find(path, "compile-fail") == -1 {
44-
for p in fs::list_dir(path) { find_rust_files(files, p); }
44+
} else if fs::file_is_dir(istr::from_estr(path))
45+
&& str::find(path, "compile-fail") == -1 {
46+
for p in fs::list_dir(istr::from_estr(path)) {
47+
find_rust_files(files, istr::to_estr(p));
48+
}
4549
}
4650
}
4751

src/lib/fs.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,71 @@ native "rust" mod rustrt {
77
fn rust_file_is_dir(path: str) -> int;
88
}
99

10-
fn path_sep() -> str { ret str::from_char(os_fs::path_sep); }
10+
fn path_sep() -> istr { ret istr::from_char(os_fs::path_sep); }
1111

12-
type path = str;
12+
type path = istr;
1313

14-
fn dirname(p: path) -> path {
15-
let i: int = str::rindex(p, os_fs::path_sep as u8);
14+
fn dirname(p: &path) -> path {
15+
let i: int = istr::rindex(p, os_fs::path_sep as u8);
1616
if i == -1 {
17-
i = str::rindex(p, os_fs::alt_path_sep as u8);
18-
if i == -1 { ret "."; }
17+
i = istr::rindex(p, os_fs::alt_path_sep as u8);
18+
if i == -1 { ret ~"."; }
1919
}
20-
ret str::substr(p, 0u, i as uint);
20+
ret istr::substr(p, 0u, i as uint);
2121
}
2222

23-
fn basename(p: path) -> path {
24-
let i: int = str::rindex(p, os_fs::path_sep as u8);
23+
fn basename(p: &path) -> path {
24+
let i: int = istr::rindex(p, os_fs::path_sep as u8);
2525
if i == -1 {
26-
i = str::rindex(p, os_fs::alt_path_sep as u8);
26+
i = istr::rindex(p, os_fs::alt_path_sep as u8);
2727
if i == -1 { ret p; }
2828
}
29-
let len = str::byte_len(p);
29+
let len = istr::byte_len(p);
3030
if i + 1 as uint >= len { ret p; }
31-
ret str::slice(p, i + 1 as uint, len);
31+
ret istr::slice(p, i + 1 as uint, len);
3232
}
3333

3434

3535
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
36-
fn connect(pre: path, post: path) -> path {
37-
let len = str::byte_len(pre);
36+
fn connect(pre: &path, post: &path) -> path {
37+
let len = istr::byte_len(pre);
3838
ret if pre[len - 1u] == os_fs::path_sep as u8 {
3939

4040
// Trailing '/'?
4141
pre + post
4242
} else { pre + path_sep() + post };
4343
}
4444

45-
fn file_is_dir(p: path) -> bool { ret rustrt::rust_file_is_dir(p) != 0; }
45+
fn file_is_dir(p: &path) -> bool {
46+
ret rustrt::rust_file_is_dir(istr::to_estr(p)) != 0;
47+
}
4648

47-
fn list_dir(p: path) -> [str] {
48-
let pl = str::byte_len(p);
49+
fn list_dir(p: &path) -> [istr] {
50+
let p = p;
51+
let pl = istr::byte_len(p);
4952
if pl == 0u || p[pl - 1u] as char != os_fs::path_sep { p += path_sep(); }
50-
let full_paths: [str] = [];
51-
for filename: str in os_fs::list_dir(p) {
52-
if !str::eq(filename, ".") {
53-
if !str::eq(filename, "..") { full_paths += [p + filename]; }
53+
let full_paths: [istr] = [];
54+
for filename: str in os_fs::list_dir(istr::to_estr(p)) {
55+
let filename = istr::from_estr(filename);
56+
if !istr::eq(filename, ~".") {
57+
if !istr::eq(filename, ~"..") { full_paths += [p + filename]; }
5458
}
5559
}
5660
ret full_paths;
5761
}
5862
59-
fn path_is_absolute(p: path) -> bool { ret os_fs::path_is_absolute(p); }
63+
fn path_is_absolute(p: &path) -> bool {
64+
ret os_fs::path_is_absolute(istr::to_estr(p));
65+
}
6066
6167
// FIXME: under Windows, we should prepend the current drive letter to paths
6268
// that start with a slash.
63-
fn make_absolute(p: path) -> path {
64-
if path_is_absolute(p) { ret p; } else { ret connect(getcwd(), p); }
69+
fn make_absolute(p: &path) -> path {
70+
if path_is_absolute(p) {
71+
ret p;
72+
} else {
73+
ret connect(istr::from_estr(getcwd()), p);
74+
}
6575
}
6676
6777
// Local Variables:

src/lib/istr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ index, rindex, find, starts_with, ends_with, substr, slice, split,
33
concat, connect, to_upper, replace, char_slice, trim_left, trim_right, trim,
44
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars, to_chars,
55
char_len, char_at, bytes, is_ascii, shift_byte, pop_byte, unsafe_from_byte,
6-
unsafe_from_bytes;
6+
unsafe_from_bytes, from_char;
77

88
export from_estr, to_estr;
99

src/test/compiletest/compiletest.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import std::getopts;
33
import std::test;
44
import std::fs;
55
import std::str;
6+
import std::istr;
67
import std::vec;
78
import std::task;
89

@@ -126,7 +127,8 @@ fn make_tests(cx: &cx) -> tests_and_conv_fn {
126127
log #fmt["making tests from %s", cx.config.src_base];
127128
let configport = port::<[u8]>();
128129
let tests = [];
129-
for file: str in fs::list_dir(cx.config.src_base) {
130+
for file: istr in fs::list_dir(istr::from_estr(cx.config.src_base)) {
131+
let file = istr::to_estr(file);
130132
log #fmt["inspecting file %s", file];
131133
if is_test(cx.config, file) {
132134
tests += [make_test(cx, file, configport)];
@@ -137,19 +139,21 @@ fn make_tests(cx: &cx) -> tests_and_conv_fn {
137139

138140
fn is_test(config: &config, testfile: &str) -> bool {
139141
// Pretty-printer does not work with .rc files yet
140-
let valid_extensions =
141-
alt config.mode { mode_pretty. { [".rs"] } _ { [".rc", ".rs"] } };
142-
let invalid_prefixes = [".", "#", "~"];
143-
let name = fs::basename(testfile);
142+
let valid_extensions = alt config.mode {
143+
mode_pretty. { [~".rs"] }
144+
_ { [~".rc", ~".rs"] }
145+
};
146+
let invalid_prefixes = [~".", ~"#", ~"~"];
147+
let name = fs::basename(istr::from_estr(testfile));
144148

145149
let valid = false;
146150

147151
for ext in valid_extensions {
148-
if str::ends_with(name, ext) { valid = true }
152+
if istr::ends_with(name, ext) { valid = true }
149153
}
150154

151155
for pre in invalid_prefixes {
152-
if str::starts_with(name, pre) { valid = false }
156+
if istr::starts_with(name, pre) { valid = false }
153157
}
154158

155159
ret valid;

src/test/compiletest/header.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import std::option;
22
import std::str;
3+
import std::istr;
34
import std::io;
45
import std::fs;
56

@@ -95,7 +96,8 @@ fn parse_pp_exact(line: &str, testfile: &str) -> option::t<str> {
9596
option::some(s) { option::some(s) }
9697
option::none. {
9798
if parse_name_directive(line, "pp-exact") {
98-
option::some(fs::basename(testfile))
99+
option::some(istr::to_estr(
100+
fs::basename(istr::from_estr(testfile))))
99101
} else {
100102
option::none
101103
}

0 commit comments

Comments
 (0)