ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Joey McCollum via ntg-context <ntg-context@ntg.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Cc: Joey McCollum <jmccollum20140511@gmail.com>
Subject: Checking for a Unicode prefix of a Unicode string
Date: Fri, 26 Nov 2021 01:42:00 -0500	[thread overview]
Message-ID: <CAGxRUG9bLojOeEERpXXhYVbsin7geDVk81ZEBS-nudgU+0OYog@mail.gmail.com> (raw)


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

             reply	other threads:[~2021-11-26  6:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-26  6:42 Joey McCollum via ntg-context [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGxRUG9bLojOeEERpXXhYVbsin7geDVk81ZEBS-nudgU+0OYog@mail.gmail.com \
    --to=ntg-context@ntg.nl \
    --cc=jmccollum20140511@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).