diff options
| Commit | a2a869b47f6cc26c6347b334693af408403db70b (patch) | |
|---|---|---|
| Author | 2023-05-05 10:59:54 +0300 | |
| Committer | 2023-05-05 10:59:54 +0300 | |
| Files | e389b64c528a34ed94028807a5054fdaa477898e /awk-edits/#strings.awk | |
| syndamia.com-a2a869b47f6cc26c6347b334693af408403db70b.tar syndamia.com-a2a869b47f6cc26c6347b334693af408403db70b.tar.gz syndamia.com-a2a869b47f6cc26c6347b334693af408403db70b.zip | ||
| Parent | aa7cd428253ed30071e6bfb3015d86c1b6e7a16b (diff) | |
[#strings] glsub support for overlapping and simplification
1 files changed, 20 insertions, 22 deletions
| -rw-r--r-- | awk-edits/#strings.awk | 42 |
diff --git a/awk-edits/#strings.awk b/awk-edits/#strings.awk index 3365df2..5e214cb 100644 --- a/awk-edits/#strings.awk +++ b/awk-edits/#strings.awk @@ -49,7 +49,8 @@ function gesub(regexp, exregexp, replacement, target) { # Behaves kind of like gsub, however substitution of regexp with replacement # only happens, when regexp is preceded by lbregexp and succeeded by laregexp. # Only regexp get's replaced, the matches of lbregexp and laregexp are left -# as-is. Overlapping of any form isn't supported. +# as-is. Overlapping of the lookahead and lookbehind substrings is supported, +# but any sort of overlapping which includes regexp isn't. # This function is quite slow, try to use as little as possible. # Returns the result string, rather than doing it inplace. function glsub(lbregexp, regexp, laregexp, replacement, target) { @@ -57,29 +58,26 @@ function glsub(lbregexp, regexp, laregexp, replacement, target) { target = $0; } - 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] == "") { - match(target, lbregexp regexp laregexp); - si = RLENGTH + 1; - } - else { - si = index(target, surrounds[i]); + result = "" + match(target, lbregexp regexp); + while (RLENGTH > -1) { + result = result substr(target, 1, RSTART - 1); + matched = substr(target, RSTART, RLENGTH); + target = substr(target, RSTART + RLENGTH); + + match(target, laregexp); + if (RSTART == 1) { + match(matched, regexp); + result = result substr(matched, 1, RSTART - 1); + matched = substr(matched, RSTART); + sub(/.*/, replacement, matched); } - 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); + result = result matched; - sub(/.*/, replacement, csep); - result = result behind csep ahead surrounds[i]; + match(target, lbregexp regexp); } - return result; + return result target; +} + } |