Finding and extracting substrings
bpj opened this issue · comments
This could be a partial replacement for #90. If Lua is integrated as suggested there all this could of course easi{ly,er} be done in Lua, except that Lua would only count bytes, not characters
It would be good to have macros for finding the position of a substring and extracting a substring, and reasonably also the length of some text in the classic awk/perl style.
!index[(INDEX)](TEXT)(SUBSTR)
!substr(TEXT)(OFFSET)[(LENGTH)]
!len(TEXT)
!def(x)(foo bar baz bap)
!index(!x)(ba)
--> 5
!index(2)(!x)(ba)
--> 9
!index(-1)(!x)(ba)
--> 13
!substr(!x)(5)
--> bar baz bap
!substr(!x)(-2)
--> ap
!substr(!x)(5)(6)
--> bar ba
!substr(!x)(5)(-5)
--> bar ba
!substr(!x)(!index(!x)(ba))(!eval(-!index(-1)(!x)(ba)))
--> "bar baz "
!substr(!index(!foo)(!bar))(!eval(!len(!bar)+3))
I stole the idea for the INDEX argument to !index
, which with negative indices also makes an !rindex
unnecessary, from the LaTeX xstring package. (Don't worry, I'm fully aware that all of those xstring macros can be implemented using the three proposed above and existing pp macros! :-)