Skip to content

Commit

Permalink
[demo] Perl and Python backtrack, but POSIX style engines don't.
Browse files Browse the repository at this point in the history
This was what I expected!
  • Loading branch information
Andy Chu committed Jul 3, 2020
1 parent f162537 commit f1eba2e
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions demo/regex-backtrack.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
#
# Demo for EggEx. Do any of these common engines backtrack?
#
# Usage:
# ./regex-backtrack.sh <function name>

Expand Down Expand Up @@ -47,27 +49,70 @@ egrep-task() {
local text=$1
local pattern=$2

echo -n 'eg '
echo "$text" | egrep "$pattern" || true
echo -n 'egrep '
echo "$text" | egrep "$pattern"
}

sed-task() {
local text=$1
local pattern=$2

echo -n 'sed '
echo "$text" | sed "/$pattern/p"
}

awk-task() {
local bin=$1
local text=$2
local pattern=$3

echo -n "$bin "
echo "$text" | $bin "/$pattern/ { print }"
}

mawk-task() { awk-task mawk "$@"; }
gawk-task() { awk-task gawk "$@"; }

libc-task() {
### bash is linked against libc

local text=$1
local pattern=$2

echo -n 'libc '
# note: pattern can't be quoted
[[ "$text" =~ $pattern ]] && echo $text
}

python-task() {
local text=$1
local pattern=$2

echo -n 'py '
echo -n 'py '
python -c '
import re, sys
pattern, text = sys.argv[1:]
#print(pattern)
#print(text)
# Assumed to match
print(re.match(pattern, text).group(0))
' "$pattern" "$text"
}

perl-task() {
local text=$1
local pattern=$2

echo -n 'perl '
echo "$text" | perl -n -e "print if /$pattern/"

# https://stackoverflow.com/questions/4794145/perl-one-liner-like-grep
}

benchmark() {
local max=${1:-20}
local max=${1:-22}

TIMEFORMAT='%U' # CPU seconds spent in user mode

Expand All @@ -76,7 +121,12 @@ benchmark() {
local text=$(text $i)

time egrep-task "$text" "$pattern"
time sed-task "$text" "$pattern"
time libc-task "$text" "$pattern"
time gawk-task "$text" "$pattern"
time mawk-task "$text" "$pattern"
time python-task "$text" "$pattern"
time perl-task "$text" "$pattern"
echo
done
}
Expand Down

0 comments on commit f1eba2e

Please sign in to comment.