api: unify string conversions, simplify interop

- The data member of String's can now be passed directly to functions
  expecting C strings, as we now guarantee that they are NUL-terminated.
  This obviates the need to use xstrndup and free, simplifying code and
  enhancing performance.
- Use xmemdupz instead of xstrndup for converting String's into C strings.
  It's faster because it doesn't calculate strlen(string.data) (which is
  unnecesary as that information is already provided in string.size anyway).
- Use cstr_to_string to convert from C strings to String, it is both shorter
  and faster than the usual strlen/xstrndup combo, which calls strlen twice.
  cstr_to_string internally calls strlen and then xmemdupz.
This commit is contained in:
Nicolas Hillegeer
2014-05-31 13:34:41 +02:00
parent 563698b2dc
commit 54ca93465c
2 changed files with 32 additions and 56 deletions

View File

@@ -184,7 +184,7 @@ void buffer_set_slice(Buffer buffer,
for (size_t i = 0; i < new_len; i++) {
String l = replacement.items[i];
lines[i] = xstrndup(l.data, l.size);
lines[i] = xmemdupz(l.data, l.size);
}
try_start();
@@ -392,15 +392,12 @@ void buffer_set_name(Buffer buffer, String name, Error *err)
return;
}
aco_save_T aco;
int ren_ret;
char *val = xstrndup(name.data, name.size);
try_start();
// Using aucmd_*: autocommands will be executed by rename_buffer
aco_save_T aco;
aucmd_prepbuf(&aco, buf);
ren_ret = rename_buffer((char_u *)val);
free(val);
int ren_ret = rename_buffer((char_u *) name.data);
aucmd_restbuf(&aco);
if (try_end(err)) {