Summary Files Commits Branches @commit @diff @stats
path: root / awk-edits / #strings.awk
diff options
context:
space:
mode:
1 files changed, 22 insertions, 21 deletions
-rw-r--r--awk-edits/#strings.awk43
diff --git a/awk-edits/#strings.awk b/awk-edits/#strings.awk
index cc469e8..6972105 100644
--- a/awk-edits/#strings.awk
+++ b/awk-edits/#strings.awk
@@ -46,32 +46,33 @@ function gesub(regexp, exregexp, replacement, target) {
# glsub(lbregexp, regexp, largexp, replacement [, empt, target])
# Aka global sub with non-capturing look-around (lookbehind and lookahead)
-function glsub(lbregexp, regexp, laregexp, replacement, empt, target) {
+function glsub(lbregexp, regexp, laregexp, replacement, target) {
if (!length(target)) {
target = $0;
}
- if (empt >= 2) {
- RSTART = 1;
- RLENGTH = 0;
- empt -= 2;
- }
- result = "";
- subst = "";
- while ((RSTART == 1 && RLENGTH == 0 && length(result) == 0) || match(target, lbregexp)) {
- result = result substr(target, 1, RSTART + RLENGTH - 1);
- target = substr(target, RSTART + RLENGTH);
- match(target, regexp);
- if (RSTART == 1) {
- subst = substr(target, RSTART, RLENGTH);
- target = substr(target, RSTART + RLENGTH);
- match(target, laregexp);
- if ((empt == 1 && length(target) == 0) || RSTART == 1) {
- sub(/.*/, replacement, subst);
- }
- result = result subst;
+ surlen = split(target, surrounds, lbregexp regexp laregexp);
+ result = surrounds[1];
+ # POSIX compliance, seps parameter in split isn't available
+ target = substr(target, length(surrounds[1]) + 1);
+ for (i = 2; i <= surlen; i++) {
+ if (surrounds[i] == "") {
+ si = length(target) + 1;
+ }
+ else {
+ si = index(target, surrounds[i]);
}
+ csep = substr(target, 1, si - 1);
+ target = substr(target, length(surrounds[i]) + si);
+
+ match(csep, regexp);
+ behind = substr(csep, 1, RSTART - 1);
+ ahead = substr(csep, RSTART + RLENGTH);
+ csep = substr(csep, RSTART, RLENGTH);
+
+ sub(/.*/, replacement, csep);
+ result = result behind csep ahead surrounds[i];
}
- return result target;
+ return result;
}