CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...

Home Page:http://cdelord.fr/pp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

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).