Skip to content

Commit

Permalink
introduce escapeRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Feb 14, 2025
1 parent a711c39 commit 7d9206e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ body:
- utility/recursive-declare-p.sh
- utility/replace-snippet.sh
- utility/replace-help-snippet.sh
- utility/string-utils.sh
- utility/source-once.sh
- utility/update-bash-docu.sh
- setup.sh
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ The scripts are ordered by topic:
- [Recursive `declare -p`](#recursive-declare--p)
- [Replace Snippets](#replace-snippets)
- [`source` once](#source-once)
- [string utils](#string-utils)
- [Update Documentation](#update-bash-documentation)
# Continuous Integration
Expand Down Expand Up @@ -2050,6 +2051,35 @@ printf -v "$guard" "%s" "true"
</utility-source-once>
## string utils
String processing utils
<utility-string-utils>
<!-- auto-generated, do not modify here but in src/utility/string-utils.sh.doc -->
```bash
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
# Assumes tegonal's scripts were fetched with gt - adjust location accordingly
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
source "$dir_of_tegonal_scripts/utility/string-utils.sh"
# will output v4\.2\.0
escapeRegex "v4.2.0"
# useful in combination with grep which does not support literal searches:
# escapes to tegonal\+
pattern=$(escapeRegex "tegonal+")
grep -E "$pattern"
```
</utility-string-utils>
## Update Bash documentation
Updates the `Usage` section of a bash file based on a sibling doc which is named *.doc.sh (e.g foo.sh and foo.doc.sh).
Expand Down
5 changes: 1 addition & 4 deletions src/utility/parse-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
# Version: v4.4.0-SNAPSHOT
####### Description #############
#
# Intended to parse command line arguments. Provides a simple way to parse named arguments including a documentation
# if one uses the parameter `--help` and shows the version if one uses --version.
# I.e. that also means that `--help` and `--version` are reserved patterns and should not be used by your
# script/function.
# Utility functions for argument parser like function such as parse-args and parse-fn-args
#
####### Usage ###################
#
Expand Down
16 changes: 16 additions & 0 deletions src/utility/string-utils.doc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
# Assumes tegonal's scripts were fetched with gt - adjust location accordingly
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"

source "$dir_of_tegonal_scripts/utility/string-utils.sh"

# will output v4\.2\.0
escapeRegex "v4.2.0"

# useful in combination with grep which does not support literal searches:
# escapes to tegonal\+
pattern=$(escapeRegex "tegonal+")
grep -E "$pattern"
52 changes: 52 additions & 0 deletions src/utility/string-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
#
# __ __
# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts
# / __/ -_) _ `/ _ \/ _ \/ _ `/ / It is licensed under Apache License 2.0
# \__/\__/\_, /\___/_//_/\_,_/_/ Please report bugs and contribute back your improvements
# /___/
# Version: v4.4.0-SNAPSHOT
#
####### Description #############
#
# utility functions for processing strings
#
####### Usage ###################
#
# #!/usr/bin/env bash
# set -euo pipefail
# shopt -s inherit_errexit
# # Assumes tegonal's scripts were fetched with gt - adjust location accordingly
# dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
# source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
#
# source "$dir_of_tegonal_scripts/utility/string-utils.sh"
#
# # will output v4\.2\.0
# escapeRegex "v4.2.0"
#
# # useful in combination with grep which does not support literal searches:
# # escapes to tegonal\+
# pattern=$(escapeRegex "tegonal+")
# grep -E "$pattern"
#
###################################
set -euo pipefail
shopt -s inherit_errexit
unset CDPATH

if ! [[ -v dir_of_tegonal_scripts ]]; then
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/.."
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
fi

function escapeRegex() {
local -r pattern='s/[.[\*$^(){}+?|\\]/\\&/g'
if (($# == 0)); then
sed "$pattern"
elif (($# == 1)); then
sed "$pattern" <<<"$1"
else
traceAndDie "you need to either pass one element which shall be escaped or none in which case we read from stdin, given: %s" "$#"
fi
}

0 comments on commit 7d9206e

Please sign in to comment.