From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/113551 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joey McCollum via ntg-context Newsgroups: gmane.comp.tex.context Subject: Re: Checking for a Unicode prefix of a Unicode string Date: Sat, 27 Nov 2021 12:13:50 -0500 Message-ID: References: <47a78903-0e58-7b6a-a6ba-1cf7f83e62de@xs4all.nl> Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============9048425396880472212==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12347"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Joey McCollum To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Sat Nov 27 18:16:55 2021 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mr1Jn-00031t-5k for gctc-ntg-context-518@m.gmane-mx.org; Sat, 27 Nov 2021 18:16:55 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 848FD2896EB; Sat, 27 Nov 2021 18:14:22 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ERnLQWKdJj3f; Sat, 27 Nov 2021 18:14:20 +0100 (CET) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 658972895CA; Sat, 27 Nov 2021 18:14:20 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 88B2D28925E for ; Sat, 27 Nov 2021 18:14:18 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LNhl6ecXEPrU for ; Sat, 27 Nov 2021 18:14:16 +0100 (CET) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.166.171; helo=mail-il1-f171.google.com; envelope-from=jmccollum20140511@gmail.com; receiver= Original-Received: from mail-il1-f171.google.com (mail-il1-f171.google.com [209.85.166.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 8EEA5287C15 for ; Sat, 27 Nov 2021 18:14:16 +0100 (CET) Original-Received: by mail-il1-f171.google.com with SMTP id i9so12387089ilu.1 for ; Sat, 27 Nov 2021 09:14:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UILlqrTB14KcrUAt2Phg0O1zqmwlhw1WkeGwC+aAmy8=; b=Gn4nGQV0mappH/TBKMyENy+Fte8CDXbkXhjRZuZzzNX24CHKZqyPqTFvGfI+d1lwWP 5m7EwwAvzueqDGSPCkhlDpyODlS7kjB4TBdmsIbAsEgxp+C6ApxrkRBjHB8/NObwcxuE n4mqMNpUNESVe/mrDv6QnkZesWdxcOPv8OIrbAx4slk5wdaCToU/9V7ITnK6Ik1R4uAd ifhpbh9WjCgsxK94T3aEfTdNPoXk2EwvaXJjqgu40dNGQ32fAKE56PtgdLVSwnhnwMxF XBXdeM0hgoJTrF02A9PdZMV6U3upTjYcYocdS2OBguMv+ZUKyFnKZnvjvfopitbe/81y 3iQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UILlqrTB14KcrUAt2Phg0O1zqmwlhw1WkeGwC+aAmy8=; b=jU0b6scCo2GcVk1HnYZ5fvLYjmXqX2H3h/PTOp/DVy9J3KnOxF9cUaNUhah1Lmo0s/ YQV7gJf/IQuEjsKEPP1S259V9/pByX/24uiQgxZAxa4WDC3ho/XY0MM3sK1G8JuQt5hh mnd73EC0Op4osNzrvYEP3TVBc9uVWfoktcP3tmLGB4SLTX5IadmgzbA9yXwiAjpITKVp JqpN+APNRYmYlGU1aUZt9pWeKqmMAguf9Cjh7GvPj4rUjl8OfCslDpr/JgGCOXvqbKrG hFXd7gbqnYCQ2HtXhg2WvuCaJepp9F+lpKi26I2v/qzkF7sPwLlgpJthLhw5WHde8tLo wyZA== X-Gm-Message-State: AOAM533s1rCHZpbwqJTXmeVZ9OReW/69N0RKXa+gv/Fjvl9mABuZi5WW COXhu2a+FuRQlR50qWmC34mbhJcZhKUOES+LHWB4YYrpt4Q= X-Google-Smtp-Source: ABdhPJyCU3Y+I423Jjz6OSDUpGal7K2DH34QFeiOfN5aQt6t/JThDze/UMLnhY6m7Xq0tC4RjRVtfTwfKcGVGGpNSYw= X-Received: by 2002:a05:6e02:1c48:: with SMTP id d8mr20738059ilg.96.1638033255147; Sat, 27 Nov 2021 09:14:15 -0800 (PST) In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:113551 Archived-At: --===============9048425396880472212== Content-Type: multipart/alternative; boundary="0000000000004daa7e05d1c8566c" --0000000000004daa7e05d1c8566c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable All right, I think I've solved the expansion problem I described before: to ensure that the first input is expanded when it is passed to Lua, I have to pass it as [=3D=3D[#1]=3D=3D], not "#1". But the updated MWE below still do= es not seem to work, as the \DoIfPrefixElse macro is printing "NOP" instead of "YES": ``` \starttexdefinition loc [#1] \doifassignmentelse{#1} { % if an assignment, then parse and format accordingly \getparameters[loc][#1] % Was a section number specified? \doifdefined{locsec} { =C2=A7 \locsec } } { % otherwise, just print the input as-is #1 } \stoptexdefinition \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([=3D=3D[#2]= =3D=3D],"^"..[=3D=3D[#1]=3D=3D]))}} \def\currentbtxloctext{\loc[sec=3D31]} \starttext \currentbtxloctext\blank \DoIfPrefixElse{=C2=A7}{\currentbtxloctext}{YES}{NOP} \stoptext ``` Indeed, if I add a simple \doifelse equality check, it looks like the value I expect is not the same as what the macro produces, even though they look identical: ``` \starttexdefinition loc [#1] \doifassignmentelse{#1} { % if an assignment, then parse and format accordingly \getparameters[loc][#1] % Was a section number specified? \doifdefined{locsec} { =C2=A7 \locsec } } { % otherwise, just print the input as-is #1 } \stoptexdefinition \def\DoIfPrefixElse#1#2{\ctxlua{commands.doifelse(string.find([=3D=3D[#2]= =3D=3D],"^"..[=3D=3D[#1]=3D=3D]))}} \def\currentbtxloctext{\loc[sec=3D31]} \starttext =C2=A7 31\blank%the raw text we expect \currentbtxloctext\blank%the text as produced by the macro \doifelse{\currentbtxloctext}{=C2=A7 31}{YES}{NOP}\blank% should output= YES, but doesn't \DoIfPrefixElse{=C2=A7}{\currentbtxloctext}{YES}{NOP}\blank% should out= put YES, but doesn't \stoptext ``` What am I missing here? Joey On Fri, Nov 26, 2021 at 11:57 AM Joey McCollum wrote: > Thanks! Invoking string.find(str,"^"..pre) is certainly much easier. This > sort of thing would be a nice general-purpose helper function. Denis Maie= r > and I should have a "wish list" of desired helpers and features (mostly > related to bibliographies/publication support) ready for you (and Alan) > sometime soon. > > But for now, it looks like I still need to work out errors that arise whe= n > I pass a macro as the "str" input to be searched. I've defined a \loc mac= ro > that accepts an assignment and outputs a formatted string, and later, I > want to check if this macro (after it has been fully expanded) starts wit= h > a certain prefix. A minimal (non-)working example follows: > > ``` > > \starttexdefinition loc [#1] > > \doifassignmentelse{#1} { > > % if an assignment, then parse and format accordingly > > \getparameters[loc][#1] > > % Was a section number specified? > > \doifdefined{locsec} { > > =C2=A7 \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=3D31]} > > > \starttext > > \DoIfPrefixElse{=C2=A7}{\currentbtxloctext}{YES}{NOP} > > \stoptext > ``` > > Lua is throwing an "invalid escape sequence near '"\l'" error, which is > presumably because the macro \loc[sec=3D31] is not being expanded. How do= I > fix this? > > Thanks! > > Joey > > On Fri, Nov 26, 2021 at 3:46 AM Hans Hagen via ntg-context < > ntg-context@ntg.nl> wrote: > >> 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), an= d >> > 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)) =3D=3D 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 =3D "DoIfPrefixElse", >> arguments =3D { "argument", "argument" }, >> actions =3D 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 t= o >> 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 >> >> ________________________________________________________________________= ___________ >> > --0000000000004daa7e05d1c8566c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
All right, I think I've solved the expansion problem I= described before: to ensure that the first input is expanded when it is pa= ssed to Lua, I have to pass it as [=3D=3D[#1]=3D=3D], not "#1". B= ut the updated MWE below still does not seem to work, as the \DoIfPrefixEls= e macro is printing "NOP" instead of "YES":

```

\starttexdefini= tion loc [#1]

\doifassignmentelse{#1} { =

% if an assignment, th= en parse and format accordingly

\getparameters[loc][#1= ]

% Was a section number= specified?

\doifdefined{locsec} {=

=C2=A7 \locsec

}

} {

% otherwise, just prin= t the input as-is

#1

}

\stoptexdefinition


\def\DoIfPrefixElse#1#2{\ctxlu= a{commands.doifelse(string.find([=3D=3D[#2]=3D=3D],"^"..[=3D=3D[#= 1]=3D=3D]))}}


\def\currentbtxloctext{\loc[se= c=3D31]}


\starttext

\currentbtxloctext\blank

\DoIfPrefixElse{=C2=A7}{\c= urrentbtxloctext}{YES}{NOP}

\stoptext

```
Indeed, if I add a simple \doifelse=C2=A0equality check, it loo= ks like the value I expect is not the same as what the macro produces, even= though they look identical:

```

\starttexdefinition loc [#1]
=C2= =A0 =C2=A0 \doifassignmentelse{#1} {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 % if a= n assignment, then parse and format accordingly
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 \getparameters[loc][#1]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 % Was a section = number specified?
=C2=A0 =C2=A0 =C2=A0 =C2=A0 \doifdefined{locsec} {=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A7 \locsec
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 } {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 % other= wise, just print the input as-is
=C2=A0 =C2=A0 =C2=A0 =C2=A0 #1
=C2= =A0 =C2=A0 }
\stoptexdefinition

\def\DoIfPrefixElse#1#2{\ctxlua{c= ommands.doifelse(string.find([=3D=3D[#2]=3D=3D],"^"..[=3D=3D[#1]= =3D=3D]))}}

\def\currentbtxloctext{\loc[sec=3D31]}

\starttext=
=C2=A0 =C2=A0 =C2=A7 31\blank%the raw text we expect
=C2=A0 =C2=A0 \= currentbtxloctext\blank%the text as produced by the macro
=C2=A0 =C2=A0 = \doifelse{\currentbtxloctext}{=C2=A7 31}{YES}{NOP}\blank% should output YES= , but doesn't
=C2=A0 =C2=A0 \DoIfPrefixElse{=C2=A7}{\currentbtxlocte= xt}{YES}{NOP}\blank% should output YES, but doesn't
\stoptext

```

What am I missing here?
=
Joey

On Fri, Nov 26, 2021 at 11:57 AM Joey McCollum <= ;jmccollum20140511@gmail.com= > wrote:
=
Thanks! Invoking=C2=A0string.find(str,"^"..pre) = is certainly much easier. This sort of thing would be a nice general-purpos= e helper function. Denis Maier and I should have a "wish list" of= desired helpers and features (mostly related to bibliographies/publication= support) ready for you (and Alan) sometime soon.

But fo= r now, it looks like I still need to work out errors that arise when I pass= a macro as the "str" input to be searched. I've defined a \l= oc macro that accepts an assignment and outputs a formatted string, and lat= er, I want to check if this macro (after it has been fully expanded) starts= with a certain prefix. A minimal (non-)working example follows:
=
```

\starttexdef= inition loc [#1]

\doifa= ssignmentelse{#1} {

= % if an assignment, then parse and format accordingly

\getparameters[loc][#1]

% Was a section number specified?=

\doifdefined{locs= ec} {

=C2=A7 \l= ocsec\btxcomma

} <= /p>

} {

% otherwise, just print the input as-i= s

#1

}

\stoptexdefinition


\def\DoIfPrefi= xElse#1#2{\ctxlua{commands.doifelse(string.find("#2","^"= ;.."#1"))}}


<= /p>

\def\currentbtxloctext{\loc= [sec=3D31]}


\starttext

\DoIfPrefixElse{=C2=A7}{\currentbtxloctext}{YES}{= NOP}

\stoptext

```
=

Lua is throwing an "invalid escape sequence near '"\l'" error, which i= s presumably because the macro \loc[sec=3D31] is not being expanded. How do I fix this?
<= span style=3D"white-space:pre-wrap">
Thanks!

Joey

On Fri, Nov 26, 2021 at 3:46 AM Hans Hagen via ntg-context <ntg-context@ntg.nl&= gt; wrote:
On 11= /26/2021 7:42 AM, Joey McCollum via ntg-context wrote:
> I wasn't aware of a general-purpose "doifstartswith" mac= ro in ConTeXt
> (the \doifnextcharelse macro only works one character at a time, and t= he
> \doifinstring=C2=A0macros 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:
>
> ```
>=C2=A0 =C2=A0 function isprefix(prefix, str)
>=C2=A0 =C2=A0 =C2=A0 if string.sub(str, 1, string.len(prefix)) =3D=3D p= refix then
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return true
>=C2=A0 =C2=A0 =C2=A0 end
>=C2=A0 =C2=A0 =C2=A0 return false
>=C2=A0 =C2=A0 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 {
=C2=A0 =C2=A0 =C2=A0name=C2=A0 =C2=A0 =C2=A0 =3D "DoIfPrefixElse"= ,
=C2=A0 =C2=A0 =C2=A0arguments =3D { "argument", "argument&qu= ot; },
=C2=A0 =C2=A0 =C2=A0actions=C2=A0 =C2=A0=3D function(pre,str)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0commands.doifelse(string.find(str,"^= "..pre))
=C2=A0 =C2=A0 =C2=A0end
}
\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


-----------------------------------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hans Hagen | PRAGMA ADE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ridderstraat 27 | 80= 61 GH Hasselt | The Netherlands
=C2=A0 =C2=A0 =C2=A0 =C2=A0 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 t= he Wiki!

maillist : ntg-cont= ext@ntg.nl / http://www.ntg.nl/mailman/listinfo/nt= g-context
webpage=C2=A0 : http://www.pragma-ade.nl / http://context.aanhet.net=
archive=C2=A0 : https://bitbucket.org/phg/context-m= irror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: http://contextgarden.net
___________________________________________________________________________= ________
--0000000000004daa7e05d1c8566c-- --===============9048425396880472212== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============9048425396880472212==--