Skip to content

Commit 728fe77

Browse files
committed
Use pattern-matching instead of conditionals where appropriate to improve code clarity
1 parent a10974d commit 728fe77

File tree

3 files changed

+81
-113
lines changed

3 files changed

+81
-113
lines changed

src/libcore/bool.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ pub fn is_false(v: bool) -> bool { !v }
4949
/// Parse logic value from `s`
5050
impl FromStr for bool {
5151
fn from_str(s: &str) -> Option<bool> {
52-
if s == "true" {
53-
Some(true)
54-
} else if s == "false" {
55-
Some(false)
56-
} else {
57-
None
52+
match s {
53+
"true" => Some(true),
54+
"false" => Some(false),
55+
_ => None,
5856
}
5957
}
6058
}

src/libcore/path.rs

+42-58
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ pub impl Path {
311311
unsafe {
312312
do str::as_c_str(self.to_str()) |buf| {
313313
let mut st = stat::arch::default_stat();
314-
let r = libc::stat(buf, &mut st);
315-
316-
if r == 0 { Some(st) } else { None }
314+
match libc::stat(buf, &mut st) {
315+
0 => Some(st),
316+
_ => None,
317+
}
317318
}
318319
}
319320
}
@@ -323,9 +324,10 @@ pub impl Path {
323324
unsafe {
324325
do str::as_c_str(self.to_str()) |buf| {
325326
let mut st = stat::arch::default_stat();
326-
let r = libc::lstat(buf, &mut st);
327-
328-
if r == 0 { Some(st) } else { None }
327+
match libc::lstat(buf, &mut st) {
328+
0 => Some(st),
329+
_ => None,
330+
}
329331
}
330332
}
331333
}
@@ -456,10 +458,9 @@ impl GenericPath for PosixPath {
456458

457459
fn dirname(&self) -> ~str {
458460
let s = self.dir_path().to_str();
459-
if s.len() == 0 {
460-
~"."
461-
} else {
462-
s
461+
match s.len() {
462+
0 => ~".",
463+
_ => s,
463464
}
464465
}
465466

@@ -515,25 +516,18 @@ impl GenericPath for PosixPath {
515516
}
516517

517518
fn with_filetype(&self, t: &str) -> PosixPath {
518-
if t.len() == 0 {
519-
match self.filestem() {
520-
None => copy *self,
521-
Some(ref s) => self.with_filename(*s)
522-
}
523-
} else {
524-
let t = ~"." + str::to_owned(t);
525-
match self.filestem() {
526-
None => self.with_filename(t),
527-
Some(ref s) => self.with_filename(*s + t)
528-
}
519+
match (t.len(), self.filestem()) {
520+
(0, None) => copy *self,
521+
(0, Some(ref s)) => self.with_filename(*s),
522+
(_, None) => self.with_filename(fmt!(".%s", t)),
523+
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
529524
}
530525
}
531526

532527
fn dir_path(&self) -> PosixPath {
533-
if self.components.len() != 0 {
534-
self.pop()
535-
} else {
536-
copy *self
528+
match self.components.len() {
529+
0 => copy *self,
530+
_ => self.pop(),
537531
}
538532
}
539533

@@ -638,26 +632,25 @@ impl GenericPath for WindowsPath {
638632
let device;
639633
let rest;
640634

641-
match windows::extract_drive_prefix(s) {
642-
Some((ref d, ref r)) => {
643-
host = None;
644-
device = Some(copy *d);
645-
rest = copy *r;
646-
}
647-
None => {
648-
match windows::extract_unc_prefix(s) {
649-
Some((ref h, ref r)) => {
635+
match (
636+
windows::extract_drive_prefix(s),
637+
windows::extract_unc_prefix(s),
638+
) {
639+
(Some((ref d, ref r)), _) => {
640+
host = None;
641+
device = Some(copy *d);
642+
rest = copy *r;
643+
}
644+
(None, Some((ref h, ref r))) => {
650645
host = Some(copy *h);
651646
device = None;
652647
rest = copy *r;
653-
}
654-
None => {
648+
}
649+
(None, None) => {
655650
host = None;
656651
device = None;
657652
rest = str::to_owned(s);
658-
}
659653
}
660-
}
661654
}
662655

663656
let mut components = ~[];
@@ -673,10 +666,9 @@ impl GenericPath for WindowsPath {
673666

674667
fn dirname(&self) -> ~str {
675668
let s = self.dir_path().to_str();
676-
if s.len() == 0 {
677-
~"."
678-
} else {
679-
s
669+
match s.len() {
670+
0 => ~".",
671+
_ => s,
680672
}
681673
}
682674

@@ -732,26 +724,18 @@ impl GenericPath for WindowsPath {
732724
}
733725

734726
fn with_filetype(&self, t: &str) -> WindowsPath {
735-
if t.len() == 0 {
736-
match self.filestem() {
737-
None => copy *self,
738-
Some(ref s) => self.with_filename(*s)
739-
}
740-
} else {
741-
let t = ~"." + str::to_owned(t);
742-
match self.filestem() {
743-
None => self.with_filename(t),
744-
Some(ref s) =>
745-
self.with_filename(*s + t)
746-
}
727+
match (t.len(), self.filestem()) {
728+
(0, None) => copy *self,
729+
(0, Some(ref s)) => self.with_filename(*s),
730+
(_, None) => self.with_filename(fmt!(".%s", t)),
731+
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
747732
}
748733
}
749734

750735
fn dir_path(&self) -> WindowsPath {
751-
if self.components.len() != 0 {
752-
self.pop()
753-
} else {
754-
copy *self
736+
match self.components.len() {
737+
0 => copy *self,
738+
_ => self.pop(),
755739
}
756740
}
757741

src/libcore/str.rs

+35-49
Original file line numberDiff line numberDiff line change
@@ -128,57 +128,43 @@ pub fn push_char(s: &mut ~str, ch: char) {
128128
let off = len;
129129
do as_buf(*s) |buf, _len| {
130130
let buf: *mut u8 = ::cast::transmute(buf);
131-
if nb == 1u {
132-
*ptr::mut_offset(buf, off) =
133-
code as u8;
134-
} else if nb == 2u {
135-
*ptr::mut_offset(buf, off) =
136-
(code >> 6u & 31u | tag_two_b) as u8;
137-
*ptr::mut_offset(buf, off + 1u) =
138-
(code & 63u | tag_cont) as u8;
139-
} else if nb == 3u {
140-
*ptr::mut_offset(buf, off) =
141-
(code >> 12u & 15u | tag_three_b) as u8;
142-
*ptr::mut_offset(buf, off + 1u) =
143-
(code >> 6u & 63u | tag_cont) as u8;
144-
*ptr::mut_offset(buf, off + 2u) =
145-
(code & 63u | tag_cont) as u8;
146-
} else if nb == 4u {
147-
*ptr::mut_offset(buf, off) =
148-
(code >> 18u & 7u | tag_four_b) as u8;
149-
*ptr::mut_offset(buf, off + 1u) =
150-
(code >> 12u & 63u | tag_cont) as u8;
151-
*ptr::mut_offset(buf, off + 2u) =
152-
(code >> 6u & 63u | tag_cont) as u8;
153-
*ptr::mut_offset(buf, off + 3u) =
154-
(code & 63u | tag_cont) as u8;
155-
} else if nb == 5u {
156-
*ptr::mut_offset(buf, off) =
157-
(code >> 24u & 3u | tag_five_b) as u8;
158-
*ptr::mut_offset(buf, off + 1u) =
159-
(code >> 18u & 63u | tag_cont) as u8;
160-
*ptr::mut_offset(buf, off + 2u) =
161-
(code >> 12u & 63u | tag_cont) as u8;
162-
*ptr::mut_offset(buf, off + 3u) =
163-
(code >> 6u & 63u | tag_cont) as u8;
164-
*ptr::mut_offset(buf, off + 4u) =
165-
(code & 63u | tag_cont) as u8;
166-
} else if nb == 6u {
167-
*ptr::mut_offset(buf, off) =
168-
(code >> 30u & 1u | tag_six_b) as u8;
169-
*ptr::mut_offset(buf, off + 1u) =
170-
(code >> 24u & 63u | tag_cont) as u8;
171-
*ptr::mut_offset(buf, off + 2u) =
172-
(code >> 18u & 63u | tag_cont) as u8;
173-
*ptr::mut_offset(buf, off + 3u) =
174-
(code >> 12u & 63u | tag_cont) as u8;
175-
*ptr::mut_offset(buf, off + 4u) =
176-
(code >> 6u & 63u | tag_cont) as u8;
177-
*ptr::mut_offset(buf, off + 5u) =
178-
(code & 63u | tag_cont) as u8;
131+
match nb {
132+
1u => {
133+
*ptr::mut_offset(buf, off) = code as u8;
134+
}
135+
2u => {
136+
*ptr::mut_offset(buf, off) = (code >> 6u & 31u | tag_two_b) as u8;
137+
*ptr::mut_offset(buf, off + 1u) = (code & 63u | tag_cont) as u8;
138+
}
139+
3u => {
140+
*ptr::mut_offset(buf, off) = (code >> 12u & 15u | tag_three_b) as u8;
141+
*ptr::mut_offset(buf, off + 1u) = (code >> 6u & 63u | tag_cont) as u8;
142+
*ptr::mut_offset(buf, off + 2u) = (code & 63u | tag_cont) as u8;
143+
}
144+
4u => {
145+
*ptr::mut_offset(buf, off) = (code >> 18u & 7u | tag_four_b) as u8;
146+
*ptr::mut_offset(buf, off + 1u) = (code >> 12u & 63u | tag_cont) as u8;
147+
*ptr::mut_offset(buf, off + 2u) = (code >> 6u & 63u | tag_cont) as u8;
148+
*ptr::mut_offset(buf, off + 3u) = (code & 63u | tag_cont) as u8;
149+
}
150+
5u => {
151+
*ptr::mut_offset(buf, off) = (code >> 24u & 3u | tag_five_b) as u8;
152+
*ptr::mut_offset(buf, off + 1u) = (code >> 18u & 63u | tag_cont) as u8;
153+
*ptr::mut_offset(buf, off + 2u) = (code >> 12u & 63u | tag_cont) as u8;
154+
*ptr::mut_offset(buf, off + 3u) = (code >> 6u & 63u | tag_cont) as u8;
155+
*ptr::mut_offset(buf, off + 4u) = (code & 63u | tag_cont) as u8;
156+
}
157+
6u => {
158+
*ptr::mut_offset(buf, off) = (code >> 30u & 1u | tag_six_b) as u8;
159+
*ptr::mut_offset(buf, off + 1u) = (code >> 24u & 63u | tag_cont) as u8;
160+
*ptr::mut_offset(buf, off + 2u) = (code >> 18u & 63u | tag_cont) as u8;
161+
*ptr::mut_offset(buf, off + 3u) = (code >> 12u & 63u | tag_cont) as u8;
162+
*ptr::mut_offset(buf, off + 4u) = (code >> 6u & 63u | tag_cont) as u8;
163+
*ptr::mut_offset(buf, off + 5u) = (code & 63u | tag_cont) as u8;
164+
}
165+
_ => {}
179166
}
180167
}
181-
182168
raw::set_len(s, new_len);
183169
}
184170
}

0 commit comments

Comments
 (0)