Skip to content

Merge #1007 (regex support for expr) with latest master #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 71 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ yes = { optional=true, path="src/yes" }
time = "0.1.38"
filetime = "0.1.10"
libc = "0.2.26"
regex = "0.1.80"
regex = "0.2.2"
rand = "0.3.15"
tempdir = "0.3.5"
unindent = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Utilities
| Done | Semi-Done | To Do |
|-----------|-----------|--------|
| arch | cp | chcon |
| base32 | expr (no regular expressions) | csplit |
| base32 | expr | csplit |
| base64 | install | dd |
| basename | ls | df |
| cat | more | join |
Expand Down
11 changes: 11 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
os: Visual Studio 2015

environment:
matrix:
- TARGET: x86_64-pc-windows-msvc
MSYS_BITS: 64
TOOLCHAIN: msvc
PLATFORM: x86_64
- TARGET: i686-pc-windows-msvc
MSYS_BITS: 32
TOOLCHAIN: msvc
PLATFORM: i686
- TARGET: i686-pc-windows-gnu
MINGW_URL: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/4.9.2/threads-win32/dwarf/i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z/download
MINGW_ARCHIVE: i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z
Expand Down Expand Up @@ -33,6 +41,9 @@ install:

- rustc -V
- cargo -V
- if "%TOOLCHAIN%" == "msvc" if "%PLATFORM%" == "i686" call "%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat"
- if "%TOOLCHAIN%" == "msvc" if "%PLATFORM%" == "x86_64" "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64
- if "%TOOLCHAIN%" == "msvc" if "%PLATFORM%" == "x86_64" call "%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64

artifacts:
- path: target\debug\uutils.exe
Expand Down
2 changes: 1 addition & 1 deletion src/date/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "uu_date"
path = "date.rs"

[dependencies]
chrono = "0.3.0"
chrono = "0.4.0"
clap = "2.24.1"
uucore = { path="../uucore" }

Expand Down
4 changes: 2 additions & 2 deletions src/date/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern crate clap;
extern crate uucore;

use chrono::{DateTime, FixedOffset, Offset, Local};
use chrono::offset::utc::UTC;
use chrono::offset::Utc;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
// Get the current time, either in the local time zone or UTC.
let now: DateTime<FixedOffset> = match settings.utc {
true => {
let now = UTC::now();
let now = Utc::now();
now.with_timezone(&now.offset().fix())
}
false => {
Expand Down
1 change: 1 addition & 0 deletions src/expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ path = "expr.rs"

[dependencies]
libc = "0.2.26"
onig = "1.3.0"
uucore = { path="../uucore" }

[[bin]]
Expand Down
5 changes: 3 additions & 2 deletions src/expr/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#[macro_use]
extern crate uucore;
extern crate onig;

mod tokens;
mod syntax_tree;
Expand Down Expand Up @@ -95,9 +96,9 @@ separates increasing precedence groups. EXPRESSION may be:
ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2
ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2

STRING : REGEXP [NOT IMPLEMENTED] anchored pattern match of REGEXP in STRING
STRING : REGEXP anchored pattern match of REGEXP in STRING

match STRING REGEXP [NOT IMPLEMENTED] same as STRING : REGEXP
match STRING REGEXP same as STRING : REGEXP
substr STRING POS LENGTH substring of STRING, POS counted from 1
index STRING CHARS index in STRING where any CHARS is found, or 0
length STRING length of STRING
Expand Down
21 changes: 21 additions & 0 deletions src/expr/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//!

use tokens::{Token};
use onig::{Regex, Syntax, REGEX_OPTION_NONE};

type TokenStack = Vec<(usize, Token)>;
pub type OperandsList = Vec< Box<ASTNode> >;
Expand Down Expand Up @@ -105,6 +106,7 @@ impl ASTNode {
),
"|" => infix_operator_or(&operand_values),
"&" => infix_operator_and(&operand_values),
":" | "match" => operator_match(&operand_values),
"length" => prefix_operator_length( &operand_values ),
"index" => prefix_operator_index( &operand_values ),
"substr" => prefix_operator_substr( &operand_values ),
Expand Down Expand Up @@ -350,6 +352,25 @@ fn infix_operator_and( values: &Vec<String> ) -> Result<String, String> {
}
}

fn operator_match(values: &Vec<String>) -> Result<String, String> {
assert!(values.len() == 2);
let re = match Regex::with_options(&values[1], REGEX_OPTION_NONE, Syntax::grep()) {
Ok(m) => m,
Err(err) => return Err(err.description().to_string())
};
if re.captures_len() > 0 {
Ok(match re.captures(&values[0]) {
Some(captures) => captures.at(1).unwrap().to_string(),
None => "".to_string()
})
} else {
Ok(match re.find(&values[0]) {
Some((start, end)) => (end - start).to_string(),
None => "0".to_string()
})
}
}

fn prefix_operator_length( values: &Vec<String> ) -> Result<String, String> {
assert!( values.len() == 1 );
Ok( values[0].len().to_string() )
Expand Down
2 changes: 1 addition & 1 deletion src/hashsum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ path = "hashsum.rs"
[dependencies]
getopts = "0.2.14"
libc = "0.2.26"
regex = "0.1.80"
regex = "0.2.2"
regex-syntax = "0.4.1"
rust-crypto = "0.2.36"
rustc-serialize = "0.3.24"
Expand Down
10 changes: 5 additions & 5 deletions src/hashsum/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ fn hashsum(algoname: &str, mut digest: Box<Digest>, files: Vec<String>, binary:
for (i, line) in buffer.lines().enumerate() {
let line = safe_unwrap!(line);
let (ck_filename, sum, binary_check) = match gnu_re.captures(&line) {
Some(caps) => (caps.name("fileName").unwrap(),
caps.name("digest").unwrap().to_ascii_lowercase(),
caps.name("binary").unwrap() == "*"),
Some(caps) => (caps.name("fileName").unwrap().as_str(),
caps.name("digest").unwrap().as_str().to_ascii_lowercase(),
caps.name("binary").unwrap().as_str() == "*"),
None => match bsd_re.captures(&line) {
Some(caps) => (caps.name("fileName").unwrap(),
caps.name("digest").unwrap().to_ascii_lowercase(),
Some(caps) => (caps.name("fileName").unwrap().as_str(),
caps.name("digest").unwrap().as_str().to_ascii_lowercase(),
true),
None => {
bad_format += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/ptx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ libc = "0.2.26"
aho-corasick = "0.6.3"
memchr = "1.0.1"
regex-syntax = "0.4.1"
regex = "0.1.80"
regex = "0.2.2"
uucore = { path="../uucore" }

[[bin]]
Expand Down
11 changes: 6 additions & 5 deletions src/ptx/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,12 @@ fn create_word_set(config: &Config, filter: &WordFilter,
for line in &lines.0 {
// if -r, exclude reference from word set
let (ref_beg, ref_end) = match ref_reg.find(line) {
Some(x) => x,
None => (0,0)
Some(x) => (x.start(), x.end()),
None => (0, 0)
};
// match words with given regex
for (beg, end) in reg.find_iter(line) {
for mat in reg.find_iter(line) {
let (beg, end) = (mat.start(), mat.end());
if config.input_ref && ((beg, end) == (ref_beg, ref_end)) {
continue;
}
Expand Down Expand Up @@ -279,8 +280,8 @@ fn get_reference(config: &Config, word_ref: &WordRef, line: &str) ->
} else if config.input_ref {
let reg = Regex::new(&config.context_regex).unwrap();
let (beg, end) = match reg.find(line) {
Some(x) => x,
None => (0,0)
Some(x) => (x.start(), x.end()),
None => (0, 0)
};
format!("{}", &line[beg .. end])
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl RandomFile {
fn test_split_default() {
let (at, mut ucmd) = at_and_ucmd!();
let name = "split_default";
let glob = Glob::new(&at, ".", r"x[:alpha:][:alpha:]$");
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
RandomFile::new(&at, name).add_lines(2000);
ucmd.args(&[name]).succeeds();
assert_eq!(glob.count(), 2);
Expand All @@ -115,7 +115,7 @@ fn test_split_num_prefixed_chunks_by_bytes() {
fn test_split_str_prefixed_chunks_by_bytes() {
let (at, mut ucmd) = at_and_ucmd!();
let name = "split_str_prefixed_chunks_by_bytes";
let glob = Glob::new(&at, ".", r"b[:alpha:][:alpha:]$");
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$");
RandomFile::new(&at, name).add_bytes(10000);
ucmd.args(&["-b", "1000", name, "b"]).succeeds();
assert_eq!(glob.count(), 10);
Expand All @@ -137,7 +137,7 @@ fn test_split_num_prefixed_chunks_by_lines() {
fn test_split_str_prefixed_chunks_by_lines() {
let (at, mut ucmd) = at_and_ucmd!();
let name = "split_str_prefixed_chunks_by_lines";
let glob = Glob::new(&at, ".", r"d[:alpha:][:alpha:]$");
let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$");
RandomFile::new(&at, name).add_lines(10000);
ucmd.args(&["-l", "1000", name, "d"]).succeeds();
assert_eq!(glob.count(), 10);
Expand Down