diff options
author | dgp <dgp@users.sourceforge.net> | 2020-03-13 18:32:45 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2020-03-13 18:32:45 (GMT) |
commit | fdfd0ad0b667f995f43147ca8504fc3bdb873e61 (patch) | |
tree | e12d2ac3d32284db6fab0d67a2b419eade3e5845 /generic/tclBinary.c | |
parent | f7550ff059c94a5139b1387ed8cf2e02072d743f (diff) | |
download | tcl-fdfd0ad0b667f995f43147ca8504fc3bdb873e61.zip tcl-fdfd0ad0b667f995f43147ca8504fc3bdb873e61.tar.gz tcl-fdfd0ad0b667f995f43147ca8504fc3bdb873e61.tar.bz2 |
New internal routine TclGetBytesFromObj().
Aimed to become public, see TIP 568 in progress.
Diffstat (limited to 'generic/tclBinary.c')
-rw-r--r-- | generic/tclBinary.c | 65 |
1 files changed, 55 insertions, 10 deletions
diff --git a/generic/tclBinary.c b/generic/tclBinary.c index f7fdd9f..b27a790 100644 --- a/generic/tclBinary.c +++ b/generic/tclBinary.c @@ -436,6 +436,53 @@ Tcl_SetByteArrayObj( /* *---------------------------------------------------------------------- * + * TclGetBytesFromObj -- + * + * Attempt to extract the value from objPtr in the representation + * of a byte sequence. On success return the extracted byte sequence. + * On failures, return NULL and record error message and code in + * interp (if not NULL). + * + * Results: + * Pointer to array of bytes, or NULL. representing the ByteArray object. + * Writes number of bytes in array to *lengthPtr. + * + *---------------------------------------------------------------------- + */ + +unsigned char * +TclGetBytesFromObj( + Tcl_Interp *interp, /* For error reporting */ + Tcl_Obj *objPtr, /* Value to extract from */ + int *lengthPtr) /* If non-NULL, filled with length of the + * array of bytes in the ByteArray object. */ +{ + ByteArray *baPtr; + const Tcl_ObjIntRep *irPtr = TclFetchIntRep(objPtr, &properByteArrayType); + + if (irPtr == NULL) { + SetByteArrayFromAny(NULL, objPtr); + irPtr = TclFetchIntRep(objPtr, &properByteArrayType); + if (irPtr == NULL) { + if (interp) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "expected bytes but got non-byte character")); + Tcl_SetErrorCode(interp, "TCL", "VALUE", "BYTES", NULL); + } + return NULL; + } + } + baPtr = GET_BYTEARRAY(irPtr); + + if (lengthPtr != NULL) { + *lengthPtr = baPtr->used; + } + return baPtr->bytes; +} + +/* + *---------------------------------------------------------------------- + * * Tcl_GetByteArrayFromObj -- * * Attempt to get the array of bytes from the Tcl object. If the object @@ -458,18 +505,16 @@ Tcl_GetByteArrayFromObj( * array of bytes in the ByteArray object. */ { ByteArray *baPtr; - const Tcl_ObjIntRep *irPtr = TclFetchIntRep(objPtr, &properByteArrayType); + const Tcl_ObjIntRep *irPtr; + unsigned char *result = TclGetBytesFromObj(NULL, objPtr, lengthPtr); - if (irPtr == NULL) { - irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); - if (irPtr == NULL) { - SetByteArrayFromAny(NULL, objPtr); - irPtr = TclFetchIntRep(objPtr, &properByteArrayType); - if (irPtr == NULL) { - irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); - } - } + if (result) { + return result; } + + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); + assert(irPtr != NULL); + baPtr = GET_BYTEARRAY(irPtr); if (lengthPtr != NULL) { |