Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Finding and extracting substrings #92

Open
bpj opened this issue Dec 22, 2019 · 1 comment
Open

Finding and extracting substrings #92

bpj opened this issue Dec 22, 2019 · 1 comment

Comments

@bpj
Copy link

bpj commented Dec 22, 2019

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
Copy link
Owner

CDSoft commented Dec 22, 2023

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants