File: __str

package info (click to toggle)
mongo-c-driver 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,088 kB
  • sloc: ansic: 193,670; python: 7,780; cpp: 1,493; sh: 659; makefile: 78
file content (101 lines) | stat: -rwxr-xr-x 2,002 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash

# This is a tiny shell utility for string manipulation.
# Allow us to use "local"
# shellcheck disable=SC3043

set -eu

_HELP='Usage:
  str {lower,upper}
  str test

Commands:
  lower, upper
    Convert input (stdin) to all-lowercase or all-uppercase, respectively

  test <str1> (-ieq|-ine|-contains|-matches) <str2>
    Like "test", but with additional string comparisons:
      -ieq • case-insensitive equal
      -ine • case-insensitive not-equal
      -contains • Check if <str1> contains <str2>
      -matches • Check if <str1> matches pattern <str2> (A grep -E pattern)
'

fail() {
  # shellcheck disable=SC2059
  printf -- "$@" 1>&2
  printf -- "\n" 1>&2
  return 1
}

__str__upper() {
  __justStdin upper __upper "$@"
}
__upper() {
  tr '[:lower:]' '[:upper:]'
}

__str__lower() {
  __justStdin lower __lower "$@"
}
__lower() {
  tr '[:upper:]' '[:lower:]'
}

__justStdin() {
  if test $# -gt 2; then
    fail "Command '%s' does not take any arguments (write input into stdin)" "$1" || return
  fi
  "$2"
}

__str__help() {
  printf %s "$_HELP"
}
__str____help() {
  __str help
}
__str___h() {
  __str help
}
__str___help() {
  __str help
}

__str__test() {
  test "$#" -eq 3 || fail '“str test” expects three arguments (Got %d: “%s”)' $# "$*" \
    || return
  local lhs="$1"
  local op="$2"
  local rhs="$3"
  local norm_lhs norm_rhs;
  norm_lhs=$(echo "$lhs" | __str lower) || return
  norm_rhs=$(echo "$rhs" | __str lower) || return
  case $op in
    -ieq)
      test "$norm_lhs" = "$norm_rhs";;
    -ine)
      test "$norm_lhs" != "$norm_rhs";;
    -matches)
      printf %s "$lhs" | grep -qE -- "$rhs";;
    -contains)
      printf %s "$lhs" | grep -qF -- "$rhs";;
    -*|=*)
      # Just defer to the underlying test command
      test "$lhs" "$op" "$rhs"
  esac
}

__str() {
  local _Command="$1"
  local _CommandIdent
  _CommandIdent="$(echo "__str__$_Command" | sed '
    s/-/_/g
    s/\./__/g
  ')"
  shift
  "$_CommandIdent" "$@"
}

__str "$@"