Merge #708 'Remove NULL/non-NULL tests after vim_str(n)save'

- replace alloc with xmalloc
This commit is contained in:
Justin M. Keyes
2014-05-22 12:50:59 -04:00
55 changed files with 937 additions and 1643 deletions

View File

@@ -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);
char_u *p = res;
while (*p != NUL) {
int l;
if (res != NULL)
while (*p != NUL) {
int l;
if (enc_utf8) {
int c, uc;
int newl;
char_u *s;
if (enc_utf8) {
int c, uc;
int newl;
char_u *s;
c = utf_ptr2char(p);
uc = utf_toupper(c);
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++;
/* 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;
}