Skip to content

Instantly share code, notes, and snippets.

@lifthrasiir
Created February 11, 2013 15:28
Show Gist options
  • Save lifthrasiir/4755122 to your computer and use it in GitHub Desktop.
Save lifthrasiir/4755122 to your computer and use it in GitHub Desktop.
Utility portions of Angolmois rust edition
pub mod util {
pub mod str {
// NOTE: these additions will be eventually sent to libcore/str.rs
// and are not subject to the above copyright notice.
const tag_cont_u8: u8 = 128u8; // copied from libcore/str.rs
pub pure fn each_chari_byte(s: &str, it: fn(uint, char) -> bool) {
let mut pos = 0u;
let len = s.len();
while pos < len {
let str::CharRange {ch, next} = ::str::char_range_at(s, pos);
if !it(pos, ch) { break; }
pos = next;
}
}
pub pure fn fix_utf8(v: &[const u8], handler: pure fn(&[const u8])
-> ~[u8]) -> ~[u8] {
let mut i = 0u;
let total = vec::len::<u8>(v);
let mut result = ~[];
while i < total {
let chend = i + str::utf8_char_width(v[i]);
let mut j = i + 1u;
while j < total && j < chend && v[j] & 192u8 == tag_cont_u8 {
j += 1u;
}
if j == chend {
assert i != chend;
result = vec::append(result, v.view(i, j));
} else {
result = vec::append(result, handler(v.view(i, j)));
}
i = j;
}
result
}
pub pure fn fix_utf8_str(s: &str, handler: pure fn(&[const u8]) -> ~str) -> ~str {
from_fixed_utf8_bytes(str::to_bytes(s), handler)
}
pub pure fn from_fixed_utf8_bytes(v: &[const u8],
handler: pure fn(&[const u8]) -> ~str) -> ~str {
let newhandler: pure fn(&[const u8]) -> ~[u8] =
|v: &[const u8]| -> ~[u8] { str::to_bytes(handler(v)) };
let bytes = fix_utf8(v, newhandler);
unsafe { str::raw::from_bytes(bytes) }
}
pub trait StrUtil {
pure fn slice_to_end(&self, begin: uint) -> ~str;
pure fn each_chari_byte(self, it: fn(uint, char) -> bool);
pure fn fix_utf8(self, handler: pure fn(&[const u8]) -> ~str) -> ~str;
}
pub impl &str: StrUtil {
pure fn slice_to_end(&self, begin: uint) -> ~str {
self.slice(begin, self.len())
}
pure fn each_chari_byte(self, it: fn(uint, char) -> bool) {
each_chari_byte(self, it)
}
pure fn fix_utf8(self, handler: pure fn(&[const u8]) -> ~str) -> ~str {
fix_utf8_str(self, handler)
}
}
}
pub mod io {
// NOTE: these additions will be eventually sent to libcore/io.rs
// and are not subject to the above copyright notice.
pub trait ReaderUtilEx {
fn read_and_fix_utf8_line(&self, handler: pure fn(&[const u8]) -> ~str) -> ~str;
fn each_fixed_utf8_line(&self, handler: pure fn(&[const u8]) -> ~str,
it: fn(&str) -> bool);
}
pub impl<T: io::Reader> T : ReaderUtilEx {
fn read_and_fix_utf8_line(&self, handler: pure fn(&[const u8]) -> ~str) -> ~str {
let mut bytes = ~[];
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
bytes.push(ch as u8);
}
::util::str::from_fixed_utf8_bytes(bytes, handler)
}
fn each_fixed_utf8_line(&self, handler: pure fn(&[const u8]) -> ~str,
it: fn(&str) -> bool) {
while !self.eof() {
if !it(self.read_and_fix_utf8_line(handler)) { break; }
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment