c3c
c3c copied to clipboard
Standard library String
There are 4 possible designs:
- String is stringbuilder type, non-opaque, char subarrays used where needed. ABI stability concerns.
- Same as (1) but String is opaque (i.e. just a pointer to it), which allows less static creation of it (but might not be needed). Better ABI stability.
- String is a distinct char subarray type with additional types. Problem is that a substring may revert to being a basic char subarray. So
given
String a = "123"thena[1..2]may yield achar[]substring rather than String substring. Added language features may fix that. - As (2) but with capacity prepended the pointer. Basically one less indirection, but value is not stable under mutation.
// 1
String foo;
foo.append("hello");
foo.delete()
// 2
String foo = string::new();
foo.append("hello");
foo.delete();
// 3
String foo = "hello";
String bar = foo[1..2]; // <- needs compiler support
// 4
String foo = string::new();
String bar = foo; // Bad
foo.append("hello");
// bar may be dangling now!
foo.destroy();
// bar is most certainly dangling here