Use pattern-matching instead of conditionals where appropriate to improve code clarity

This commit is contained in:
Brendan Zabarauskas 2013-05-18 16:27:54 +10:00
parent a10974da2d
commit 728fe775a2
3 changed files with 81 additions and 113 deletions

View file

@ -49,12 +49,10 @@ pub fn is_false(v: bool) -> bool { !v }
/// Parse logic value from `s`
impl FromStr for bool {
fn from_str(s: &str) -> Option<bool> {
if s == "true" {
Some(true)
} else if s == "false" {
Some(false)
} else {
None
match s {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
}

View file

@ -311,9 +311,10 @@ pub impl Path {
unsafe {
do str::as_c_str(self.to_str()) |buf| {
let mut st = stat::arch::default_stat();
let r = libc::stat(buf, &mut st);
if r == 0 { Some(st) } else { None }
match libc::stat(buf, &mut st) {
0 => Some(st),
_ => None,
}
}
}
}
@ -323,9 +324,10 @@ pub impl Path {
unsafe {
do str::as_c_str(self.to_str()) |buf| {
let mut st = stat::arch::default_stat();
let r = libc::lstat(buf, &mut st);
if r == 0 { Some(st) } else { None }
match libc::lstat(buf, &mut st) {
0 => Some(st),
_ => None,
}
}
}
}
@ -456,10 +458,9 @@ impl GenericPath for PosixPath {
fn dirname(&self) -> ~str {
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
match s.len() {
0 => ~".",
_ => s,
}
}
@ -515,25 +516,18 @@ impl GenericPath for PosixPath {
}
fn with_filetype(&self, t: &str) -> PosixPath {
if t.len() == 0 {
match self.filestem() {
None => copy *self,
Some(ref s) => self.with_filename(*s)
}
} else {
let t = ~"." + str::to_owned(t);
match self.filestem() {
None => self.with_filename(t),
Some(ref s) => self.with_filename(*s + t)
}
match (t.len(), self.filestem()) {
(0, None) => copy *self,
(0, Some(ref s)) => self.with_filename(*s),
(_, None) => self.with_filename(fmt!(".%s", t)),
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
}
}
fn dir_path(&self) -> PosixPath {
if self.components.len() != 0 {
self.pop()
} else {
copy *self
match self.components.len() {
0 => copy *self,
_ => self.pop(),
}
}
@ -638,26 +632,25 @@ impl GenericPath for WindowsPath {
let device;
let rest;
match windows::extract_drive_prefix(s) {
Some((ref d, ref r)) => {
host = None;
device = Some(copy *d);
rest = copy *r;
}
None => {
match windows::extract_unc_prefix(s) {
Some((ref h, ref r)) => {
match (
windows::extract_drive_prefix(s),
windows::extract_unc_prefix(s),
) {
(Some((ref d, ref r)), _) => {
host = None;
device = Some(copy *d);
rest = copy *r;
}
(None, Some((ref h, ref r))) => {
host = Some(copy *h);
device = None;
rest = copy *r;
}
None => {
}
(None, None) => {
host = None;
device = None;
rest = str::to_owned(s);
}
}
}
}
let mut components = ~[];
@ -673,10 +666,9 @@ impl GenericPath for WindowsPath {
fn dirname(&self) -> ~str {
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
match s.len() {
0 => ~".",
_ => s,
}
}
@ -732,26 +724,18 @@ impl GenericPath for WindowsPath {
}
fn with_filetype(&self, t: &str) -> WindowsPath {
if t.len() == 0 {
match self.filestem() {
None => copy *self,
Some(ref s) => self.with_filename(*s)
}
} else {
let t = ~"." + str::to_owned(t);
match self.filestem() {
None => self.with_filename(t),
Some(ref s) =>
self.with_filename(*s + t)
}
match (t.len(), self.filestem()) {
(0, None) => copy *self,
(0, Some(ref s)) => self.with_filename(*s),
(_, None) => self.with_filename(fmt!(".%s", t)),
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
}
}
fn dir_path(&self) -> WindowsPath {
if self.components.len() != 0 {
self.pop()
} else {
copy *self
match self.components.len() {
0 => copy *self,
_ => self.pop(),
}
}

View file

@ -128,57 +128,43 @@ pub fn push_char(s: &mut ~str, ch: char) {
let off = len;
do as_buf(*s) |buf, _len| {
let buf: *mut u8 = ::cast::transmute(buf);
if nb == 1u {
*ptr::mut_offset(buf, off) =
code as u8;
} else if nb == 2u {
*ptr::mut_offset(buf, off) =
(code >> 6u & 31u | tag_two_b) as u8;
*ptr::mut_offset(buf, off + 1u) =
(code & 63u | tag_cont) as u8;
} else if nb == 3u {
*ptr::mut_offset(buf, off) =
(code >> 12u & 15u | tag_three_b) as u8;
*ptr::mut_offset(buf, off + 1u) =
(code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) =
(code & 63u | tag_cont) as u8;
} else if nb == 4u {
*ptr::mut_offset(buf, off) =
(code >> 18u & 7u | tag_four_b) as u8;
*ptr::mut_offset(buf, off + 1u) =
(code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) =
(code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) =
(code & 63u | tag_cont) as u8;
} else if nb == 5u {
*ptr::mut_offset(buf, off) =
(code >> 24u & 3u | tag_five_b) as u8;
*ptr::mut_offset(buf, off + 1u) =
(code >> 18u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) =
(code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) =
(code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 4u) =
(code & 63u | tag_cont) as u8;
} else if nb == 6u {
*ptr::mut_offset(buf, off) =
(code >> 30u & 1u | tag_six_b) as u8;
*ptr::mut_offset(buf, off + 1u) =
(code >> 24u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) =
(code >> 18u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) =
(code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 4u) =
(code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 5u) =
(code & 63u | tag_cont) as u8;
match nb {
1u => {
*ptr::mut_offset(buf, off) = code as u8;
}
2u => {
*ptr::mut_offset(buf, off) = (code >> 6u & 31u | tag_two_b) as u8;
*ptr::mut_offset(buf, off + 1u) = (code & 63u | tag_cont) as u8;
}
3u => {
*ptr::mut_offset(buf, off) = (code >> 12u & 15u | tag_three_b) as u8;
*ptr::mut_offset(buf, off + 1u) = (code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) = (code & 63u | tag_cont) as u8;
}
4u => {
*ptr::mut_offset(buf, off) = (code >> 18u & 7u | tag_four_b) as u8;
*ptr::mut_offset(buf, off + 1u) = (code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) = (code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) = (code & 63u | tag_cont) as u8;
}
5u => {
*ptr::mut_offset(buf, off) = (code >> 24u & 3u | tag_five_b) as u8;
*ptr::mut_offset(buf, off + 1u) = (code >> 18u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) = (code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) = (code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 4u) = (code & 63u | tag_cont) as u8;
}
6u => {
*ptr::mut_offset(buf, off) = (code >> 30u & 1u | tag_six_b) as u8;
*ptr::mut_offset(buf, off + 1u) = (code >> 24u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 2u) = (code >> 18u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 3u) = (code >> 12u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 4u) = (code >> 6u & 63u | tag_cont) as u8;
*ptr::mut_offset(buf, off + 5u) = (code & 63u | tag_cont) as u8;
}
_ => {}
}
}
raw::set_len(s, new_len);
}
}