Tcl_DString(3)
π Tcl_DString(3) β Tcl Library Procedures π
okay so bestie, gather round π this is the Tcl_DString(3) page from the Tcl Library Procedures, and it's literally the whole glow-up guide for dynamic strings β the little string containers that grow as you feed them. no cap.
β¨ NAME β¨
Tcl_DStringInit, Tcl_DStringAppend, Tcl_DStringAppendElement, Tcl_DStringStartSublist, Tcl_DStringEndSublist, Tcl_DStringLength, Tcl_DStringValue, Tcl_DStringSetLength, Tcl_DStringFree, Tcl_DStringResult, Tcl_DStringGetResult, Tcl_DStringToObj β manipulate dynamic strings π¦
π SYNOPSIS π
#include <tcl.h>
Tcl_DStringInit(dsPtr)
char *
Tcl_DStringAppend(dsPtr, bytes, length)
char *
Tcl_DStringAppendElement(dsPtr, element)
Tcl_DStringStartSublist(dsPtr)
Tcl_DStringEndSublist(dsPtr)
Tcl_Size
Tcl_DStringLength(dsPtr)
char *
Tcl_DStringValue(dsPtr)
Tcl_DStringSetLength(dsPtr, newLength)
Tcl_DStringFree(dsPtr)
Tcl_DStringResult(interp, dsPtr)
Tcl_DStringGetResult(interp, dsPtr)
Tcl_Obj *
Tcl_DStringToObj(dsPtr)
π ARGUMENTS π
Tcl_DString *dsPtr(in/out) β pointer to the struct that's out here managing a dynamic string. it's giving main character energy.const char *bytes(in) β pointer to the characters you wanna append to the dynamic string.const char *element(in) β pointer to the characters you wanna append as a list element to the dynamic string.Tcl_Size length(in) β how many bytes frombytesto add to the dynamic string. if it's negative, bestie, add all the characters up to the null terminating character πTcl_Size newLength(in) β the new length for the dynamic string, not counting the null terminating character.Tcl_Interp *interp(in/out) β the interpreter whose result is getting set from, or moved to, the dynamic string.
π DESCRIPTION π
Dynamic strings are the mechanism for building up arbitrarily long strings by gradually appending info, one lil piece at a time. if the dynamic string is short then there's no memory allocation overhead β zero, nada, iconic. but as the string gets bigger, extra space gets allocated as needed. growth mindset fr fr. π±
Tcl_DStringInit initializes a dynamic string to zero length. the Tcl_DString structure has to have been allocated by the caller. no assumptions get made about the current state of the structure β anything already chillin' in it is discarded, bye π. if the structure has been used before, you should call Tcl_DStringFree first to free up any memory that was allocated for the old string.
Tcl_DStringAppend adds new information to a dynamic string, allocating more memory for the string if needed. if length is less than zero, then everything in bytes gets appended to the dynamic string; otherwise length says exactly how many bytes to append. Tcl_DStringAppend returns a pointer to the characters of the new string. you can also grab the string from the string field of the Tcl_DString structure.
Tcl_DStringAppendElement is basically Tcl_DStringAppend's sibling except it does not take a length argument (it appends all of element) and it converts the string into a proper list element before appending. Tcl_DStringAppendElement adds a separator space before the new list element unless the new list element is the first in a list or sub-list β that means either the current string is empty, or it contains the single character {, or the last two characters of the current string are {. Tcl_DStringAppendElement returns a pointer to the characters of the new string. slay β¨
Tcl_DStringStartSublist and Tcl_DStringEndSublist are your besties for creating nested lists. to append a list element that is itself a sublist: first call Tcl_DStringStartSublist, then call Tcl_DStringAppendElement for each of the elements in the sublist, then call Tcl_DStringEndSublist to end the sublist. Tcl_DStringStartSublist appends a space character if needed, followed by an open brace; Tcl_DStringEndSublist appends a close brace. lists can be nested to any depth β it's giving inception π
Tcl_DStringLength is a macro that returns the current length of a dynamic string (not including the terminating null character). Tcl_DStringValue is a macro that returns a pointer to the current contents of a dynamic string. macros, so, like, free real estate.
Tcl_DStringSetLength changes the length of a dynamic string. if newLength is less than the string's current length, then the string gets truncated. if newLength is greater than the string's current length, then the string becomes longer and new space gets allocated for it if needed. however β Tcl_DStringSetLength will not initialize the new space, except to provide a terminating null character; it's up to the caller to fill in the new space. and heads up: Tcl_DStringSetLength does not free up the string's storage space, even if the string is truncated all the way to zero length, so Tcl_DStringFree will still need to be called. no skipping steps π
Tcl_DStringFree should be called when you're finished using the string. it frees up any memory that was allocated for the string and reinitializes the string's value to an empty string. the cleanup era π
Tcl_DStringResult sets the result of interp to the value of the dynamic string given by dsPtr. it does this by moving a pointer from dsPtr to the interpreter's result β which saves the cost of allocating new memory and copying the string. efficiency? ate. Tcl_DStringResult also reinitializes the dynamic string to an empty string. since the dynamic string is reinitialized, there's no need to further call Tcl_DStringFree on it, and it can be reused without calling Tcl_DStringInit. the caller must ensure that the dynamic string stored in dsPtr is encoded in Tcl's internal UTF-8 format.
Tcl_DStringGetResult does the opposite of Tcl_DStringResult. it sets the value of dsPtr to the result of interp and it clears interp's result. if possible it does this by moving a pointer rather than by copying the string. π
Tcl_DStringToObj returns a Tcl_Obj containing the value of the dynamic string given by dsPtr. it does this by moving a pointer from dsPtr to a newly allocated Tcl_Obj and reinitializing the dynamic string to an empty string. this saves the cost of allocating new memory and copying the string. since the dynamic string is reinitialized, there's no need to further call Tcl_DStringFree on it and it can be reused without calling Tcl_DStringInit. the returned Tcl_Obj has a reference count of 0. the caller must ensure that the dynamic string stored in dsPtr is encoded in Tcl's internal UTF-8 format. π«Ά
ngl bestie, that UTF-8 requirement shows up twice for a reason βTcl_DStringResultandTcl_DStringToObjboth hand your bytes straight over without copying, so the encoding has to already be Tcl's internal UTF-8 format. don't be the one who forgets π
β KEYWORDS β
append, dynamic string, free, result
and that's the whole page, periodt. this was Tcl_DString(3) from the Tcl manual, version 7.4 β go build some strings, bestie, you're gonna eat πβ¨π
π¦ 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) 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