ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Checking for a Unicode prefix of a Unicode string
@ 2021-11-26  6:42 Joey McCollum via ntg-context
  2021-11-26  8:45 ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Joey McCollum via ntg-context @ 2021-11-26  6:42 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Joey McCollum


[-- Attachment #1.1: Type: text/plain, Size: 1780 bytes --]

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
```

Unfortunately, if I want to check for prefixes that include two-byte
characters like § and ¶, then the positions and string lengths that I
specify no longer correspond to the actual byte offsets and lengths that
Lua uses. I'm aware of the utf8 plugin that was intended to address this
issue, but the following code also isn't working:

```
  function sbl.isprefix(prefix, str)
    -- lua is devious and measures string length in bytes, not chars,
    -- so we can't just use string.sub and string.len as we normally would.
    local i = utf8.offset(str, 1)
    local j = utf8.offset(str, utf8.len(prefix) + 1) - 1
    if string.sub(str, i, j) == prefix then
      return true
    end
    return false
  end
```

The only other detail that may be relevant is that I'm passing a macro as
the "str" input. But this should be expanded when the Lua code is
manipulating it, right?

I'm sure there's something obvious that I'm doing wrong, but I've been
trying to get this to work for at least a couple hours now, and I don't
know what else to try. If there is a simple fix to my Lua code or an
existing ConTeXt macro that will get the job done, I'd appreciate it!

Joey

[-- Attachment #1.2: Type: text/html, Size: 2093 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-11-29 20:13 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-26  6:42 Checking for a Unicode prefix of a Unicode string Joey McCollum via ntg-context
2021-11-26  8:45 ` Hans Hagen via ntg-context
2021-11-26 16:57   ` Joey McCollum via ntg-context
2021-11-27 17:13     ` Joey McCollum via ntg-context
2021-11-27 18:44       ` Hans Hagen via ntg-context
2021-11-28  1:39         ` Joey McCollum via ntg-context
2021-11-28 10:39           ` Hans Hagen via ntg-context
2021-11-29 17:11             ` Joey McCollum via ntg-context
2021-11-29 20:13               ` Joey McCollum via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).