CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...
http://cdelord.fr/pp
GNU General Public License v3.0
252 stars 21 forks source link

Finding and extracting substrings #92

Open bpj opened 4 years ago

bpj commented 4 years ago

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! :-)

CDSoft commented 6 months ago

Sorry for the late reply... Please keep in mind that pp is not supported anymore, it's hard to deploy. For new projects I suggest ypp which is is based on a Lua interpreter and way easier to compile and install and binaries are easier to produce (thanks to zig) and deploy (see hey).