From: Joe E. <jen...@fl...> - 2012-12-29 20:11:48
|
Kieran Elby wrote: > [...] > Incidentally, [string bytelength $v] is not quite the same as > [string length [encoding convertto utf-8 $v]] - consider this example: > [...] > One use-case I've encountered for "string bytelength" is to help > truncate a string so that it's UTF-8 encoding will fit in a database > column whose length is specified in bytes. Which, as you point out above, is incorrect; [string bytelength $v] is not always the same as the number of bytes in the UTF-8 encoding of $v. > Another use-case is estimating the size of a Tcl value in memory - but > perhaps an info subcommand or a special debugging command for this > purpose would be more appropriate? This too will give inaccurate results. The amount of memory allocated for the string rep of a value is generally not the same as its [string bytelength] either. (Tcl will overallocate the string buffer under certain circumstances to help ensure amortized O(n) time for [append]). I think this just underscores my point. In every use case I've seen, [string bytelength] is nothing more than a cheap way to get the answer to the wrong question :-) --Joe English jen...@fl... |