mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-10 08:57:36 +00:00
Made more stuff pure.
escape functions in char, io.with_str_reader, base64 and md5sum, cell.empty_cell and is_empty.
This commit is contained in:
parent
15989ecb13
commit
6d99a2f8a9
@ -126,16 +126,18 @@ pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
|
|||||||
* - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
|
* - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
|
||||||
* - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
|
* - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
|
||||||
*/
|
*/
|
||||||
pub fn escape_unicode(c: char) -> ~str {
|
pub pure fn escape_unicode(c: char) -> ~str {
|
||||||
let s = u32::to_str(c as u32, 16u);
|
let s = u32::to_str(c as u32, 16u);
|
||||||
let (c, pad) = (if c <= '\xff' { ('x', 2u) }
|
let (c, pad) = (if c <= '\xff' { ('x', 2u) }
|
||||||
else if c <= '\uffff' { ('u', 4u) }
|
else if c <= '\uffff' { ('u', 4u) }
|
||||||
else { ('U', 8u) });
|
else { ('U', 8u) });
|
||||||
assert str::len(s) <= pad;
|
assert str::len(s) <= pad;
|
||||||
let mut out = ~"\\";
|
let mut out = ~"\\";
|
||||||
|
unsafe {
|
||||||
str::push_str(&mut out, str::from_char(c));
|
str::push_str(&mut out, str::from_char(c));
|
||||||
for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); }
|
for uint::range(str::len(s), pad) |_i| { str::push_str(&mut out, ~"0"); }
|
||||||
str::push_str(&mut out, s);
|
str::push_str(&mut out, s);
|
||||||
|
}
|
||||||
move out
|
move out
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +153,7 @@ pub fn escape_unicode(c: char) -> ~str {
|
|||||||
* - Any other chars in the range [0x20,0x7e] are not escaped.
|
* - Any other chars in the range [0x20,0x7e] are not escaped.
|
||||||
* - Any other chars are given hex unicode escapes; see `escape_unicode`.
|
* - Any other chars are given hex unicode escapes; see `escape_unicode`.
|
||||||
*/
|
*/
|
||||||
pub fn escape_default(c: char) -> ~str {
|
pub pure fn escape_default(c: char) -> ~str {
|
||||||
match c {
|
match c {
|
||||||
'\t' => ~"\\t",
|
'\t' => ~"\\t",
|
||||||
'\r' => ~"\\r",
|
'\r' => ~"\\r",
|
||||||
|
@ -512,7 +512,7 @@ pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
|
|||||||
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
|
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
|
pub pure fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
|
||||||
str::byte_slice(s, |bytes| with_bytes_reader(bytes, f))
|
str::byte_slice(s, |bytes| with_bytes_reader(bytes, f))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,18 @@
|
|||||||
use io::Reader;
|
use io::Reader;
|
||||||
|
|
||||||
pub trait ToBase64 {
|
pub trait ToBase64 {
|
||||||
fn to_base64() -> ~str;
|
pure fn to_base64() -> ~str;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl &[u8]: ToBase64 {
|
impl &[u8]: ToBase64 {
|
||||||
fn to_base64() -> ~str {
|
pure fn to_base64() -> ~str {
|
||||||
let chars = str::chars(
|
let chars = str::chars(
|
||||||
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
);
|
);
|
||||||
|
|
||||||
let len = self.len();
|
|
||||||
let mut s = ~"";
|
let mut s = ~"";
|
||||||
|
unsafe {
|
||||||
|
let len = self.len();
|
||||||
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
|
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
|
||||||
|
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
@ -51,23 +52,23 @@ impl &[u8]: ToBase64 {
|
|||||||
}
|
}
|
||||||
_ => fail ~"Algebra is broken, please alert the math police"
|
_ => fail ~"Algebra is broken, please alert the math police"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl &str: ToBase64 {
|
impl &str: ToBase64 {
|
||||||
fn to_base64() -> ~str {
|
pure fn to_base64() -> ~str {
|
||||||
str::to_bytes(self).to_base64()
|
str::to_bytes(self).to_base64()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromBase64 {
|
pub trait FromBase64 {
|
||||||
fn from_base64() -> ~[u8];
|
pure fn from_base64() -> ~[u8];
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ~[u8]: FromBase64 {
|
impl ~[u8]: FromBase64 {
|
||||||
fn from_base64() -> ~[u8] {
|
pure fn from_base64() -> ~[u8] {
|
||||||
if self.len() % 4u != 0u { fail ~"invalid base64 length"; }
|
if self.len() % 4u != 0u { fail ~"invalid base64 length"; }
|
||||||
|
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
@ -80,6 +81,7 @@ impl ~[u8]: FromBase64 {
|
|||||||
|
|
||||||
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
|
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
while i < len {
|
while i < len {
|
||||||
let mut n = 0u;
|
let mut n = 0u;
|
||||||
@ -122,13 +124,13 @@ impl ~[u8]: FromBase64 {
|
|||||||
r.push(((n >> 8u ) & 0xFFu) as u8);
|
r.push(((n >> 8u ) & 0xFFu) as u8);
|
||||||
r.push(((n ) & 0xFFu) as u8);
|
r.push(((n ) & 0xFFu) as u8);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ~str: FromBase64 {
|
impl ~str: FromBase64 {
|
||||||
fn from_base64() -> ~[u8] {
|
pure fn from_base64() -> ~[u8] {
|
||||||
str::to_bytes(self).from_base64()
|
str::to_bytes(self).from_base64()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ pub fn Cell<T>(value: T) -> Cell<T> {
|
|||||||
Cell { value: Some(move value) }
|
Cell { value: Some(move value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn empty_cell<T>() -> Cell<T> {
|
pub pure fn empty_cell<T>() -> Cell<T> {
|
||||||
Cell { value: None }
|
Cell { value: None }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ impl<T> Cell<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the cell is empty and false if the cell is full.
|
/// Returns true if the cell is empty and false if the cell is full.
|
||||||
fn is_empty() -> bool {
|
pure fn is_empty() -> bool {
|
||||||
self.value.is_none()
|
self.value.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#[forbid(deprecated_mode)];
|
#[forbid(deprecated_mode)];
|
||||||
|
|
||||||
pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
|
pub pure fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
|
||||||
// subtle: if orig_len is merely uint, then the code below
|
// subtle: if orig_len is merely uint, then the code below
|
||||||
// which performs shifts by 32 bits or more has undefined
|
// which performs shifts by 32 bits or more has undefined
|
||||||
// results.
|
// results.
|
||||||
@ -10,14 +10,14 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
|
|||||||
let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]);
|
let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]);
|
||||||
let mut bitlen = orig_len + 8u64;
|
let mut bitlen = orig_len + 8u64;
|
||||||
while (bitlen + 64u64) % 512u64 > 0u64 {
|
while (bitlen + 64u64) % 512u64 > 0u64 {
|
||||||
msg.push(0u8);
|
unsafe {msg.push(0u8);}
|
||||||
bitlen += 8u64;
|
bitlen += 8u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
// append length
|
// append length
|
||||||
let mut i = 0u64;
|
let mut i = 0u64;
|
||||||
while i < 8u64 {
|
while i < 8u64 {
|
||||||
msg.push((orig_len >> (i * 8u64)) as u8);
|
unsafe {msg.push((orig_len >> (i * 8u64)) as u8);}
|
||||||
i += 1u64;
|
i += 1u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
|
|||||||
let mut c = 0x98badcfeu32;
|
let mut c = 0x98badcfeu32;
|
||||||
let mut d = 0x10325476u32;
|
let mut d = 0x10325476u32;
|
||||||
|
|
||||||
fn rot(r: int, x: u32) -> u32 {
|
pure fn rot(r: int, x: u32) -> u32 {
|
||||||
let r = r as u32;
|
let r = r as u32;
|
||||||
(x << r) | (x >> (32u32 - r))
|
(x << r) | (x >> (32u32 - r))
|
||||||
}
|
}
|
||||||
@ -84,9 +84,9 @@ pub fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
|
|||||||
return {a: a, b: b, c: c, d: d};
|
return {a: a, b: b, c: c, d: d};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn md4_str(msg: &[u8]) -> ~str {
|
pub pure fn md4_str(msg: &[u8]) -> ~str {
|
||||||
let {a, b, c, d} = md4(msg);
|
let {a, b, c, d} = md4(msg);
|
||||||
fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
|
pure fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
|
||||||
f(a); f(b); f(c); f(d);
|
f(a); f(b); f(c); f(d);
|
||||||
}
|
}
|
||||||
let mut result = ~"";
|
let mut result = ~"";
|
||||||
@ -102,7 +102,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
|
pub pure fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_md4() {
|
fn test_md4() {
|
||||||
|
Loading…
Reference in New Issue
Block a user