Roderick <
hru...@gmail.com> wrote:
>
>
> On Thu, 27 Sep 2018, Brad Lanam wrote:
>
>> string bytelength is not useful. It returns a length used internally by Tcl
>> which doesn't have any meaning to the caller.
>> Really should never have been exposed.
>
> If I take a string from a file that I want to manipulate with tcl,
> I realy may need its byte lenth and not its string length.
Option 1: read the string in binary mode for the input channel, then
the bytelength exactly equals the string length.
Option 2: (for encoded data) use the appropriate 'encoding' incantation
to convert to an output string before output to the file, which gives a
binary string, where bytelength equals string length, then output to
the file with the file set to binary mode.
> I continously edit big, very big files or sets of big files with
> programs, sometimes C, lex, tcl, or scripts that call ed.
>
> Of course, the byte length is unnecessary if you remain in the
> frame of tcl, if all what your program does remain viewed by
> tcl.
The bytelength command does not give you 'bytelength' (the name is,
unfortunately, misleading). It gives you "storage in bytes" used by tcl
*in the frame of tcl*. You can not use 'bytelength' for the purposes
you indicate above, it will not give you correct results.
>> Always use string length.
>
> That is not always what I want, but really the byte length.
No, what you 'want' is to adjust the 'encoding' on your input/output
channels so that you read what you want and/or output what you want.
And if you do have a need to know "length in bytes in output file" then
you need to use binary mode on your channels and the 'encoding' command
to encode in order to obtain binary data from non-binary Tcl strings in
order to measure the length in bytes of an output item in a file.
> Should I use it "always", even if that is not what I want and need?
It (bytelength) is never going to do what you appear to think it does.