diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-05-22 12:50:59 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-05-22 13:00:51 -0400 |
commit | e2e47803bdfd5fb40e3dbc9cdf798bb27d306c72 (patch) | |
tree | 6ff1b06b5d5fd6d3260f3a778c33cfaf03f0c295 /src/nvim/strings.c | |
parent | 0aa8b5828cc0674894681841f40c3c05bfd2f07b (diff) | |
parent | e303a11ebfc352860cce73184ece692ab4d0f01c (diff) | |
download | rneovim-e2e47803bdfd5fb40e3dbc9cdf798bb27d306c72.tar.gz rneovim-e2e47803bdfd5fb40e3dbc9cdf798bb27d306c72.tar.bz2 rneovim-e2e47803bdfd5fb40e3dbc9cdf798bb27d306c72.zip |
Merge #708 'Remove NULL/non-NULL tests after vim_str(n)save'
- replace alloc with xmalloc
Diffstat (limited to 'src/nvim/strings.c')
-rw-r--r-- | src/nvim/strings.c | 90 |
1 files changed, 37 insertions, 53 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 50669dfcfb..d8a14c1883 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -45,14 +45,7 @@ */ char_u *vim_strsave(char_u *string) { - char_u *p; - unsigned len; - - len = (unsigned)STRLEN(string) + 1; - p = alloc(len); - if (p != NULL) - memmove(p, string, (size_t)len); - return p; + return (char_u *)xstrdup((char *)string); } /* @@ -63,12 +56,7 @@ char_u *vim_strsave(char_u *string) */ char_u *vim_strnsave(char_u *string, int len) { - char_u *p; - - p = alloc((unsigned)(len + 1)); - STRNCPY(p, string, len); - p[len] = NUL; - return p; + return (char_u *)strncpy(xmallocz(len), (char *)string, len); } /* @@ -108,7 +96,7 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b ++length; /* count a backslash */ ++length; /* count an ordinary char */ } - escaped_string = alloc(length); + escaped_string = xmalloc(length); p2 = escaped_string; for (p = string; *p; p++) { if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) { @@ -169,7 +157,7 @@ char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline } /* Allocate memory for the result and fill it. */ - escaped_string = alloc(length); + escaped_string = xmalloc(length); d = escaped_string; /* add opening quote */ @@ -253,49 +241,45 @@ void vim_strup(char_u *p) /* * Make string "s" all upper-case and return it in allocated memory. * Handles multi-byte characters as well as possible. - * Returns NULL when out of memory. */ char_u *strup_save(char_u *orig) { - char_u *p; - char_u *res; + char_u *res = vim_strsave(orig); - res = p = vim_strsave(orig); - - if (res != NULL) - while (*p != NUL) { - int l; - - if (enc_utf8) { - int c, uc; - int newl; - char_u *s; - - c = utf_ptr2char(p); - uc = utf_toupper(c); - - /* Reallocate string when byte count changes. This is rare, - * thus it's OK to do another malloc()/free(). */ - l = utf_ptr2len(p); - newl = utf_char2len(uc); - if (newl != l) { - s = alloc((unsigned)STRLEN(res) + 1 + newl - l); - memmove(s, res, p - res); - STRCPY(s + (p - res) + newl, p + l); - p = s + (p - res); - free(res); - res = s; - } - - utf_char2bytes(uc, p); - p += newl; - } else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) - p += l; /* skip multi-byte character */ - else { - *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ - p++; + char_u *p = res; + while (*p != NUL) { + int l; + + if (enc_utf8) { + int c, uc; + int newl; + char_u *s; + + c = utf_ptr2char(p); + uc = utf_toupper(c); + + /* Reallocate string when byte count changes. This is rare, + * thus it's OK to do another malloc()/free(). */ + l = utf_ptr2len(p); + newl = utf_char2len(uc); + if (newl != l) { + s = xmalloc(STRLEN(res) + 1 + newl - l); + memmove(s, res, p - res); + STRCPY(s + (p - res) + newl, p + l); + p = s + (p - res); + free(res); + res = s; } + + utf_char2bytes(uc, p); + p += newl; + } else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) + p += l; /* skip multi-byte character */ + else { + *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ + p++; } + } return res; } |