From e23dcc8fef791fc5542c1e4a73aae74ed26c75ba Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Tue, 7 Mar 2017 11:20:48 +0100 Subject: [PATCH] [spell check] avoid multiple replace by adding prev and next char to sed replace command --- scripts/spell_check/check_spelling.sh | 23 +++++++++++++++-------- scripts/spell_check/spell_test.sh | 2 +- scripts/spell_check/test.sh | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/scripts/spell_check/check_spelling.sh b/scripts/spell_check/check_spelling.sh index 3f2a87fc039..fc2549ffabb 100755 --- a/scripts/spell_check/check_spelling.sh +++ b/scripts/spell_check/check_spelling.sh @@ -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}"')' 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 - echo "$LINE" + echo -e "$LINE" ERRORFOUND=YES NOCOLOR=$(echo "$LINE" | ${GP}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g') if [[ "$NOCOLOR" =~ ^[[:alnum:]][[:alnum:]\/\._-]+$ ]]; then @@ -141,7 +141,9 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do fi NUMBER=$(echo "$NOCOLOR" | cut -d: -f1) 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') if [[ "$ERRORNOCOLOR" =~ ^[[:digit:]]+: ]]; then @@ -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') if [[ ! "${ERRORSMALLCASE}" =~ $IGNORECASE_INWORD ]]; 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#?} ERROR=${ERROR#?} fi 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} ERROR=${ERROR::-1} fi @@ -177,7 +183,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do # Skip global replace 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" - ${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 elif [[ ( -n ${GLOBREP_CURRENTFILE["$ERROR"]} ) || ( -n ${GLOBREP_IGNORE["$ERROR"]} ) ]]; then echo "skipping occurrence" @@ -190,6 +196,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do fi if [[ "$FILE" =~ \.(h|cpp|sip)$ ]]; then if [[ "$ERRORLINE" =~ ^\s*(\/*\|\/\/) ]]; then + # line is already commented SPELLOKSTR='#spellok' fi fi @@ -215,20 +222,20 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do case $n in 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" - ${GP}sed -i "${NUMBER}s/$ERROR/$CORRECTIONCASE/g" $FILE + ${GP}sed -i "${NUMBER}s/${PREVCHAR}${ERROR}${NEXTCHAR}/${PREVCHAR}$CORRECTIONCASE${NEXTCHAR}/g" $FILE break ;; f) GLOBREP_CURRENTFILE+=(["$ERROR"]=1) 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 ;; a) GLOBREP_CURRENTFILE+=(["$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" - ${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 ;; p) @@ -242,7 +249,7 @@ for I in $(seq -f '%02g' 0 $(($SPLIT-1)) ) ; do 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) 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 ;; c) diff --git a/scripts/spell_check/spell_test.sh b/scripts/spell_check/spell_test.sh index c4bf0a6fa33..ee49ea862f4 100755 --- a/scripts/spell_check/spell_test.sh +++ b/scripts/spell_check/spell_test.sh @@ -15,7 +15,7 @@ elif [[ ! -z $TRAVIS_COMMIT_RANGE ]]; then FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' ) export PATH=${HOME}/osgeo4travis/bin:${PATH} else - echo "Runnng spell test on all files (might take a while)" + echo "Running spell test on all files (might take a while)" FILES="" fi diff --git a/scripts/spell_check/test.sh b/scripts/spell_check/test.sh index e4d48e22080..3aaaac84b80 100755 --- a/scripts/spell_check/test.sh +++ b/scripts/spell_check/test.sh @@ -14,6 +14,8 @@ # # ########################################################################### +# Testing the spell test :) + # GNU prefix command for mac os support (gsed, gsplit) GP= if [[ "$OSTYPE" =~ darwin* ]]; then