WriteString bug???

BlitzPlus Forums/BlitzPlus Programming/WriteString bug???


I know, I can write my own string write/read functions.

And I know that 2 bytes isn't the end of the world.

But why didn't anyone deciding the format of a string stored in this way (as opposed to null-terminated) do it with a byte for the number of bytes for the length? In 90% of cases it would save 2 bytes per string. It's not much, but it's still a fairly obvious way to do it, and if it saves space, why not?



Decideing the string format?

Um...there really is no set in stone string format, the CPU can't tell the difference between a 4 byte number stored in one of it's registers and a string of 4 characters without some extra overhead provided by the program provided opcodes to present the string onscreen through a font or "print text" routine.

As such it is up to the developers of the compiler to implament string formats...and Mark has chosen to go with a simple but very flexable structure...internaly within Blitz it makes a lot of sense to use a 32-bit integer to store the length of the associated string...Remember that a string can be passed to various functions (actually a memory "pointer" is PUSHed onto the stack, to be POPed once the function code executes)...and if you had several versions of a string (a "short" string with a 1 byte header..."medium" string with 2 byte header, etc) then this will cause all sorts of problems as during the execution of your application you may pass the same function routine a "short" string most of the time, but every so often a "medium" string is passed because you are 1 character over the 256 character "short" string limit...it is then up to the code in the function to decifer just what sort of string it has been passed...by keeping it simple Blitz can use the exact same core opcodes for strings everywhere within the application...

You can write your own string file saveing functions...as you have direct knowledge of what strings you will need, how long they are, etc...Mark didn't know what you had in mind when developing Blitz so he kept things much simpler to accomidate as many programing situations as possable...

I use strings for just about everything, from storeing text as strings are designed to do...to useing them to store graphics, linked lists with parent/child relationships, radix sorting routines, just about everything where I need to have axcess to individual bytes (chars)...strange but true.