onetrueawk / awk

One true awk
Other
1.96k stars 156 forks source link

Consolidate sub and gsub in dosub #209

Closed mpinjr closed 8 months ago

mpinjr commented 8 months ago

In addition to the deduplicative benefits, several uses-after-free are fixed:

function d() { delete a } BEGIN { sub(/./, d(), a[0]) }
function d() { delete a } BEGIN { sub(d(), "repl", a[0]) }
function d() { delete a } BEGIN { gsub(/./, d(), a[0]) }
function d() { delete a } BEGIN { gsub(d(), "repl", a[0]) }

When not matching globally, dosub breaks the loop immediately after the lone replacement (a significant speedup in some cases).

mpinjr commented 8 months ago

The bsd-features/gensub half of the work is in https://github.com/mpinjr/awk/commits/dosub-bsd.