Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d96f7d2

Browse files
update: match instead of if-else in sup/sub script matching
1 parent 7f5e297 commit d96f7d2

File tree

4 files changed

+42
-50
lines changed

4 files changed

+42
-50
lines changed

‎CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.2.23
2+
3+
+ support color display
4+
15
## v0.2.22
26
+ Fixed the cache can't update with new added problems
37

‎Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ path = "src/bin/lc.rs"
44

55
[package]
66
name = "leetcode-cli"
7-
version = "0.2.22"
7+
version = "0.2.23"
88
authors = ["clearloop <cdr.today@foxmail.com>"]
99
edition = "2018"
1010
description = "Leet your code in command-line."

‎src/helper.rs

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,58 +112,40 @@ mod html {
112112
}
113113

114114
pub fn superscript(n: u8) -> String {
115-
if n == 0 {
116-
"0".to_owned()
117-
} else if n == 1 {
118-
"1".to_owned()
119-
} else if n == 2 {
120-
"2".to_owned()
121-
} else if n == 3 {
122-
"3".to_owned()
123-
} else if n == 4 {
124-
"4".to_owned()
125-
} else if n == 5 {
126-
"5".to_owned()
127-
} else if n == 6 {
128-
"6".to_owned()
129-
} else if n == 7 {
130-
"7".to_owned()
131-
} else if n == 8 {
132-
"8".to_owned()
133-
} else if n == 9 {
134-
"9".to_owned()
135-
} else if n >= 10 {
136-
superscript(n / 10) + &superscript(n % 10)
137-
} else {
138-
n.to_string()
115+
match n {
116+
0 => "0".to_string(),
117+
1 => "1".to_string(),
118+
2 => "2".to_string(),
119+
3 => "3".to_string(),
120+
4 => "4".to_string(),
121+
5 => "5".to_string(),
122+
6 => "6".to_string(),
123+
7 => "7".to_string(),
124+
8 => "8".to_string(),
125+
9 => "9".to_string(),
126+
x if x > 10 => (superscript(n / 10).parse().unwrap_or(0)
127+
+ &superscript(n % 10).parse().unwrap_or(0))
128+
.to_string(),
129+
_ => n.to_string(),
139130
}
140131
}
141132

142133
pub fn subscript(n: u8) -> String {
143-
if n >= 10 {
144-
subscript(n / 10) + &subscript(n % 10)
145-
} else if n == 0 {
146-
"0".to_owned()
147-
} else if n == 1 {
148-
"1".to_owned()
149-
} else if n == 2 {
150-
"2".to_owned()
151-
} else if n == 3 {
152-
"3".to_owned()
153-
} else if n == 4 {
154-
"4".to_owned()
155-
} else if n == 5 {
156-
"5".to_owned()
157-
} else if n == 6 {
158-
"6".to_owned()
159-
} else if n == 7 {
160-
"7".to_owned()
161-
} else if n == 8 {
162-
"8".to_owned()
163-
} else if n == 9 {
164-
"9".to_owned()
165-
} else {
166-
n.to_string()
134+
match n {
135+
x if x >= 10 => (subscript(n / 10).parse().unwrap_or(0)
136+
+ &subscript(n % 10).parse().unwrap_or(0))
137+
.to_string(),
138+
0 => "0".to_string(),
139+
1 => "1".to_string(),
140+
2 => "2".to_string(),
141+
3 => "3".to_string(),
142+
4 => "4".to_string(),
143+
5 => "5".to_string(),
144+
6 => "6".to_string(),
145+
7 => "7".to_string(),
146+
8 => "8".to_string(),
147+
9 => "9".to_string(),
148+
_ => n.to_string(),
167149
}
168150
}
169151
impl HTML for String {
@@ -179,6 +161,12 @@ mod html {
179161
let mut sup = false;
180162
let mut sub = false;
181163
let mut color: Option<Color> = None;
164+
165+
// TODO: check how to make this `unwrap` more flexible..
166+
//
167+
// or looks better.
168+
//
169+
// or do some handwrite matching.
182170
let re_color = Regex::new(r#"color=['"]([^'"]+)"#).unwrap();
183171
for (i, e) in tks.chars().enumerate() {
184172
match e {

‎src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//! **Please make sure you have logined in `leetcode.com` with `chrome`**, more info plz checkout [this](#cookies)
2929
//!
3030
//! ```sh
31-
//! leetcode 0.2.22
31+
//! leetcode 0.2.23
3232
//! May the Code be with You 👻
3333
//!
3434
//! USAGE:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /