4 ms·
OT: Is there any API out there that implements say a length* prefix in all C strings? * 2 or 4 octets size
by lota-putty 7y ago
OT: Is there any API out there that implements say a length* prefix in all C strings?
* 2 or 4 octets size
- ninkendo 7y agoWhat you're talking about typically goes by the name of "pascal strings", and while they're possible to do, C's string literals are not compatible with them, so nobody does it.
- tyingq 7y agosds (simple dynamic strings) is a decent compromise: https://github.com/antirez/sds https://github.com/antirez/sds (used in redis)
- childintime 7y agoIt is certainly possible to declare Pascal string literals with not much hassle: https://stackoverflow.com/questions/7648947/declaring-pascal-style-strings-in-c https://stackoverflow.com/questions/7648947/declaring-pascal... One of the answers states that GCC and Clang do have support for Pascal strings. Probably these strings do not work as (well) in #defines, i.e. they don't concat like regular literals.
- avar 7y agoYes, those are a dime a dozen. E.g. GString[1] is one example. Rather than strict "Pascal strings" like the sibling comments rightly points out would be stupid, they're a light struct with a char/size_t len (and usually also size_t alloc_len). This allows for skipping strlen() on them before e.g. copying, while using them as a C string by getting directly at the char. 1. https://developer.gnome.org/glib/stable/glib-Strings.html#GString https://developer.gnome.org/glib/stable/glib-Strings.html#GS...
- zokier 7y agosds comes to mind, but there are like gazillion different implementations of the same concept. https://github.com/antirez/sds https://github.com/antirez/sds
- pravus 7y agoThe PJ-SIP project defines their own length-based string wrapper for C strings. https://github.com/pjsip/pjproject https://github.com/pjsip/pjproject https://www.pjsip.org/pjlib/docs/html/structpj__str__t.htm https://www.pjsip.org/pjlib/docs/html/structpj__str__t.htm