diff options
author | Nicolas Hillegeer <nicolas@hillegeer.com> | 2014-05-17 22:01:57 +0200 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-05-18 06:45:39 -0300 |
commit | 9eb68075d80a755bf546b20f99a36a542de1ecfd (patch) | |
tree | e644d09ee94d72c528e9ae8b95b74395497d5467 | |
parent | b591447f772ed5411e207854b2125bff08902bf7 (diff) | |
download | rneovim-9eb68075d80a755bf546b20f99a36a542de1ecfd.tar.gz rneovim-9eb68075d80a755bf546b20f99a36a542de1ecfd.tar.bz2 rneovim-9eb68075d80a755bf546b20f99a36a542de1ecfd.zip |
api/helpers: implement C string to String helper
Use it in buffers.c
-rw-r--r-- | src/nvim/api/buffer.c | 8 | ||||
-rw-r--r-- | src/nvim/api/helpers.c | 12 | ||||
-rw-r--r-- | src/nvim/api/helpers.h | 7 |
3 files changed, 22 insertions, 5 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 49b34e8dff..e73823e5c3 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -108,8 +108,8 @@ StringArray buffer_get_slice(Buffer buffer, goto end; } - rv.items[i].data = xstrdup((char *)ml_get_buf(buf, (linenr_T)lnum, false)); - rv.items[i].size = strlen(rv.items[i].data); + const char *bufstr = (char *) ml_get_buf(buf, (linenr_T) lnum, false); + rv.items[i] = cstr_to_string(bufstr); } end: @@ -307,9 +307,7 @@ String buffer_get_name(Buffer buffer, Error *err) return rv; } - rv.data = xstrdup((char *)buf->b_ffname); - rv.size = strlen(rv.data); - return rv; + return cstr_to_string((char *)buf->b_ffname); } void buffer_set_name(Buffer buffer, String name, Error *err) diff --git a/src/nvim/api/helpers.c b/src/nvim/api/helpers.c index 88ec12f3b5..3256abeeee 100644 --- a/src/nvim/api/helpers.c +++ b/src/nvim/api/helpers.c @@ -339,6 +339,18 @@ tabpage_T * find_tab(Tabpage tabpage, Error *err) return rv; } +String cstr_to_string(const char *str) { + if (str == NULL) { + return (String) { .data = NULL, .size = 0 }; + } + + size_t len = strlen(str); + return (String) { + .data = xmemdup(str, len), + .size = len + }; +} + static bool object_to_vim(Object obj, typval_T *tv, Error *err) { tv->v_type = VAR_UNKNOWN; diff --git a/src/nvim/api/helpers.h b/src/nvim/api/helpers.h index c9c7e9cdc8..f93487d62f 100644 --- a/src/nvim/api/helpers.h +++ b/src/nvim/api/helpers.h @@ -86,5 +86,12 @@ win_T * find_window(Window window, Error *err); /// @return the tabpage pointer tabpage_T * find_tab(Tabpage tabpage, Error *err); +/// Copies a C string into a String (binary safe string, characters + length) +/// +/// @param str the C string to copy +/// @return the resulting String, if the input string was NULL, then an +/// empty String is returned +String cstr_to_string(const char *str); + #endif // NVIM_API_HELPERS_H |