| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # benchmarks/compute/palindrome.sh unicode
|
| 5 | # benchmarks/compute/palindrome.sh bytes
|
| 6 |
|
| 7 | #shopt -s globasciiranges
|
| 8 |
|
| 9 | main() {
|
| 10 | if test "$1" = "unicode"; then
|
| 11 | # Redirects don't work yet in Oil!
|
| 12 | #echo "palindrome.sh: unicode" 1>&2
|
| 13 | true
|
| 14 | else
|
| 15 | LANG=C
|
| 16 | fi
|
| 17 |
|
| 18 | while read -r line; do
|
| 19 | local n=${#line}
|
| 20 |
|
| 21 | if test $n -eq 0; then # skip blank lines
|
| 22 | continue
|
| 23 | fi
|
| 24 |
|
| 25 | h=$((n / 2)) # floor division
|
| 26 | local palindrome=T
|
| 27 | for (( i = 0; i < h; ++i )); do
|
| 28 | #echo ${line:i:1} ${line:n-1-i:1}
|
| 29 | if test "${line:i:1}" != "${line:n-1-i:1}"; then
|
| 30 | palindrome=''
|
| 31 | fi
|
| 32 | done
|
| 33 |
|
| 34 | if test -n "$palindrome"; then
|
| 35 | printf '%s\n' "$line"
|
| 36 | fi
|
| 37 | done
|
| 38 | }
|
| 39 |
|
| 40 | main "$@"
|