I'm quite surprised that Nim's strings are null-terminated, and have a length field. According to a forum article[1], it is mainly due to the C interoperability.
I understand that FFI is kinda important for system languages, but isn't it giving up too much to disallow null characters in the middle of a string? Null-terminated strings generally work well, but there have been some corner cases that made me annoyed. (Such as PHP's preg_* functions)
This is actually really common in C++ libraries. I even worked in one (which I know was on Windows, but I don't think was MFC; maybe OWL?) where the pointers to the String object were actually pointing directly to the null-terminated C string, and then each function in the class would start by fixing up the pointer. E.g., something like
class StringClass {
...all members, and then...
char *str;
};
StringClass *StringClass::new() {
StringClass st = new StringClass;
...
return (StringClass *)st->str;
}
void StringClass::someFunc() {
StringClass *realThis = this - sizeof(StringClass) + sizeof(char *);
...
}
Evil, eh?
The good news is that, no, this does not have to prevent you having nulls in your string. The way this usually works is that all of the language's native string handling routines just use the length, so that's fine, and you're at least protected if you want to call C string handling routines instead. This can result in some unexpected behavior on the C side, but it beats the alternative.
> but isn't it giving up too much to disallow null characters in the middle of a string?
You can have null characters in the middle of strings:
var s = "foo"
s[1] = '\0'
echo len(s)
echo s[2]
Nim will always append a null character to a string for when it gets passed to a C function, but Nim's string functions work just fine with the string proper containing null characters also.
I understand that FFI is kinda important for system languages, but isn't it giving up too much to disallow null characters in the middle of a string? Null-terminated strings generally work well, but there have been some corner cases that made me annoyed. (Such as PHP's preg_* functions)
[1] http://forum.nimrod-lang.org/t/125