Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added case awareness option

Added a `case|no-case` flag to `is_in_class` and `match` to specify
explit case handling. Fixes `is_lower` and `is_upper` returning wrong
results when `nocasematch` is set.
  • Loading branch information...
commit eee25f7f7b645b24a8554a6813b850102b135fd8 1 parent 31924fb
@kopischke authored
Showing with 38 additions and 8 deletions.
  1. +38 −8 src/lib/strings.sh
View
46 src/lib/strings.sh
@@ -1,6 +1,41 @@
function is_in_class {
+ local casematch=$(shopt -p nocasematch)
+ case "$1" in
+ --case)
+ shopt -u nocasematch
+ shift ;;
+ --no-case)
+ shopt -s nocasematch
+ shift ;;
+ *)
+ casematch='' ;;
+ esac
+
local regex="[^$1]"
- [[ $2 =~ $regex ]] && return 1 || return 0
+ local retval=0
+ [[ -z "$2" || $2 =~ $regex ]] && retval=1
+ [[ -n "$casematch" ]] && $casematch
+ return $retval
+}
+
+function match {
+ local casematch=$(shopt -p nocasematch)
+ case "$1" in
+ --case)
+ shopt -u nocasematch
+ shift ;;
+ --no-case)
+ shopt -s nocasematch
+ shift ;;
+ *)
+ casematch='' ;;
+ esac
+
+ local regex="$1"
+ local retval=0
+ [[ $2 =~ $regex ]] && echo "${BASH_REMATCH[0]}" || retval=$?
+ [[ -n "$casematch" ]] && $casematch
+ return $retval
}
function is_7bit {
@@ -13,11 +48,11 @@ function is_blank {
}
function is_upper {
- is_in_class '[:upper:]' "$1"
+ is_in_class --case '[:upper:]' "$1"
}
function is_lower {
- is_in_class '[:lower:]' "$1"
+ is_in_class --case '[:lower:]' "$1"
}
function is_mixed {
@@ -32,11 +67,6 @@ function utf8_normalize {
echo "$1" | iconv -s -f UTF-8-Mac -t UTF-8
}
-function match {
- local regex="$1"
- [[ $2 =~ $regex ]] && echo "${BASH_REMATCH[0]}"
-}
-
function ltrim {
local to_trim="${2:-[:space:]}"
match "[^$to_trim].*$" "$1"
Please sign in to comment.
Something went wrong with that request. Please try again.