6 ms·
The example has functions' string parameters and separate parameters for the string's length. Is this really necessary what happens if the declared length is mo
by polotics 8y ago
The example has functions' string parameters and separate parameters for the string's length. Is this really necessary what happens if the declared length is more than the actual length?
- tom_mellior 8y agoKnowing nothing about this, I would guess that this is to allow sending binary data that contains embedded null bytes? Though I guess it would be good to have nicer wrappers for the common case where you do only send text.
- loeg 8y agoProbably they use length-prefixed strings internally (which is probably a requirement for taking a C web framework seriously), but allow C nul-terminated strings at the API boundary for convenience of use.
- pantalaimon 8y agoNo it is not necessary. If no length is given, the string is assumed to be NULL-terminated. It's a bit unfortunate that they use two code paths for that which are basically just copy & paste. https://github.com/boazsegev/facil.io/blob/master/lib/facil/http/http.c#L193 https://github.com/boazsegev/facil.io/blob/master/lib/facil/...
- bowild 8y agoThis approach moves an `if` statement out of the loop, so there's no need to repeat it.
- bowild 8y agoActually, it's to allow for non-NUL terminated strings. A good example is when using a String supplied by a scripting language such as Ruby or JavaScript. Normally, you might need to copy the string to add a NUL byte, or request that the scripting engine provide a NUL terminated string (which might result in the same action performed). Using this optional approach, a potential copy of the data can be avoided.