Why doesn't the null-terminator placed arbitrarily in a string terminate it?
Asked Answered
M

3

9

The null character or null-terminator (\0) is to be used to terminate a contiguous sequence of characters. I find that in C, I can add the character into a string at a random position and the string will be cut off from that point. For example:

char * s = "Hello\0World";

will result in s being equal to the string "Hello". In JavaScript, however, this is not the case:

var s = "Hello\0World";

The above won't work as expected. s will be equal to the string "HelloWorld".

Why doesn't this work?

Moluccas answered 26/9, 2012 at 14:25 Comment(2)
I think that Javascript is not C. so the null byte in a string is simply a null byte in a string.Broaddus
Most languages follow the Pascal tradition of representing strings as an array with an integer length (thus allowing the null character). C is actually the odd one out in this sense.Maffa
O
16

JavaScript does not use NULL terminated strings, while C does.

Javascript strings are stored by keeping track of the characters and the length separately instead of trying to assume that a NULL marks the end of the string.

Onagraceous answered 26/9, 2012 at 14:28 Comment(1)
What does JS use for strings?Moluccas
B
3

In Javascript a NULL byte in a String is simply a NULL byte in a string. If you want truncate the string

var s = "Hello\0World".split("\0").shift();

but in this case I think it not need to disturb the null byte :)

Broaddus answered 26/9, 2012 at 14:33 Comment(0)
P
3

The C string still points to an address in memory where "Hello\0World" is stored, only that most string handling functions considers 0 end of string. For some functions you must pass a string length argument, but most simply read until they find the null byte. In memory the string is actually "Hello\0World\0".

A JavaScript engine cannot determine string length by looking for a null byte, since you in such a case wouldn't ever be able to have a nullbyte inside a string. There's probably something about that in the specs. The engine must instead storing the length of the string separately, and then read that many characters from memory whenever you access the string.

And how to properly parse and store the size of buffers is something scripting languages usually try to hide from the user. That's half the purpose of scripting, to not require the programmer to worry about adding 0 to created character buffers and or storing string length separately so that string handling functions don't print a bunch of random characters outside your buffer looking for a nullbyte...

So exactly how does a JavaScript string behave? I don't know, it's probably up to the engine to describe its properties in depth. As long as you interface with the object like the specification says, it can be implemented in whatever manner, using structs for buffer and length, using a translation character for 0, using a linked list of characters, etc...

Polymath answered 26/9, 2012 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.