api: also NUL-terminate Strings made from cstrs

I believe we can now mostly assume that all encountered String's data
members are safe to pass into functions that accept C strings. That should
simplify interop with C string code.
This commit is contained in:
Nicolas Hillegeer 2014-05-30 18:45:11 +02:00
parent e1793949ab
commit 563698b2dc

View File

@ -352,19 +352,22 @@ tabpage_T * find_tab(Tabpage tabpage, Error *err)
return rv;
}
/// Copies a C string into a String (binary safe string, characters + length)
/// Copies a C string into a String (binary safe string, characters + length).
/// The resulting string is also NUL-terminated, to facilitate interoperating
/// with code using C strings.
///
/// @param str the C string to copy
/// @return the resulting String, if the input string was NULL, then an
/// @return the resulting String, if the input string was NULL, an
/// empty String is returned
String cstr_to_string(const char *str) {
String cstr_to_string(const char *str)
{
if (str == NULL) {
return (String) STRING_INIT;
}
size_t len = strlen(str);
return (String) {
.data = xmemdup(str, len),
.data = xmemdupz(str, len),
.size = len
};
}