ref: 900fd8cabc919740344092a7318c9a4c357c5f5e
parent: c20ac350746b318e6399b1bb11d1ad7800263bef
author: ISSOtm <eldredhabert0@gmail.com>
date: Fri Dec 25 20:44:45 EST 2020
Improve gbdiff script Use better constructs where possible - <<< instead of echo| - Parameter expansions instead of `cut` - Etc. Improves performance and reliability somewhat Also, accept "d" between diff line info parts
--- a/contrib/gbdiff.bash
+++ b/contrib/gbdiff.bash
@@ -31,29 +31,29 @@
# Separator between files switches states
echo $LINE
STATE=3
- elif echo $LINE | grep -Eq '^[0-9]+(,[0-9]+)?c[0-9]+(,[0-9]+)?'; then
+ elif grep -Eq '^[0-9]+(,[0-9]+)?[cd][0-9]+(,[0-9]+)?' <<< "$LINE"; then
# Line info resets the whole thing
STATE=1
elif [ $STATE -eq 1 -o $STATE -eq 3 ]; then
# Compute the GB address from the ROM offset
- OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':')
+ OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
BANK=$((0x$OFS / 0x4000))
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
# Try finding the preceding symbol closest to the diff
if [ $STATE -eq 1 ]; then
STATE=2
- SYMFILE=$(echo $1 | cut -d '.' -f 1).sym
+ SYMFILE=${1%.*}.sym
else
STATE=4
- SYMFILE=$(echo $2 | cut -d '.' -f 1).sym
+ SYMFILE=${2%.*}.sym
fi
EXTRA=$(if [ -f "$SYMFILE" ]; then
# Read the sym file for such a symbol
# Ignore comment lines, only pick matching bank
- grep -Fv ';' "$SYMFILE" |
+ cut -d ';' -f 1 "$SYMFILE" |
grep -Ei $(printf "^%02x:" $BANK) |
while read -r SYMADDR SYM; do
- SYMADDR=$((0x$(echo $SYMADDR | cut -d ':' -f 2)))
+ SYMADDR=$((0x${SYMADDR#*:}))
if [ $SYMADDR -le $ADDR ]; then
printf " (%s+%#x)\n" $SYM $(($ADDR - $SYMADDR))
fi
@@ -63,9 +63,9 @@
printf "%02x:%04x %s\n" $BANK $ADDR $EXTRA
fi
if [ $STATE -eq 2 -o $STATE -eq 4 ]; then
- OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':')
+ OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
BANK=$((0x$OFS / 0x4000))
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
- printf "%s %02x:%04x: %s\n" "$(echo $LINE | cut -d ' ' -f 1)" $BANK $ADDR "$(echo $LINE | cut -d ' ' -f 3-)"
+ printf "%s %02x:%04x: %s\n" "${LINE:0:1}" $BANK $ADDR "${LINE#*: }"
fi
done