diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2023-06-02 12:00:02 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2023-06-02 12:00:02 (GMT) |
commit | 2867ac26373a64724686df3d77d9d323343bcc84 (patch) | |
tree | ae328d054031055467a0fc8c3767e56431e69f36 /generic/tclInt.h | |
parent | 56d50aec7cab7011ab2b1e430403aa92d76e313c (diff) | |
download | tcl-2867ac26373a64724686df3d77d9d323343bcc84.zip tcl-2867ac26373a64724686df3d77d9d323343bcc84.tar.gz tcl-2867ac26373a64724686df3d77d9d323343bcc84.tar.bz2 |
New (internal) macro TclAttemptInitStringRep()
Diffstat (limited to 'generic/tclInt.h')
-rw-r--r-- | generic/tclInt.h | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/generic/tclInt.h b/generic/tclInt.h index e2c0bde..d40bbe5 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -4475,7 +4475,7 @@ MODULE_SCOPE void TclDbInitNewObj(Tcl_Obj *objPtr, const char *file, /* *---------------------------------------------------------------- - * Macro used by the Tcl core to set a Tcl_Obj's string representation to a + * Macros used by the Tcl core to set a Tcl_Obj's string representation to a * copy of the "len" bytes starting at "bytePtr". The value of "len" must * not be negative. When "len" is 0, then it is acceptable to pass * "bytePtr" = NULL. When "len" > 0, "bytePtr" must not be NULL, and it @@ -4488,17 +4488,22 @@ MODULE_SCOPE void TclDbInitNewObj(Tcl_Obj *objPtr, const char *file, * Because "len" is referenced multiple times, take care that it is an * expression with the same value each use. * - * The ANSI C "prototype" for this macro is: + * The ANSI C "prototypes" for these macros are: * + * MODULE_SCOPE void TclInitEmptyStringRep(Tcl_Obj *objPtr); * MODULE_SCOPE void TclInitStringRep(Tcl_Obj *objPtr, char *bytePtr, size_t len); + * MODULE_SCOPE void TclAttemptInitStringRep(Tcl_Obj *objPtr, char *bytePtr, size_t len); * *---------------------------------------------------------------- */ +#define TclInitEmptyStringRep(objPtr) \ + ((objPtr)->length = (((objPtr)->bytes = &tclEmptyString), 0)) + + #define TclInitStringRep(objPtr, bytePtr, len) \ if ((len) == 0) { \ - (objPtr)->bytes = &tclEmptyString; \ - (objPtr)->length = 0; \ + TclInitEmptyStringRep(objPtr); \ } else { \ (objPtr)->bytes = (char *)ckalloc((len) + 1U); \ memcpy((objPtr)->bytes, (bytePtr) ? (bytePtr) : &tclEmptyString, (len)); \ @@ -4506,6 +4511,16 @@ MODULE_SCOPE void TclDbInitNewObj(Tcl_Obj *objPtr, const char *file, (objPtr)->length = (len); \ } +#define TclAttemptInitStringRep(objPtr, bytePtr, len) \ + ((((len) == 0) ? ( \ + TclInitEmptyStringRep(objPtr) \ + ) : ( \ + (objPtr)->bytes = (char *)attemptckalloc((len) + 1U), \ + memcpy((objPtr)->bytes, (bytePtr) ? (bytePtr) : &tclEmptyString, (len)), \ + (objPtr)->bytes[len] = '\0', \ + (objPtr)->length = (len) \ + )), (objPtr)->bytes) + /* *---------------------------------------------------------------- * Macro used by the Tcl core to get the string representation's byte array |