\starttexdefinition loc [#1]
\doifassignmentelse{#1} {
% if an assignment, then parse and format accordingly
\getparameters[loc][#1]
% Was a section number specified?
\doifdefined{locsec} {
§ \locsec\btxcomma
}
} {
% otherwise, just print the input as-is
#1
}
\stoptexdefinition
\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}
\def\currentbtxloctext{\loc[sec=31]}
\starttext
\DoIfPrefixElse{§}{\currentbtxloctext}{YES}{NOP}
\stoptext
```On 11/26/2021 7:42 AM, Joey McCollum via ntg-context wrote:
> I wasn't aware of a general-purpose "doifstartswith" macro in ConTeXt
> (the \doifnextcharelse macro only works one character at a time, and the
> \doifinstring macros may capture substrings that are not prefixes), and
> I'd like to develop one for something I'm working on. I've been trying
> to do this in Lua, as that seemed like the most natural approach.
> Normally, something like this would work fine as a foundation:
>
> ```
> function isprefix(prefix, str)
> if string.sub(str, 1, string.len(prefix)) == prefix then
> return true
> end
> return false
> end
> ```
how about
if string.find(str,"^"..prefix) then
in:
\starttext
\def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^".."#1"))}}
\DoIfPrefixElse{pre}{prefix}{YES}{NOP}
\DoIfPrefixElse{pre}{suffix}{YES}{NOP}
\stoptext
utf strings are just sequences of bytes so matching works
when you want to do more in lua you can decide for
\startluacode
interfaces.implement {
name = "DoIfPrefixElse",
arguments = { "argument", "argument" },
actions = function(pre,str)
commands.doifelse(string.find(str,"^"..pre))
end
}
\stopluacode
\DoIfPrefixElse{pre}{prefix}{YES}{NOP}
\DoIfPrefixElse{pre}{suffix}{YES}{NOP}
but in any case: make sure that you don't clash with built in ...
if needed i can make a set of fast(er) ones but someone has to collect a
list of 'handy helpers' first
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________