Tcl_GetIndexFromObj(3)
π Tcl_GetIndexFromObj β looking up strings in a table of keywords π
okay bestie, welcome to Tcl_GetIndexFromObj(3), a page from the Tcl Library Procedures β¨. Two functions, one mission: you got a string, you got a lil list of allowed keywords, and you wanna know which one it is. Let's go π¦
β¨ NAME β¨
Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct β lookup string in table of keywords πΈ
π 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 you use for error reporting. If it'sNULL, then no message is provided on errors. lowkey the "who do I complain to" field πTcl_Obj *objPtr(in/out) β the string value of this value is what gets searched throughtablePtr. If theTCL_INDEX_TEMP_TABLEflag is not specified, the internal representation is modified to hold the index of the matching table entry. yes bestie, it writes back into the object πconst char *const *tablePtr(in) β an array of null-terminated strings. The end of the array is marked by aNULLstring pointer. Note that, unless theTCL_INDEX_TEMP_TABLEflag is specified, references to thetablePtrmay be retained in the internal representation ofobjPtr, so this should represent the address of a statically-allocated array. like, do not be out here passing stack garbage, fr fr π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 byoffset. Note that, unless theTCL_INDEX_TEMP_TABLEflag is specified, references to thestructTablePtrmay be retained in the internal representation ofobjPtr, so this should represent the address of a statically-allocated array of structures. same energy as above, iconic π§int offset(in) β the offset to add tostructTablePtrto get to the next entry. The end of the array is marked by aNULLstring pointer.const char *msg(in) β a null-terminated string describing what is being looked up, such asoption. This string is included in error messages. it's the little label that shows up when things go wrong π«Άint flags(in) β OR-ed combination of bits providing additional information for operation. The only bits that are currently defined areTCL_EXACT,TCL_INDEX_TEMP_TABLE, andTCL_NULL_OK. that's it, that's the whole menu βenum|char|short|int|long *indexPtr(out) β if not(int *)NULL, the index of the string intablePtrthat matches the value ofobjPtris returned here. The variable can be any integer type, signed or unsigned,char,short,longorlong long. It can also be anenum. flexible queen π
π DESCRIPTION π
These procedures provide an efficient way for looking up keywords, switch names, option names, and similar things where the literal value of a Tcl value must be chosen from a predefined set. Tcl_GetIndexFromObj compares objPtr against each of the strings in tablePtr to find a match. A match occurs if objPtr's string value is identical to one of the strings in tablePtr, or if it is a non-empty unique abbreviation for exactly one of the strings in tablePtr and the TCL_EXACT flag was not specified; in either case TCL_OK is returned. If indexPtr is not NULL the index of the matching entry is stored at *indexPtr. slay β¨
If there is no matching entry, TCL_ERROR is returned and an error message is left in interp's result if interp is not NULL. Msg is included in the error message to indicate 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β. the typo callout is real π
If the TCL_INDEX_TEMP_TABLE was not specified, 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. If Tcl_GetIndexFromObj is invoked again with the same objPtr and tablePtr arguments (e.g. during a reinvocation of a Tcl command), it returns the matching index immediately without having to redo the lookup operation. 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. 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. so bestie, the caching is the whole glow-up here π₯
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 (i.e. that there is 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. struct-pilled π§π
π REFERENCE COUNT MANAGEMENT π
Tcl_GetIndexFromObj and Tcl_GetIndexFromObjStruct do not modify the reference count of their objPtr arguments; they only read. Note however that these functions may set the interpreter result; if that is the only place that is holding a reference to the object, it will be deleted. ngl that last bit is a jumpscare π
β οΈ real talk: if the interpreter result was the only thing holding that object, setting the result means it gets deleted. no cap, watch your refs.
π¦ SEE ALSO π¦
prefix(n), Tcl_WrongNumArgs(3)
β KEYWORDS β
index, option, value, table lookup
and that's the whole page, bestie!! you ate π
β¨ this one shipped way back with Tcl 8.1, and it's filed under the Tcl_GetIndexFromObj(3) man page β go forth and look up your keywords, mwah ππ«Ά
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms