mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
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:
parent
e1793949ab
commit
563698b2dc
@ -352,19 +352,22 @@ tabpage_T * find_tab(Tabpage tabpage, Error *err)
|
|||||||
return rv;
|
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
|
/// @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
|
/// empty String is returned
|
||||||
String cstr_to_string(const char *str) {
|
String cstr_to_string(const char *str)
|
||||||
|
{
|
||||||
if (str == NULL) {
|
if (str == NULL) {
|
||||||
return (String) STRING_INIT;
|
return (String) STRING_INIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
return (String) {
|
return (String) {
|
||||||
.data = xmemdup(str, len),
|
.data = xmemdupz(str, len),
|
||||||
.size = len
|
.size = len
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user