diff options
| author | Sam Nystrom <sam@samnystrom.dev> | 2023-06-23 17:04:12 -0400 |
|---|---|---|
| committer | Sam Nystrom <sam@samnystrom.dev> | 2023-06-23 17:44:35 -0400 |
| commit | aaf9675d7d73f334f6cb6305066451db7a32e12e (patch) | |
| tree | 0086b37690dcab19ab8c31ff85fc20d983fde3e7 | |
| parent | 8efeed7a3ed024a96625864135b038294bbcc82a (diff) | |
Don't rely on implementation details of bufio
Previously we modified internal fields of bufio::memstream to make it
readable, which is bad. This commit uses bufio::fixed to read from a
strio::stream.
| -rw-r--r-- | main.ha | 21 |
1 files changed, 12 insertions, 9 deletions
@@ -24,6 +24,7 @@ use fs; use os; use strconv; use strings; +use strio; use time; use unix::poll; use unix::tty; @@ -200,9 +201,9 @@ type state = enum { fn parse_srt(file: io::handle) []subtitle = { let subtitles: []subtitle = []; let current = subtitle { ... }; - let content = bufio::dynamic(io::mode::RDWR); + let content = strio::dynamic(); defer io::close(&content)!; - io::writeall(&content, strings::toutf8("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n"))!; + fmt::fprint(&content, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n")!; let state = state::INDEX; for (let nr = 0; true; nr += 1) { @@ -239,14 +240,16 @@ fn parse_srt(file: io::handle) []subtitle = { state = state::TEXT; case state::TEXT => if (len(line) > 0) { - io::writeall(&content, strings::toutf8(line))!; - io::writeall(&content, strings::toutf8("\n"))!; + fmt::fprintf(&content, "{}\n", line)!; continue; }; - io::writeall(&content, strings::toutf8("</root>"))!; - content.pos = 0; - current.text = match (parse_text(&content)) { + fmt::fprint(&content, "</root>")!; + let buf = bufio::fixed( + strings::toutf8(strio::string(&content)), + io::mode::READ, + ); + current.text = match (parse_text(&buf)) { case let t: []text => yield t; case let err: io::error => @@ -258,8 +261,8 @@ fn parse_srt(file: io::handle) []subtitle = { append(subtitles, current); current = subtitle { ... }; - bufio::reset(&content); - io::writeall(&content, strings::toutf8("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n"))!; + strio::reset(&content); + fmt::fprint(&content, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n")!; state = state::INDEX; }; |
