prefix(3tcl)

πŸ’– tcl::prefix β€” the prefix-matching bestie πŸ’–

okay so this is a page from the Tcl Built-In Commands manual, section 3tcl, and it's literally all about looking up prefixes in lists of strings. No cap, it's giving "autocomplete but make it a Tcl command." πŸ¦‹


✨ NAME ✨

tcl::prefix β€” facilities for prefix matching 🌸


✨ SYNOPSIS ✨

::tcl::prefix all table string
::tcl::prefix longest table string
::tcl::prefix match ?option ...? table string

✨ DESCRIPTION ✨

So bestie, this whole doc is about commands that go looking for a prefix inside a list of strings. Here's the lineup, and yes they all ate: πŸ‘‘

  • ::tcl::prefix all table string β€” hands you back a list of every element in table that starts with the prefix string. All of them. The whole squad. πŸ’…
  • ::tcl::prefix longest table string β€” gives you the longest common prefix of all the elements in table that begin with the prefix string. Like, how far can they all agree before they start diverging? 🌈
  • ::tcl::prefix match ?option ...? table string β€” the main character, fr fr. If string is equal to one element in table, or it's a prefix of exactly one element, then boom, you get that matched element back. But if it doesn't match? Then it all depends on the -error option, no pressure. (Also, tiny rec: it's recommended that you sort table before using it with this subcommand, so that the list of matches in the error message comes out sorted too. It's not strictly necessary for this subcommand to actually work, though β€” it's just a vibe thing. πŸŽ€)

The options it supports are:

  • -exact β€” only exact matches count. No partial credit, periodt. βœ‹
  • -message string β€” use string in the error message when there's a mismatch. The default is β€œoption”. πŸ’¬
  • -error options β€” these options get used when no match is found. If options is empty, then no error is generated at all and you just get an empty string back. Otherwise, the options are used as return options when generating the error message. The default corresponds to setting β€œ-level 0”. For example: if β€œ-error {-errorcode MyError -level 1}” is used, an error would be generated as:
return -errorcode MyError -level 1 -code error \
       "ambiguous option ..."

✨ EXAMPLES ✨

Basic use, babes:

namespace import ::tcl::prefix
prefix match {apa bepa cepa} apa
     β†’ apa
prefix match {apa bepa cepa} a
     β†’ apa
prefix match -exact {apa bepa cepa} a
     β†’ bad option "a": must be apa, bepa, or cepa
prefix match -message "switch" {apa ada bepa cepa} a
     β†’ ambiguous switch "a": must be apa, ada, bepa, or cepa
prefix longest {fblocked fconfigure fcopy file fileevent flush} fc
     β†’ fco
prefix all {fblocked fconfigure fcopy file fileevent flush} fc
     β†’ fconfigure fcopy

Simplifying option matching (so clean, so satisfying 🧚):

array set opts {-apa 1 -bepa "" -cepa 0}
foreach {arg val} $args {
    set opts([prefix match {-apa -bepa -cepa} $arg]) $val
}

Creating a switch that supports prefixes β€” iconic behavior honestly:

switch [prefix match {apa bepa cepa} $arg] {
    apa  { }
    bepa { }
    cepa { }
}

✨ SEE ALSO ✨

lsearch(3tcl), namespace(3tcl), string(3tcl), Tcl_GetIndexFromObj(3tcl) 🫢


✨ KEYWORDS ✨

prefix, table lookup ⭐


okay that's the whole page, you're officially a prefix-matching girlie now πŸ’œ This one's from Tcl 8.6, it's the prefix(3tcl) page, and it lives in the Tcl Built-In Commands manual. Mwah, go forth and match those prefixes. πŸ’…πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from