Tcl_StringObj(3)

πŸ“¦ version 9.1.0, in nixpkgs unstable

okay bestie, welcome to theTcl Library Procedures corner of the internet β€” this one's Tcl_StringObj(3) and it's literally the whole string-manipulation saga πŸ¦‹βœ¨ Grab a snack, because this is part 1 of a trilogy and we are not skipping a single flag. slay πŸ’…


πŸŽ€ NAME πŸŽ€

A whole squad of functions that all do one iconic job β€” manipulate Tcl values as strings:

  • Tcl_NewStringObj
  • Tcl_NewUnicodeObj
  • Tcl_SetStringObj
  • Tcl_SetUnicodeObj
  • Tcl_GetStringFromObj
  • Tcl_GetString
  • Tcl_GetUnicodeFromObj
  • Tcl_GetUnicode
  • Tcl_GetUniChar
  • Tcl_GetCharLength
  • Tcl_GetRange
  • Tcl_AppendToObj
  • Tcl_AppendUnicodeToObj
  • Tcl_AppendObjToObj
  • Tcl_AppendStringsToObj
  • Tcl_AppendLimitedToObj
  • Tcl_Format
  • Tcl_AppendFormatToObj
  • Tcl_ObjPrintf
  • Tcl_AppendPrintfToObj
  • Tcl_SetObjLength
  • Tcl_AttemptSetObjLength
  • Tcl_ConcatObj
  • Tcl_IsEmpty

✨ SYNOPSIS ✨

First things first, bestie, you gotta pull in the header:

#include <tcl.h>

Then it's just... a lot of signatures. No cap, here they all are, verbatim, because I refuse to fumble the bag:

Tcl_Obj *
Tcl_NewStringObj(bytes, length)

Tcl_Obj *
Tcl_NewUnicodeObj(unicode, numChars)

void
Tcl_SetStringObj(objPtr, bytes, length)

void
Tcl_SetUnicodeObj(objPtr, unicode, numChars)

char *
Tcl_GetStringFromObj(objPtr, lengthPtr)

char *
Tcl_GetString(objPtr)

Tcl_UniChar *
Tcl_GetUnicodeFromObj(objPtr, lengthPtr)

Tcl_UniChar *
Tcl_GetUnicode(objPtr)

int
Tcl_GetUniChar(objPtr, index)

Tcl_Size
Tcl_GetCharLength(objPtr)

Tcl_Obj *
Tcl_GetRange(objPtr, first, last)

void
Tcl_AppendToObj(objPtr, bytes, length)

void
Tcl_AppendUnicodeToObj(objPtr, unicode, numChars)

void
Tcl_AppendObjToObj(objPtr, appendObjPtr)

void
Tcl_AppendStringsToObj(objPtr, string, string, ... (char *)NULL)

void
Tcl_AppendLimitedToObj(objPtr, bytes, length, limit, ellipsis)

Tcl_Obj *
Tcl_Format(interp, format, objc, objv)

int
Tcl_AppendFormatToObj(interp, objPtr, format, objc, objv)

Tcl_Obj *
Tcl_ObjPrintf(format, ...)

void
Tcl_AppendPrintfToObj(objPtr, format, ...)

void
Tcl_SetObjLength(objPtr, newLength)

int
Tcl_AttemptSetObjLength(objPtr, newLength)

Tcl_Obj *
Tcl_ConcatObj(objc, objv)

int
Tcl_IsEmpty(fIobjPtr)

(Yes, fIobjPtr is exactly what's written there. Not me, not my typo β€” I'm just the messenger 🫢)


πŸ’… ARGUMENTS πŸ’…

Every single parameter, and I mean every one, with the vibes attached:

  • const char *bytes (in) β€” Points at a TUTF-8 byte sequence (that's Tcl's internal modified UTF-8 encoding, iykyk) that you're using to set or append to a string value. Heads up: this byte array may contain embedded null characters unless numChars is negative. And if your app genuinely needs null bytes, listen β€” you should represent them as the two-byte sequence 0xC0 0x80, which is what TUTF-8 uses to represent a null byte, or use Tcl_ExternalToUtf to convert, or use Tcl_NewByteArrayObj if the string is really just a collection of uninterpreted bytes. multiple options, pick your fighter πŸ₯Š
  • Tcl_Size length (in) β€” The number of bytes to copy from bytes when you're initializing, setting, or appending to a string value. If it's negative, then it's all bytes up to the first null that get used. πŸŽ€
  • const Tcl_UniChar *unicode (in) β€” Points to the first byte of an array of Unicode characters used to set or append to a string value. Same deal as above: this byte array may contain embedded null characters unless numChars is negative. ⭐
  • Tcl_Size numChars (in) β€” The number of Unicode characters to copy from unicode when initializing, setting, or appending to a string value. If negative, all characters up to the first null character get used. πŸ’–
  • Tcl_Size index (in) β€” The index of the Unicode character you want returned. 🎯
  • Tcl_Size first (in) β€” The index of the first Unicode character in the Unicode range that gets returned as a new value. If it's negative, it behaves the same as if the value was 0. πŸ™ˆ
  • Tcl_Size last (in) β€” The index of the last Unicode character in the Unicode range to be returned as a new value. If it's negative, take all characters up to the last one available. 🌸
  • Tcl_Obj *objPtr (in/out) β€” A pointer to a value you're reading, or to an unshared value you're modifying. Unshared. Don't forget that part, bestie, it matters. πŸ‘€
  • Tcl_Obj *appendObjPtr (in) β€” The value to append to objPtr in Tcl_AppendObjToObj. 🫢
  • Tcl_Size | int *lengthPtr (out) β€” The location where Tcl_GetStringFromObj will store the length of a value's string representation. It's chill to pass (Tcl_Size *)NULL when you're not using it. But β€” if it points to a variable whose type is not Tcl_Size, a compiler warning will be generated. And if your extension is compiled with -DTCL_8_API, this function will panic for strings with more than INT_MAX bytes/characters; otherwise just expect it to crash. 😭 I said what I said, it's in the book.
  • const char *string (in) β€” Null-terminated string value to append to objPtr. πŸ“
  • Tcl_Size limit (in) β€” Maximum number of bytes to be appended. That's the ceiling, periodt. β›”
  • const char *ellipsis (in) β€” Suffix to append when the limit leads to string truncation. If NULL is passed then the suffix "..." is used. πŸŽ€
  • const char *format (in) β€” Format control string including % conversion specifiers. The classic. ✨
  • Tcl_Size objc (in) β€” The number of elements to format or concatenate. πŸ”’
  • Tcl_Obj **objv (in) β€” The array of values to format or concatenate. 🧺
  • Tcl_Size newLength (in) β€” New length for the string value of objPtr, not including the final null character. Like, don't count the null, she's not part of the party. 🚫

🌸 DESCRIPTION 🌸

So here's the whole vibe: the procedures in this manual entry let Tcl values be manipulated as string values. They use the value's internal representation to stash extra info and make string manipulation more efficient. And specifically, they make a series of append operations efficient by allocating extra storage space for the string so it doesn't have to be copied for each append. the way that's such a glow-up πŸ’š Also, indexing and length computations are optimized because the Unicode string representation gets calculated and cached as needed. πŸ”₯

okay this next bit is important, don't scroll past β€” When you're using the Tcl_Append* family of functions where the interpreter's result is the value being appended to, it's important to call Tcl_ResetResult first to make sure you're not unintentionally appending to existing data in the result value. do NOT skip this, bestie, I'm begging 😭

Tcl_NewStringObj and Tcl_SetStringObj create a new value or modify an existing value to hold a copy of the string given by bytes and length. πŸ“

Tcl_NewUnicodeObj and Tcl_SetUnicodeObj create a new value or modify an existing value to hold a copy of the Unicode string given by unicode and numChars. 🌟

Tcl_NewStringObj and Tcl_NewUnicodeObj both return a pointer to a newly created value with reference count zero. All four procedures set the value to hold a copy of the specified string. And Tcl_SetStringObj and Tcl_SetUnicodeObj free any old string representation as well as any old internal representation of the value. 🧹 out with the old, in with the new


okay and that's where part 1 wraps β€” do not close the tab, more parts are coming and you're gonna wanna be here for them πŸ’–πŸ¦‹

okay so bestie, Tcl_GetStringFromObj and Tcl_GetString are your go-to for snagging a value's string representation πŸ’–. You get it through the returned byte pointer, and for Tcl_GetStringFromObj you also get the length β€” stored in lengthPtr if that's not NULL. If the value's UTF string representation is invalid (like, its byte pointer is NULL), then the string representation gets regenerated from the value's internal representation. The storage that the returned byte pointer points at? Owned by the value manager, periodt. It's passed back as a writable pointer so that an extension author creating their own Tcl_ObjType can modify the string representation inside the Tcl_UpdateStringProc of their Tcl_ObjType. But outside that one limited purpose? Treat the pointer from Tcl_GetStringFromObj or Tcl_GetString as read-only, no cap. It's recommended you assign it to a (const char *) variable. Even in those limited situations where writing is acceptable, you gotta respect the copy-on-write semantics required by Tcl_Obj's, with appropriate calls to Tcl_IsShared and Tcl_DuplicateObj before any in-place modification of the string representation. Tcl_GetString is the one you reach for in the common case where you don't need the length of the string representation. ✨

Next up, Tcl_GetUnicodeFromObj and Tcl_GetUnicode return a value's value as a Unicode string πŸ¦‹. This comes as the returned pointer and (for Tcl_GetUnicodeFromObj) the length, stored in lengthPtr if it's non-NULL. The storage referenced by the returned byte pointer is owned by the value manager and should not be modified by the caller. Tcl_GetUnicode is used in the common case where the caller doesn't need the length of the unicode string representation.

Tcl_GetUniChar returns the index'th character in the value's Unicode representation. If the index is out of range it returns -1; πŸ’…

Tcl_GetRange returns a newly created value comprised of the characters between first and last (inclusive) in the value's Unicode or byte-array representation 🌸. If the value is not a byte-array and the value's Unicode representation is invalid, the Unicode representation is regenerated from the value's string representation. If first is negative, then the returned string starts at the beginning of the value. If last is negative, then the returned string ends at the end of the value.

Tcl_GetCharLength returns the number of characters (as opposed to bytes) in the string value. Like, actual characters, not just bytes, iykyk.

Tcl_AppendToObj appends the data given by bytes and length to the string representation of the value specified by objPtr. If the value has an invalid string representation, then an attempt is made to convert bytes to the Unicode format. If the conversion is successful, then the converted form of bytes is appended to the value's Unicode representation. Otherwise, the value's Unicode representation is invalidated and converted to the UTF format, and bytes is appended to the value's new string representation. Eventually buffer growth is done by large allocations to optimize multiple calls. πŸ”₯

Tcl_AppendUnicodeToObj appends the Unicode string given by unicode and numChars to the value specified by objPtr. If the value has an invalid Unicode representation, then unicode is converted to the UTF format and appended to the value's string representation. Appends are optimized to handle repeated appends relatively efficiently (it over-allocates the string or Unicode space to avoid repeated reallocations and copies of the value's string value). πŸŽ€

Tcl_AppendObjToObj is similar to Tcl_AppendToObj, but it appends the string or Unicode value (whichever exists and is best suited to be appended to objPtr) of appendObjPtr to objPtr.

Tcl_AppendStringsToObj is similar to Tcl_AppendToObj except that it can be passed more than one value to append and each value must be a null-terminated string (i.e. none of the values may contain internal null characters). Any number of string arguments may be provided, but the last argument must be (char *)NULL to indicate the end of the list. Don't forget that sentinel, bestie! πŸ’€

Tcl_AppendLimitedToObj is similar to Tcl_AppendToObj except that it imposes a limit on how many bytes are appended. This can be handy when the string to be appended might be very large, but the value being constructed should not be allowed to grow without bound. A common usage is when constructing an error message, where the end result should be kept short enough to be read. Bytes from bytes are appended to objPtr, but no more than limit bytes total are to be appended. If the limit prevents all length bytes that are available from being appended, then the appending is done so that the last bytes appended are from the string ellipsis. This allows for an indication of the truncation to be left in the string. When length is negative, all bytes up to the first zero byte are appended, subject to the limit. When ellipsis is NULL, the default string ... is used. When ellipsis is non-NULL, it must point to a zero-byte-terminated string in Tcl's internal UTF encoding. The number of bytes appended can be less than the lesser of length and limit when appending fewer bytes is necessary to append only whole multi-byte characters. 🫢

okay but like, seriously, the truncation logic is doing the most to keep things readable, so respect the ellipsis parameter.

Tcl_Format is the C-level interface to the engine of the format command. The actual command procedure for format is little more than

Tcl_Format(interp, Tcl_GetString(objv[1]), objc-2, objv+2);

The objc Tcl_Obj values in objv are formatted into a string according to the conversion specification in format argument, following the documentation for the format command. The resulting formatted string is converted to a new Tcl_Obj with refcount of zero and returned. If some error happens during production of the formatted string, NULL is returned, and an error message is recorded in interp, if interp is non-NULL. 😭

Tcl_AppendFormatToObj is an appending alternative form of Tcl_Format with functionality equivalent to:

Tcl_Obj *newPtr = Tcl_Format(interp, format, objc, objv);
if (newPtr == NULL) return TCL_ERROR;
Tcl_AppendObjToObj(objPtr, newPtr);
Tcl_DecrRefCount(newPtr);
return TCL_OK;

but with greater convenience and efficiency when the appending functionality is needed. Slay πŸ’…

Tcl_ObjPrintf serves as a replacement for the common sequence

char buf[SOME_SUITABLE_LENGTH];
sprintf(buf, format, ...);
Tcl_NewStringObj(buf, -1);

but with greater convenience and no need to determine SOME_SUITABLE_LENGTH. The formatting is done with the same core formatting engine used by Tcl_Format. This means the set of supported conversion specifiers is that of the format command but the behavior is as similar as possible to sprintf. The "hh" and (Microsoft-specific) "w" format specifiers are not supported. The "L" format specifier means that an "mp_int *" argument is expected (or a "long double" in combination with [aAeEgGaA]). When a conversion specifier passed to Tcl_ObjPrintf includes a precision, the value is taken as a number of bytes, as sprintf does, and not as a number of characters, as format does. This is done on the assumption that C code is more likely to know how many bytes it is passing around than the number of encoded characters those bytes happen to represent. The variable number of arguments passed in should be of the types that would be suitable for passing to sprintf. Note in this example usage, x is of type int.

int x = 5;
Tcl_Obj *objPtr = Tcl_ObjPrintf("Value is %d", x);

If the value of format contains internal inconsistencies or invalid specifier formats, the formatted string result produced by Tcl_ObjPrintf will be an error message describing the error. It is impossible however to provide runtime protection against mismatches between the format and any subsequent arguments. Compile-time protection may be provided by some compilers. πŸ™ˆ

Tcl_AppendPrintfToObj is an appending alternative form of Tcl_ObjPrintf with functionality equivalent to

Tcl_Obj *newPtr = Tcl_ObjPrintf(format, ...);
Tcl_AppendObjToObj(objPtr, newPtr);
Tcl_DecrRefCount(newPtr);

but with greater convenience and efficiency when the appending functionality is needed.

When printing integer types defined by Tcl, such as Tcl_Size or Tcl_WideInt, a format size specifier is needed as the integer width of those types is dependent on the Tcl version, platform and compiler. To accomodate these differences, Tcl defines C preprocessor symbols TCL_LL_MODIFER and TCL_SIZE_MODIFER for use when formatting values of type Tcl_WideInt and Tcl_Size respectively. Their usage is illustrated by

Tcl_WideInt wide;
Tcl_Size len;
Tcl_Obj *wideObj = Tcl_ObjPrintf("wide = %" TCL_LL_MODIFIER "d", wide);
Tcl_Obj *lenObj = Tcl_ObjPrintf("len = %" TCL_SIZE_MODIFIER "d", len);

(Yes, bestie, the prose says MODIFER and the code says MODIFIER β€” that's literally how it's written, don't come for me πŸ’€.)

The Tcl_SetObjLength procedure changes the length of the string value of its objPtr argument. If the newLength argument is greater than the space allocated for the value's string, then the string space is reallocated and the old value is copied to the new space; the bytes between the old length of the string and the new length may have arbitrary values. If the newLength argument is less than the current length of the value's string, with objPtr->length is reduced without reallocating the string space; the original allocated size for the string is recorded in the value, so that the string length can be enlarged in a subsequent call to Tcl_SetObjLength without reallocating storage. In all cases Tcl_SetObjLength leaves a null character at objPtr->bytes[newLength]. πŸ“

Tcl_AttemptSetObjLength is identical in function to Tcl_SetObjLength except that if sufficient memory to satisfy the request cannot be allocated, it does not cause the Tcl interpreter to panic. Thus, if newLength is greater than the space allocated for the value's string, and there is not enough memory available to satisfy the request, Tcl_AttemptSetObjLength will take no action and return 0 to indicate failure. If there is enough memory to satisfy the request, Tcl_AttemptSetObjLength behaves just like Tcl_SetObjLength and returns 1 to indicate success. No panicking, just vibes. 🌈

The Tcl_ConcatObj function returns a new string value whose value is the space-separated concatenation of the string representations of all of the values in the objv array. Tcl_ConcatObj eliminates leading and trailing white space as it copies the string representations of the objv array to the result. If an element of the objv array consists of nothing but white space, then that value is ignored entirely. This white-space removal was added to make the output of the concat command cleaner-looking. Tcl_ConcatObj returns a pointer to a newly-created value whose ref count is zero. πŸ‘‘

Tcl_IsEmpty is the vibe check function πŸ’… it returns 1 if objPtr is the empty string, and 0 if it's not. Bestie, the icon move here is that it does not generate the string representation (unless there is literally no other way to do it) β€” so you can call it on lists with billions of elements, or on any other data structure where building the string rep is impossible or just lowkey expensive. Go off, queen πŸ‘‘


✨ REFERENCE COUNT MANAGEMENT ✨

Okay so this is the part where you gotta keep track of who owns what, and it's giving ✨responsibility✨ 🫢

Tcl_NewStringObj, Tcl_NewUnicodeObj, Tcl_Format, Tcl_ObjPrintf, and Tcl_ConcatObj always return a zero-reference object, much like Tcl_NewObj does. So refreshing β€” the object is yours now, you gotta take care of it πŸŽ€

Tcl_GetStringFromObj, Tcl_GetString, Tcl_GetUnicodeFromObj, Tcl_GetUnicode, Tcl_GetUniChar, Tcl_GetCharLength, and Tcl_GetRange all only work with an existing value β€” they are absolutely not here to manipulate its reference count in any way. They just look, they don't touch πŸ‘€πŸ’…

Tcl_SetStringObj, Tcl_SetUnicodeObj, Tcl_AppendToObj, Tcl_AppendUnicodeToObj, Tcl_AppendObjToObj, Tcl_AppendStringsToObj, Tcl_AppendStringsToObjVA, Tcl_AppendLimitedToObj, Tcl_AppendFormatToObj, Tcl_AppendPrintfToObj, Tcl_SetObjLength, and Tcl_AttemptSetObjLength β€” these ones require their objPtr to be an unshared value (i.e., a reference count no more than 1), because bestie they will modify it. No sharing allowed, that's the whole boundary βœ‹πŸ˜€

Additional arguments to the above functions can have any reference count β€” I'm talking about the appendObjPtr argument to Tcl_AppendObjToObj, and the values in the objv argument to Tcl_Format, Tcl_AppendFormatToObj, and Tcl_ConcatObj. Any ref count is fine, but... reference counts of zero are not recommended πŸ™ˆ (like, technically allowed, but you're on your own, girl)

Also important, no cap: Tcl_Format and Tcl_AppendFormatToObj may modify the interpreter result, and that involves changing the reference count of a value. So keep that in mind before you go touching things πŸ‘‘πŸ”₯


πŸ¦‹ SEE ALSO πŸ¦‹


🌸 KEYWORDS 🌸

append, internal representation, value, value type, string value, string type, string representation, concat, concatenate, unicode


And that's the whole page, bestie β€” you ate that πŸ’…βœ¨ This lil' masterpiece is from Tcl 8.1, dated back in the day, and it's Tcl_StringObj(3) β€” iconic, timeless, periodt πŸŒˆπŸ‘‘

πŸ“¦ where this page comes from
  • nixpkgs unstable: tcl 9.1.0
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms