Tcl_SplitList(3)
π Tcl_SplitList(3) β the Tcl_SplitList manual page, bestie edition π
okay so welcome to the Tcl Library Procedures corner of the group chat πΈ this whole page is about the six functions that manipulate Tcl lists: Tcl_SplitList, Tcl_Merge, Tcl_ScanElement, Tcl_ConvertElement, Tcl_ScanCountedElement, and Tcl_ConvertCountedElement. yes they're a whole friend group. sit down, hydrate, let's go β¨
π SYNOPSIS
#include <tcl.h>
int
Tcl_SplitList(interp, list, argcPtr, argvPtr)
char *
Tcl_Merge(argc, argv)
Tcl_Size
Tcl_ScanElement(src, flagsPtr)
Tcl_Size
Tcl_ScanCountedElement(src, length, flagsPtr)
Tcl_Size
Tcl_ConvertElement(src, dst, flags)
Tcl_Size
Tcl_ConvertCountedElement(src, length, dst, flags)
π ARGUMENTS
Tcl_Interp *interp(out) β the interpreter we use for error reporting π«Ά if it'sNULL, then no error message gets left behind. ghosting, honestly.const char *list(in) β pointer to a string that has proper list structure πTcl_Size | int *argcPtr(out) β gets filled in with the number of elements inlist. it may be(Tcl_Size *)NULLwhen it's not used. β οΈ and listen: if it points to a variable whose type is notTcl_Size, a compiler warning will be generated. and if your extension is compiled with-DTCL_8_API, this function will returnTCL_ERRORfor lists with more thanINT_MAXelements (which should trigger proper error-handling), otherwise expect it to crash π fr fr.const char ***argvPtr(out) β*argvPtrgets filled in with the address of an array of pointers to the strings that are the extracted elements oflist. there will be*argcPtrvalid entries in that array, followed by aNULLentry πTcl_Size argc(in) β number of elements inargv.const char *const *argv(in) β array of strings to merge together into one single list. each string becomes its own separate element β¨const char *src(in) β the string that's about to become an element of a list.int *flagsPtr(in) β pointer to a word we fill in with information aboutsrc. the value of*flagsPtrmust be passed toTcl_ConvertElement. no skipping steps bestie πTcl_Size length(in) β number of bytes in stringsrc.char *dst(in) β the place to copy the converted list element. it must contain enough characters to hold the converted string, so don't be stingy with the buffer πint flags(in) β information aboutsrc. it must be the value returned by a previous call toTcl_ScanElement, possibly OR-ed withTCL_DONT_USE_BRACES.
π¦ DESCRIPTION
these procedures may be used to disassemble and reassemble Tcl lists, and honestly? iconic behaviour.
Tcl_SplitList breaks a list up into its constituent elements, returning an array of pointers to the elements using argcPtr and argvPtr. while it's extracting the arguments, Tcl_SplitList obeys the usual rules for backslash substitutions and braces π the area of memory pointed to by *argvPtr is dynamically allocated β and in addition to the array of pointers, it also holds copies of all the list elements. so it is the caller's responsibility to free up all of this storage. don't leak, babe π
for example, suppose you called Tcl_SplitList with the following code:
Tcl_Size argc;
int code;
char *string;
char **argv;
...
code = Tcl_SplitList(interp, string, &argc, &argv);
then you should eventually free the storage with a call like the following:
Tcl_Free(argv);
Tcl_SplitList normally returns TCL_OK, which means the list was successfully parsed π if sizePtr points to a variable of type int and the list contains more than 2**31 key/value pairs, or there was a syntax error in list, then TCL_ERROR is returned and the interpreter's result will point to an error message describing the problem (if interp was not NULL). and if TCL_ERROR is returned then no memory is allocated and *argvPtr is not modified β so nothing weird happens behind your back π
Tcl_Merge is the inverse of Tcl_SplitList: it takes a collection of strings given by argc and argv and generates a result string that has proper list structure πΈ this means that commands like index may be used to extract the original elements again. in addition, if the result of Tcl_Merge is passed to Tcl_Eval, it will be parsed into argc words whose values will be the same as the argv strings passed to Tcl_Merge. Tcl_Merge will modify the list elements with braces and/or backslashes in order to produce proper Tcl list structure β¨ the result string is dynamically allocated using Tcl_Alloc; the caller must eventually release the space using Tcl_Free.
if the result of Tcl_Merge is passed to Tcl_SplitList, the elements returned by Tcl_SplitList will be identical to those passed into Tcl_Merge π«Ά however, the converse is not true: if Tcl_SplitList is passed a given string, and the resulting argc and argv are passed to Tcl_Merge, the resulting string may not be the same as the original string passed to Tcl_SplitList. this is because Tcl_Merge may use backslashes and braces differently than the original string π not a bug, just vibes.
Tcl_ScanElement and Tcl_ConvertElement are the procedures that do all of the real work of Tcl_Merge πͺ Tcl_ScanElement scans its src argument and determines how to use backslashes and braces when converting it to a list element. it returns an overestimate of the number of characters required to represent src as a list element, and it stores information in *flagsPtr that is needed by Tcl_ConvertElement.
Tcl_ConvertElement is a companion procedure to Tcl_ScanElement. it does the actual work of converting a string to a list element π its flags argument must be the same as the value returned by Tcl_ScanElement. Tcl_ConvertElement writes a proper list element to memory starting at *dst and returns a count of the total number of characters written, which will be no more than the result returned by Tcl_ScanElement. Tcl_ConvertElement writes out only the actual list element without any leading or trailing spaces: it is up to the caller to include spaces between adjacent list elements π
Tcl_ConvertElement uses one of two different approaches to handle the special characters in src. wherever possible, it handles special characters by surrounding the string with braces π
this produces clean-looking output, but cannot be used in some situations, such as when src contains unmatched braces. in these situations, Tcl_ConvertElement handles special characters by generating backslash sequences for them. the caller may insist on the second approach by OR-ing the flag value returned by Tcl_ScanElement with TCL_DONT_USE_BRACES. although this will produce an uglier result, it is useful in some special situations, such as when Tcl_ConvertElement is being used to generate a portion of an argument for a Tcl command. in this case, surrounding src with curly braces would cause the command not to be parsed correctly π¬ so like, ugly but correct. we love that for it.
by default, Tcl_ConvertElement will use quoting in its output to be sure the first character of an element is not the hash character ("#"). this is to be sure the first element of any list passed to eval is not mis-parsed as the beginning of a comment π₯ when a list element is not the first element of a list, this quoting is not necessary. when the caller can be sure that the element is not the first element of a list, it can disable quoting of the leading hash character by OR-ing the flag value returned by Tcl_ScanElement with TCL_DONT_QUOTE_HASH.
Tcl_ScanCountedElement and Tcl_ConvertCountedElement are the same as Tcl_ScanElement and Tcl_ConvertElement, except the length of string src is specified by the length argument, and the string may contain embedded nulls π fancy.
π SEE ALSO
β KEYWORDS
backslash, convert, element, list, merge, split, strings
and that's the whole page, bestie, you ate that π this one's from Tcl 8.0 and it's the Tcl_SplitList(3) page β go forth and split some lists, ily, 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
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1989-1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms