diff options
-rw-r--r-- | generic/tcl.decls | 4 | ||||
-rw-r--r-- | generic/tclDecls.h | 6 | ||||
-rw-r--r-- | generic/tclObj.c | 80 | ||||
-rw-r--r-- | generic/tclStubInit.c | 1 |
4 files changed, 90 insertions, 1 deletions
diff --git a/generic/tcl.decls b/generic/tcl.decls index 707420d..24484a5 100644 --- a/generic/tcl.decls +++ b/generic/tcl.decls @@ -2327,10 +2327,12 @@ declare 630 { # ----- BASELINE -- FOR -- 8.6.0 ----- # # TIP #445 - declare 631 { void Tcl_FreeIntRep(Tcl_Obj *objPtr) } +declare 632 { + char *Tcl_InitStringRep(Tcl_Obj *objPtr, const char *bytes, int numBytes) +} # ----- BASELINE -- FOR -- 8.7.0 ----- # diff --git a/generic/tclDecls.h b/generic/tclDecls.h index 4275b91..3e2ac18 100644 --- a/generic/tclDecls.h +++ b/generic/tclDecls.h @@ -1818,6 +1818,9 @@ EXTERN void Tcl_ZlibStreamSetCompressionDictionary( Tcl_Obj *compressionDictionaryObj); /* 631 */ EXTERN void Tcl_FreeIntRep(Tcl_Obj *objPtr); +/* 632 */ +EXTERN char * Tcl_InitStringRep(Tcl_Obj *objPtr, const char *bytes, + int numBytes); typedef struct { const struct TclPlatStubs *tclPlatStubs; @@ -2485,6 +2488,7 @@ typedef struct TclStubs { int (*tcl_FSUnloadFile) (Tcl_Interp *interp, Tcl_LoadHandle handlePtr); /* 629 */ void (*tcl_ZlibStreamSetCompressionDictionary) (Tcl_ZlibStream zhandle, Tcl_Obj *compressionDictionaryObj); /* 630 */ void (*tcl_FreeIntRep) (Tcl_Obj *objPtr); /* 631 */ + char * (*tcl_InitStringRep) (Tcl_Obj *objPtr, const char *bytes, int numBytes); /* 632 */ } TclStubs; extern const TclStubs *tclStubsPtr; @@ -3779,6 +3783,8 @@ extern const TclStubs *tclStubsPtr; (tclStubsPtr->tcl_ZlibStreamSetCompressionDictionary) /* 630 */ #define Tcl_FreeIntRep \ (tclStubsPtr->tcl_FreeIntRep) /* 631 */ +#define Tcl_InitStringRep \ + (tclStubsPtr->tcl_InitStringRep) /* 632 */ #endif /* defined(USE_TCL_STUBS) */ diff --git a/generic/tclObj.c b/generic/tclObj.c index b2b962c..7fe0293 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -17,6 +17,7 @@ #include "tclInt.h" #include "tommath.h" #include <math.h> +#include <assert.h> /* * Table of all object types. @@ -1700,6 +1701,85 @@ Tcl_GetStringFromObj( /* *---------------------------------------------------------------------- * + * Tcl_InitStringRep -- + * + * This function is called in several configurations to provide all + * the tools needed to set an object's string representation. The + * function is determined by the arguments. + * + * (objPtr->bytes != NULL && bytes != NULL) || (numBytes < 0) + * Invalid call -- panic! + * + * objPtr->bytes == NULL && bytes == NULL && numBytes >= 0 + * Allocation only - allocate space for (numBytes+1) chars. + * store in objPtr->bytes and return. Also sets + * objPtr->length to 0 and objPtr->bytes[0] to NUL. + * + * objPtr->bytes == NULL && bytes != NULL && numBytes >= 0 + * Allocate and copy. bytes is assumed to point to chars to + * copy into the string rep. objPtr->length = numBytes. Allocate + * array of (numBytes + 1) chars. store in objPtr->bytes. Copy + * numBytes chars from bytes to objPtr->bytes; Set + * objPtr->bytes[numBytes] to NUL and return objPtr->bytes. + * Caller must guarantee there are numBytes chars at bytes to + * be copied. + * + * objPtr->bytes != NULL && bytes == NULL && numBytes >= 0 + * Truncate. Set objPtr->length to numBytes and + * objPr->bytes[numBytes] to NUL. Caller has to guarantee + * that a prior allocating call allocated enough bytes for + * this to be valid. Return objPtr->bytes. + * + * Caller is expected to ascertain that the bytes copied into + * the string rep make up complete valid UTF-8 characters. + * + * Results: + * A pointer to the string rep of objPtr. + * + * Side effects: + * As described above. + * + *---------------------------------------------------------------------- + */ + +char * +Tcl_InitStringRep( + Tcl_Obj *objPtr, /* Object whose string rep is to be set */ + const char *bytes, + int numBytes) +{ + assert(numBytes >= 0); + + assert(objPtr->bytes == NULL || bytes == NULL); + + /* Allocate */ + if (objPtr->bytes == NULL) { + /* Allocate only as empty - extend later if bytes copied */ + objPtr->length = 0; + if (numBytes) { + objPtr->bytes = (char *)ckalloc((unsigned)(numBytes+1)); + if (bytes) { + /* Copy */ + memcpy(objPtr->bytes, bytes, (unsigned) numBytes); + objPtr->length = numBytes; + } + } else { + objPtr->bytes = tclEmptyStringRep; + } + } else { + /* objPtr->bytes != NULL bytes == NULL - Truncate */ + objPtr->length = numBytes; + } + + /* Terminate */ + objPtr->bytes[objPtr->length] = '\0'; + + return objPtr->bytes; +} + +/* + *---------------------------------------------------------------------- + * * Tcl_InvalidateStringRep -- * * This function is called to invalidate an object's string diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index 83dd9d6..775d4ac 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -1416,6 +1416,7 @@ const TclStubs tclStubs = { Tcl_FSUnloadFile, /* 629 */ Tcl_ZlibStreamSetCompressionDictionary, /* 630 */ Tcl_FreeIntRep, /* 631 */ + Tcl_InitStringRep, /* 632 */ }; /* !END!: Do not edit above this line. */ |