about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-18 16:27:54 +1000
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-18 17:29:12 +1000
commit728fe775a28b780d2bb26982992d5aab7623ba13 (patch)
treefbd5c027ae51692177b3eee14599803f992f108c /src
parenta10974da2db2e483f21e337f0c9a8a1e1c4c81ed (diff)
downloadrust-728fe775a28b780d2bb26982992d5aab7623ba13.tar.gz
rust-728fe775a28b780d2bb26982992d5aab7623ba13.zip
Use pattern-matching instead of conditionals where appropriate to improve code clarity
Diffstat (limited to 'src')
-rw-r--r--src/libcore/bool.rs10
-rw-r--r--src/libcore/path.rs100
-rw-r--r--src/libcore/str.rs84
3 files changed, 81 insertions, 113 deletions
diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs
index 76a8f456cd5..b3c0b8cad7a 100644
--- a/src/libcore/bool.rs
+++ b/src/libcore/bool.rs
@@ -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,
         }
     }
 }
diff --git a/src/libcore/path.rs b/src/libcore/path.rs
index 2015c5474be..c359b657cd3 100644
--- a/src/libcore/path.rs
+++ b/src/libcore/path.rs
@@ -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(),
         }
     }
 
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index ec7177e5211..59f769fd92d 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -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);
     }
 }