Skip to content

Commit b5e643b

Browse files
committed
ptx: add translation and avoid cloning
1 parent 4e7785a commit b5e643b

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

src/uu/ptx/locales/en-US.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ ptx-error-dumb-format = There is no dumb format with GNU extensions disabled
2828
ptx-error-not-implemented = { $feature } not implemented yet
2929
ptx-error-write-failed = write failed
3030
ptx-error-extra-operand = extra operand { $operand }
31+
ptx-error-empty-regexp = A regular expression cannot match a length zero string
32+
ptx-error-invalid-regexp = Invalid regexp: { $error }

src/uu/ptx/src/ptx.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,30 +205,27 @@ enum PtxError {
205205

206206
impl UError for PtxError {}
207207

208-
fn get_config(matches: &clap::ArgMatches) -> UResult<Config> {
208+
fn get_config(matches: &mut clap::ArgMatches) -> UResult<Config> {
209209
let mut config = Config::default();
210210
let err_msg = "parsing options failed";
211211
if matches.get_flag(options::TRADITIONAL) {
212212
config.gnu_ext = false;
213213
config.format = OutFormat::Roff;
214214
"[^ \t\n]+".clone_into(&mut config.context_regex);
215215
}
216-
if let Some(regex) = matches.get_one::<String>(options::SENTENCE_REGEXP) {
217-
config.sentence_regex = Some(regex.clone());
218-
216+
if let Some(regex) = matches.remove_one::<String>(options::SENTENCE_REGEXP) {
219217
// TODO: The regex crate used here is not fully compatible with GNU's regex implementation.
220218
// For example, it does not support backreferences.
221219
// In the future, we might want to switch to the onig crate (like expr does) for better compatibility.
222220

223221
// Verify regex is valid and doesn't match empty string
224-
if let Ok(re) = Regex::new(regex) {
222+
if let Ok(re) = Regex::new(&regex) {
225223
if re.is_match("") {
226-
return Err(USimpleError::new(
227-
1,
228-
"A regular expression cannot match a length zero string",
229-
));
224+
return Err(USimpleError::new(1, translate!("ptx-error-empty-regexp")));
230225
}
231226
}
227+
228+
config.sentence_regex = Some(regex);
232229
}
233230
config.auto_ref = matches.get_flag(options::AUTO_REFERENCE);
234231
config.input_ref = matches.get_flag(options::REFERENCES);
@@ -288,14 +285,16 @@ fn read_input(input_files: &[OsString], config: &Config) -> std::io::Result<File
288285
let mut file_map: FileMap = HashMap::new();
289286
let mut offset: usize = 0;
290287

291-
let sentence_splitter =
292-
if let Some(re_str) = &config.sentence_regex {
293-
Some(Regex::new(re_str).map_err(|_| {
294-
std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid regex")
295-
})?)
296-
} else {
297-
None
298-
};
288+
let sentence_splitter = if let Some(re_str) = &config.sentence_regex {
289+
Some(Regex::new(re_str).map_err(|e| {
290+
std::io::Error::new(
291+
std::io::ErrorKind::InvalidInput,
292+
translate!("ptx-error-invalid-regexp", "error" => e),
293+
)
294+
})?)
295+
} else {
296+
None
297+
};
299298

300299
for filename in input_files {
301300
let mut reader: BufReader<Box<dyn Read>> = BufReader::new(if filename == "-" {
@@ -878,8 +877,8 @@ mod options {
878877

879878
#[uucore::main]
880879
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
881-
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
882-
let mut config = get_config(&matches)?;
880+
let mut matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
881+
let mut config = get_config(&mut matches)?;
883882

884883
let input_files;
885884
let output_file: OsString;

0 commit comments

Comments
 (0)