| Jeff Hobbs 2007-11-25, 7:18 pm |
| thelfter@gmail.com wrote:
> I'm working on adding some Unicode database support to a package I
> maintain and I've gotten to the point where I can bind and retrieve
> unicode objects when I set a flag to do so.
>
> However it occurred to me that it might be better to just 'magically'
> handle the situation... So on to my questions
>
> Is there a way to tell if the result from Tcl_GetUnicodeFromObj() is
> actually a unicode object, meaning that it cannot be represented
> accurately by a Tcl_GetStringFromObj().
>
> Inversely, if I have a pointer to an area of memory.. casting it as
> either as a (uchar) or a (char) is easy, but again is there an easy
> way to tell if such a thing is needed.
I have an answer ... but it would need to be handled differently. I'm
considering the following chunk of code to be placed in tclStringObj.c:
int
TclStringObjIsAscii(
Tcl_Obj *objPtr)
{
if ((objPtr->typePtr == &tclStringType)
&& (objPtr->bytes != NULL)) {
String *stringPtr = GET_STRING(objPtr);
if (stringPtr->hasUnicode && (stringPtr->numChars >= 0)) {
return 1;
}
}
return 0;
}
This is the magic to tell you that an object represents itself as a
unicode object where the 'bytes' == 'unicode', IOW, everything in the
string is ascii. Now ... what might you do with this? In your case,
you would call it prior to Tcl_GetUnicodeFromObj, because that will
certainly create the unicode if it doesn't already exist. Mind you, the
above API doesn't tell you if there is no unicode string already, its
purpose is to say that bytes has a "correct" value, should you want to
use something with a char* API over Tcl_UniChar*.
This isn't in the core at this time, but it could be added. I thought I
was going to use it myself (and may have more use in the core itself),
but perhaps you have better supporting cases?
Jeff
|