Tcl_GetIndexFromObj(3tcl)

πŸŽ€ NAME πŸŽ€

Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct β€” bestie, it's the "lookup string in table of keywords" situation πŸ’…

So like. This is a page from Tcl Library Procedures, the Tcl_GetIndexFromObj(3tcl) one, and it's honestly such a useful little duo. Let's get into it. πŸ¦‹


✨ SYNOPSIS ✨

#include <tcl.h>

int
Tcl_GetIndexFromObj(interp, objPtr, tablePtr, msg, flags, indexPtr)

int
Tcl_GetIndexFromObjStruct(interp, objPtr, structTablePtr, offset, msg, flags, indexPtr)

🌸 ARGUMENTS 🌸

  • Tcl_Interp *interp (in) β€” the interpreter we use for reporting errors. If it's NULL, then no message gets provided on errors, so, like, screaming into the void 🫠
  • Tcl_Obj *objPtr (in/out) β€” its string value is what we search through tablePtr with. As long as the TCL_INDEX_TEMP_TABLE flag is not specified, its internal representation gets modified to hold the index of the matching table entry πŸ‘‘
  • const char *const *tablePtr (in) β€” an array of null-terminated strings, and the end of the array is marked by a NULL string pointer. Heads up bestie: unless TCL_INDEX_TEMP_TABLE is specified, references to the tablePtr may be retained in the internal representation of objPtr, so this should literally be the address of a statically-allocated array. No local stack arrays. I said what I said πŸ’€
  • const void *structTablePtr (in) β€” an array of arbitrary type, typically some struct type. The first member of the structure must be a null-terminated string. The size of the structure is given by offset. And same deal as above: unless TCL_INDEX_TEMP_TABLE is specified, references to the structTablePtr may be retained in the internal representation of objPtr, so this should represent the address of a statically-allocated array of structures 🧚
  • int offset (in) β€” the offset to add to structTablePtr to get to the next entry. The end of the array is marked by a NULL string pointer.
  • const char *msg (in) β€” a null-terminated string describing what is being looked up, such as option. This string is included in error messages.
  • int flags (in) β€” OR-ed combination of bits giving extra info for the operation. The only bits currently defined are TCL_EXACT, TCL_INDEX_TEMP_TABLE, and TCL_NULL_OK.
  • enum|char|short|int|long *indexPtr (out) β€” if it's not (int *)NULL, the index of the string in tablePtr that matches the value of objPtr gets returned here. The variable can be any integer type, signed or unsigned, char, short, long or long long. It can also be an enum. Flexible queen 🌈

πŸ’– DESCRIPTION πŸ’–

These procedures are the efficient way to look up keywords, switch names, option names, and similar things β€” the whole "the literal value of a Tcl value has to be chosen from a predefined set" situation. No cap.

Tcl_GetIndexFromObj compares objPtr against each of the strings in tablePtr to find a match. A match happens if objPtr's string value is identical to one of the strings in tablePtr, or if it's a non-empty unique abbreviation for exactly one of the strings in tablePtr and the TCL_EXACT flag was not specified. Either way you get TCL_OK back. And if indexPtr is not NULL, the index of the matching entry gets stored at *indexPtr. Slay ✨

If there's no matching entry, TCL_ERROR comes back and an error message is left in interp's result, as long as interp isn't NULL. msg is included in the error message to say what was being looked up. For example, if msg is option, the error message will have a form like β€œbad option "firt": must be first, second, or third”. Typo representation is real 😭

If TCL_INDEX_TEMP_TABLE was not specified, then when Tcl_GetIndexFromObj completes successfully it modifies the internal representation of objPtr to hold the address of the table and the index of the matching entry. So if Tcl_GetIndexFromObj gets invoked again with the same objPtr and tablePtr arguments (e.g. during a reinvocation of a Tcl command), it returns the matching index immediately, no redoing the lookup operation, we love a caching moment πŸ‘‘ Note that Tcl_GetIndexFromObj assumes that the entries in tablePtr are static β€” they must not change between invocations. This caching mechanism can be disallowed by specifying the TCL_INDEX_TEMP_TABLE flag. If the TCL_NULL_OK flag was specified, objPtr is allowed to be NULL or the empty string, and the resulting index is -1. Otherwise, if the value of objPtr is the empty string, Tcl_GetIndexFromObj will treat it as a non-matching value and return TCL_ERROR. Empty string catching strays fr fr πŸ™ˆ

Tcl_GetIndexFromObjStruct works just like Tcl_GetIndexFromObj, except that instead of treating tablePtr as an array of string pointers, it treats it as a pointer to the first string in a series of strings that have offset bytes between them β€” like, there's a pointer to the first array of characters at tablePtr, a pointer to the second array of characters at tablePtr+offset bytes, etc. This is particularly useful when processing things like Tk_ConfigurationSpec, whose string keys are in the same place in each of several array elements. So specific, so iconic πŸŽ€


🫢 REFERENCE COUNT MANAGEMENT 🫢

Tcl_GetIndexFromObj and Tcl_GetIndexFromObjStruct do not modify the reference count of their objPtr arguments β€” they only read. But note: these functions may set the interpreter result, and if that's the only place holding a reference to the object, it will be deleted. So like, keep an eye on your refcounts, bestie πŸ’œ


πŸ‘€ SEE ALSO πŸ‘€


πŸ”₯ KEYWORDS πŸ”₯

index, option, value, table lookup


okay that's the whole page, you ate that, go forth and index your little heart out πŸ’…βœ¨

sending you off with love from the Tcl crew β€” this one's from the Tcl manual, version 8.1, dated alongside Tcl_GetIndexFromObj(3tcl) itself. bye bestie, ily πŸ¦‹πŸ’–

πŸ“¦ where this page comes from