Skip to content

Commit 75e75ba

Browse files
nyuriknickorlow
authored andcommitted
Merge pull request uutils#7702 from nyurik/iters
feat: optimize iter matching
1 parent 888eddd commit 75e75ba

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

src/uu/chroot/src/chroot.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,20 @@ struct Options {
5353
///
5454
/// The `spec` must be of the form `[USER][:[GROUP]]`, otherwise an
5555
/// error is returned.
56-
fn parse_userspec(spec: &str) -> UResult<UserSpec> {
57-
match &spec.splitn(2, ':').collect::<Vec<&str>>()[..] {
56+
fn parse_userspec(spec: &str) -> UserSpec {
57+
match spec.split_once(':') {
5858
// ""
59-
[""] => Ok(UserSpec::NeitherGroupNorUser),
59+
None if spec.is_empty() => UserSpec::NeitherGroupNorUser,
6060
// "usr"
61-
[usr] => Ok(UserSpec::UserOnly(usr.to_string())),
61+
None => UserSpec::UserOnly(spec.to_string()),
6262
// ":"
63-
["", ""] => Ok(UserSpec::NeitherGroupNorUser),
63+
Some(("", "")) => UserSpec::NeitherGroupNorUser,
6464
// ":grp"
65-
["", grp] => Ok(UserSpec::GroupOnly(grp.to_string())),
65+
Some(("", grp)) => UserSpec::GroupOnly(grp.to_string()),
6666
// "usr:"
67-
[usr, ""] => Ok(UserSpec::UserOnly(usr.to_string())),
67+
Some((usr, "")) => UserSpec::UserOnly(usr.to_string()),
6868
// "usr:grp"
69-
[usr, grp] => Ok(UserSpec::UserAndGroup(usr.to_string(), grp.to_string())),
70-
// everything else
71-
_ => Err(ChrootError::InvalidUserspec(spec.to_string()).into()),
69+
Some((usr, grp)) => UserSpec::UserAndGroup(usr.to_string(), grp.to_string()),
7270
}
7371
}
7472

@@ -144,10 +142,9 @@ impl Options {
144142
}
145143
};
146144
let skip_chdir = matches.get_flag(options::SKIP_CHDIR);
147-
let userspec = match matches.get_one::<String>(options::USERSPEC) {
148-
None => None,
149-
Some(s) => Some(parse_userspec(s)?),
150-
};
145+
let userspec = matches
146+
.get_one::<String>(options::USERSPEC)
147+
.map(|s| parse_userspec(s));
151148
Ok(Self {
152149
newroot,
153150
skip_chdir,

src/uu/chroot/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ pub enum ChrootError {
3434
#[error("invalid group list: {list}", list = .0.quote())]
3535
InvalidGroupList(String),
3636

37-
/// The given user and group specification was invalid.
38-
#[error("invalid userspec: {spec}", spec = .0.quote())]
39-
InvalidUserspec(String),
40-
4137
/// The new root directory was not given.
4238
#[error(
4339
"Missing operand: NEWROOT\nTry '{0} --help' for more information.",

src/uu/nproc/src/nproc.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) NPROCESSORS nprocs numstr threadstr sysconf
6+
// spell-checker:ignore (ToDO) NPROCESSORS nprocs numstr sysconf
77

88
use clap::{Arg, ArgAction, Command};
99
use std::{env, thread};
@@ -47,7 +47,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4747
// Uses the OpenMP variable to limit the number of threads
4848
// If the parsing fails, returns the max size (so, no impact)
4949
// If OMP_THREAD_LIMIT=0, rejects the value
50-
Ok(threadstr) => match threadstr.parse() {
50+
Ok(threads) => match threads.parse() {
5151
Ok(0) | Err(_) => usize::MAX,
5252
Ok(n) => n,
5353
},
@@ -63,14 +63,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6363
match env::var("OMP_NUM_THREADS") {
6464
// Uses the OpenMP variable to force the number of threads
6565
// If the parsing fails, returns the number of CPU
66-
Ok(threadstr) => {
66+
Ok(threads) => {
6767
// In some cases, OMP_NUM_THREADS can be "x,y,z"
6868
// In this case, only take the first one (like GNU)
6969
// If OMP_NUM_THREADS=0, rejects the value
70-
let thread: Vec<&str> = threadstr.split_terminator(',').collect();
71-
match &thread[..] {
72-
[] => available_parallelism(),
73-
[s, ..] => match s.parse() {
70+
match threads.split_terminator(',').next() {
71+
None => available_parallelism(),
72+
Some(s) => match s.parse() {
7473
Ok(0) | Err(_) => available_parallelism(),
7574
Ok(n) => n,
7675
},

src/uu/split/src/strategy.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl NumberType {
108108
/// # Errors
109109
///
110110
/// If the string is not one of the valid number types,
111-
/// if `K` is not a nonnegative integer,
111+
/// if `K` is not a non-negative integer,
112112
/// or if `K` is 0,
113113
/// or if `N` is not a positive integer,
114114
/// or if `K` is greater than `N`
@@ -117,9 +117,9 @@ impl NumberType {
117117
fn is_invalid_chunk(chunk_number: u64, num_chunks: u64) -> bool {
118118
chunk_number > num_chunks || chunk_number == 0
119119
}
120-
let parts: Vec<&str> = s.split('/').collect();
121-
match &parts[..] {
122-
[n_str] => {
120+
let mut parts = s.splitn(4, '/');
121+
match (parts.next(), parts.next(), parts.next(), parts.next()) {
122+
(Some(n_str), None, None, None) => {
123123
let num_chunks = parse_size_u64(n_str)
124124
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
125125
if num_chunks > 0 {
@@ -128,7 +128,9 @@ impl NumberType {
128128
Err(NumberTypeError::NumberOfChunks(s.to_string()))
129129
}
130130
}
131-
[k_str, n_str] if !k_str.starts_with('l') && !k_str.starts_with('r') => {
131+
(Some(k_str), Some(n_str), None, None)
132+
if !k_str.starts_with('l') && !k_str.starts_with('r') =>
133+
{
132134
let num_chunks = parse_size_u64(n_str)
133135
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
134136
let chunk_number = parse_size_u64(k_str)
@@ -138,12 +140,12 @@ impl NumberType {
138140
}
139141
Ok(Self::KthBytes(chunk_number, num_chunks))
140142
}
141-
["l", n_str] => {
143+
(Some("l"), Some(n_str), None, None) => {
142144
let num_chunks = parse_size_u64(n_str)
143145
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
144146
Ok(Self::Lines(num_chunks))
145147
}
146-
["l", k_str, n_str] => {
148+
(Some("l"), Some(k_str), Some(n_str), None) => {
147149
let num_chunks = parse_size_u64(n_str)
148150
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
149151
let chunk_number = parse_size_u64(k_str)
@@ -153,12 +155,12 @@ impl NumberType {
153155
}
154156
Ok(Self::KthLines(chunk_number, num_chunks))
155157
}
156-
["r", n_str] => {
158+
(Some("r"), Some(n_str), None, None) => {
157159
let num_chunks = parse_size_u64(n_str)
158160
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
159161
Ok(Self::RoundRobin(num_chunks))
160162
}
161-
["r", k_str, n_str] => {
163+
(Some("r"), Some(k_str), Some(n_str), None) => {
162164
let num_chunks = parse_size_u64(n_str)
163165
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
164166
let chunk_number = parse_size_u64(k_str)

0 commit comments

Comments
 (0)