Skip to content

Commit

Permalink
[spell check] avoid multiple replace by adding prev and next char to …
Browse files Browse the repository at this point in the history
…sed replace command
  • Loading branch information
3nids committed Mar 7, 2017
1 parent e7ea37d commit e23dcc8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
23 changes: 15 additions & 8 deletions scripts/spell_check/check_spelling.sh
Expand Up @@ -124,10 +124,10 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
IGNORECASE=$(echo "(${IGNORECASE_FIXSPECIALCHAR}|${IGNORECASE_INWORD}|${IGNORECASE_WHOLEWORD})" |${GP}sed -r 's/\(\|/(/' |${GP}sed -r 's/\|\|/|/g' |${GP}sed -r 's/\|\)/)/')'(?!.*'"${SPELLOKRX}"')' IGNORECASE=$(echo "(${IGNORECASE_FIXSPECIALCHAR}|${IGNORECASE_INWORD}|${IGNORECASE_WHOLEWORD})" |${GP}sed -r 's/\(\|/(/' |${GP}sed -r 's/\|\|/|/g' |${GP}sed -r 's/\|\)/)/')'(?!.*'"${SPELLOKRX}"')'
CASEMATCH=$(echo "(${CASEMATCH_FIXCASE}|${MATCHCASE_INWORD})" |${GP}sed -r 's/\(\|/(/' |${GP}sed -r 's/\|\|/|/g' |${GP}sed -r 's/\|\)/)/')'(?!.*'"${SPELLOKRX}"')' CASEMATCH=$(echo "(${CASEMATCH_FIXCASE}|${MATCHCASE_INWORD})" |${GP}sed -r 's/\(\|/(/' |${GP}sed -r 's/\|\|/|/g' |${GP}sed -r 's/\|\)/)/')'(?!.*'"${SPELLOKRX}"')'


FILE=$INPUTFILES # init with input files (if ag is run with single file, file path is now in output) FILE=$INPUTFILES # init with input files (if ag is run with single file, file path is not written in output)


while read -u 3 -r LINE; do while read -u 3 -r LINE; do
echo "$LINE" echo -e "$LINE"
ERRORFOUND=YES ERRORFOUND=YES
NOCOLOR=$(echo "$LINE" | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g') NOCOLOR=$(echo "$LINE" | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g')
if [[ "$NOCOLOR" =~ ^[[:alnum:]][[:alnum:]\/\._-]+$ ]]; then if [[ "$NOCOLOR" =~ ^[[:alnum:]][[:alnum:]\/\._-]+$ ]]; then
Expand All @@ -141,7 +141,9 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
fi fi
NUMBER=$(echo "$NOCOLOR" | cut -d: -f1) NUMBER=$(echo "$NOCOLOR" | cut -d: -f1)
ERRORLINE=$(echo "$NOCOLOR" | cut -d: -f2) ERRORLINE=$(echo "$NOCOLOR" | cut -d: -f2)
ERROR=$(echo "$LINE" | ${GP}sed -r 's/^.*?\x1B\[30;43m(.*?)\x1B\[0m.*$/\1/') ERROR=$(echo "$LINE" | ${GP}sed -r 's/^.*?\x1B\[30;43m(.*?)\x1B\[0m.*?$/\1/')
PREVCHAR=$(echo "$LINE" | cut -d: -f2 | ${GP}sed -r 's/^(.*?)\x1B\[30;43m.*?\x1B\[0m.*?$/\1/' | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | tail -c 2)
NEXTCHAR=$(echo "$LINE" | cut -d: -f2 | ${GP}sed -r 's/^.*?\x1B\[30;43m.*?\x1B\[0m(.*?)$/\1/' | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | head -c 1)


ERRORNOCOLOR=$(echo "$ERROR" | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g') ERRORNOCOLOR=$(echo "$ERROR" | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g')
if [[ "$ERRORNOCOLOR" =~ ^[[:digit:]]+: ]]; then if [[ "$ERRORNOCOLOR" =~ ^[[:digit:]]+: ]]; then
Expand All @@ -152,10 +154,14 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
ERRORSMALLCASE=$(echo ${ERROR,,} |${GP}sed -r 's/\(/\\(/g' |${GP}sed -r 's/\)/\\)/g' |${GP}sed -r 's/\|/\\|/g') ERRORSMALLCASE=$(echo ${ERROR,,} |${GP}sed -r 's/\(/\\(/g' |${GP}sed -r 's/\)/\\)/g' |${GP}sed -r 's/\|/\\|/g')
if [[ ! "${ERRORSMALLCASE}" =~ $IGNORECASE_INWORD ]]; then if [[ ! "${ERRORSMALLCASE}" =~ $IGNORECASE_INWORD ]]; then
if [[ -n $(ag --nonumbers --case-sensitive "^${ERRORSMALLCASE:1:-1}${ERRORSMALLCASE: -1}?:" scripts/spell_check/spelling.dat) ]]; then if [[ -n $(ag --nonumbers --case-sensitive "^${ERRORSMALLCASE:1:-1}${ERRORSMALLCASE: -1}?:" scripts/spell_check/spelling.dat) ]]; then
PREVCHAR=${ERROR::1}
# remove first character
ERRORSMALLCASE=${ERRORSMALLCASE#?} ERRORSMALLCASE=${ERRORSMALLCASE#?}
ERROR=${ERROR#?} ERROR=${ERROR#?}
fi fi
if [[ -n $(ag --nonumbers --case-sensitive "^${ERRORSMALLCASE::-1}:" scripts/spell_check/spelling.dat) ]]; then if [[ -n $(ag --nonumbers --case-sensitive "^${ERRORSMALLCASE::-1}:" scripts/spell_check/spelling.dat) ]]; then
NEXTCHAR=${ERROR:${#ERROR}-1:1}
# remove last character
ERRORSMALLCASE=${ERRORSMALLCASE::-1} ERRORSMALLCASE=${ERRORSMALLCASE::-1}
ERROR=${ERROR::-1} ERROR=${ERROR::-1}
fi fi
Expand All @@ -177,7 +183,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
# Skip global replace # Skip global replace
if [[ -n ${GLOBREP_ALLFILES["$ERROR"]} ]]; then if [[ -n ${GLOBREP_ALLFILES["$ERROR"]} ]]; then
echo -e "replace \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m" echo -e "replace \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m"
${GP}sed -i -r "/${SPELLOKRX}/! s/$ERROR/$CORRECTIONCASE/g" $FILE ${GP}sed -i -r "/${SPELLOKRX}/! s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE
continue continue
elif [[ ( -n ${GLOBREP_CURRENTFILE["$ERROR"]} ) || ( -n ${GLOBREP_IGNORE["$ERROR"]} ) ]]; then elif [[ ( -n ${GLOBREP_CURRENTFILE["$ERROR"]} ) || ( -n ${GLOBREP_IGNORE["$ERROR"]} ) ]]; then
echo "skipping occurrence" echo "skipping occurrence"
Expand All @@ -190,6 +196,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
fi fi
if [[ "$FILE" =~ \.(h|cpp|sip)$ ]]; then if [[ "$FILE" =~ \.(h|cpp|sip)$ ]]; then
if [[ "$ERRORLINE" =~ ^\s*(\/*\|\/\/) ]]; then if [[ "$ERRORLINE" =~ ^\s*(\/*\|\/\/) ]]; then
# line is already commented
SPELLOKSTR='#spellok' SPELLOKSTR='#spellok'
fi fi
fi fi
Expand All @@ -215,20 +222,20 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
case $n in case $n in
r) r)
echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m" echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m"
${GP}sed -i "${NUMBER}s/$ERROR/$CORRECTIONCASE/g" $FILE ${GP}sed -i "${NUMBER}s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE
break break
;; ;;
f) f)
GLOBREP_CURRENTFILE+=(["$ERROR"]=1) GLOBREP_CURRENTFILE+=(["$ERROR"]=1)
echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m" echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m"
${GP}sed -i -r "/${SPELLOKRX}/! s/$ERROR/$CORRECTIONCASE/g" $FILE ${GP}sed -i -r "/${SPELLOKRX}/! s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE
break break
;; ;;
a) a)
GLOBREP_CURRENTFILE+=(["$ERROR"]=1) GLOBREP_CURRENTFILE+=(["$ERROR"]=1)
GLOBREP_ALLFILES+=(["$ERROR"]=1) GLOBREP_ALLFILES+=(["$ERROR"]=1)
echo -e "replace \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m" echo -e "replace \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m"
${GP}sed -i -r "/${SPELLOKRX}/! s/$ERROR/$CORRECTIONCASE/g" $FILE ${GP}sed -i -r "/${SPELLOKRX}/! s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE
break break
;; ;;
p) p)
Expand All @@ -242,7 +249,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do
MATCHCASE="$ERROR:$CORRECTION" MATCHCASE="$ERROR:$CORRECTION"
CORRECTIONCASE=$(echo "$MATCHCASE" | ${GP}sed -r 's/([A-Z]+):(.*)/\1:\U\2/; s/([A-Z][a-z]+):([a-z])/\1:\U\2\L/' | cut -d: -f2) CORRECTIONCASE=$(echo "$MATCHCASE" | ${GP}sed -r 's/([A-Z]+):(.*)/\1:\U\2/; s/([A-Z][a-z]+):([a-z])/\1:\U\2\L/' | cut -d: -f2)
echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m" echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTIONCASE\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m"
${GP}sed -i "${NUMBER}s/$ERROR/$CORRECTIONCASE/g" $FILE ${GP}sed -i "${NUMBER}s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE
break break
;; ;;
c) c)
Expand Down
2 changes: 1 addition & 1 deletion scripts/spell_check/spell_test.sh
Expand Up @@ -15,7 +15,7 @@ elif [[ ! -z $TRAVIS_COMMIT_RANGE ]]; then
FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' ) FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' )
export PATH=${HOME}/osgeo4travis/bin:${PATH} export PATH=${HOME}/osgeo4travis/bin:${PATH}
else else
echo "Runnng spell test on all files (might take a while)" echo "Running spell test on all files (might take a while)"
FILES="" FILES=""
fi fi


Expand Down
2 changes: 2 additions & 0 deletions scripts/spell_check/test.sh
Expand Up @@ -14,6 +14,8 @@
# # # #
########################################################################### ###########################################################################


# Testing the spell test :)

# GNU prefix command for mac os support (gsed, gsplit) # GNU prefix command for mac os support (gsed, gsplit)
GP= GP=
if [[ "$OSTYPE" =~ darwin* ]]; then if [[ "$OSTYPE" =~ darwin* ]]; then
Expand Down

0 comments on commit e23dcc8

Please sign in to comment.