tweaks: rename a function and elide its first parameter

master
Benno Schulenberg 2019-10-14 19:51:24 +02:00
parent 46566572d6
commit 17c16a4bf5
6 changed files with 19 additions and 13 deletions

View File

@ -244,7 +244,7 @@ char *make_mbchar(long code, int *length)
} else
#endif
{
mb_char = mallocstrncpy(NULL, (char *)&code, 1);
mb_char = measured_copy((char *)&code, 1);
*length = 1;
}

View File

@ -2243,7 +2243,7 @@ char *real_dir_from_tilde(const char *path)
#ifdef HAVE_PWD_H
const struct passwd *userdata;
tilded = mallocstrncpy(NULL, path, i + 1);
tilded = measured_copy(path, i + 1);
tilded[i] = '\0';
do {

View File

@ -226,7 +226,7 @@ void partition_buffer(linestruct *top, size_t top_x,
* top of the partition from it, and save the text before top_x. */
foreline = top->prev;
top->prev = NULL;
antedata = mallocstrncpy(NULL, top->data, top_x + 1);
antedata = measured_copy(top->data, top_x + 1);
antedata[top_x] = '\0';
/* Remember which line is below the bottom of the partition, detach the

View File

@ -578,7 +578,7 @@ const char *strstrwrapper(const char *haystack, const char *needle,
const char *start);
void *nmalloc(size_t howmuch);
void *nrealloc(void *ptr, size_t howmuch);
char *mallocstrncpy(char *dest, const char *src, size_t n);
char *measured_copy(const char *src, size_t n);
char *mallocstrcpy(char *dest, const char *src);
char *copy_of(const char *string);
char *free_and_assign(char *dest, char *src);

View File

@ -270,7 +270,7 @@ void do_unindent(void)
* possible, and saving the removed whitespace in the undo item. */
for (line = top; line != bot->next; line = line->next) {
size_t indent_len = length_of_white(line->data);
char *indentation = mallocstrncpy(NULL, line->data, indent_len + 1);
char *indentation = measured_copy(line->data, indent_len + 1);
indentation[indent_len] = '\0';
@ -587,7 +587,7 @@ void do_undo(void)
}
t = make_new_node(f);
t->data = copy_of(u->strdata);
data = mallocstrncpy(NULL, f->data, u->mark_begin_x + 1);
data = measured_copy(f->data, u->mark_begin_x + 1);
data[u->mark_begin_x] = '\0';
free(f->data);
f->data = data;
@ -728,7 +728,7 @@ void do_redo(void)
redidmsg = _("line break");
shoveline = make_new_node(f);
shoveline->data = copy_of(u->strdata);
data = mallocstrncpy(NULL, f->data, u->begin + 1);
data = measured_copy(f->data, u->begin + 1);
data[u->begin] = '\0';
free(f->data);
f->data = data;
@ -1902,7 +1902,7 @@ void justify_paragraph(linestruct **line, size_t par_len)
/* Copy the leading part (quoting + indentation) of the sample line. */
quote_len = quote_length(sampleline->data);
lead_len = quote_len + indent_length(sampleline->data + quote_len);
lead_string = mallocstrncpy(NULL, sampleline->data, lead_len + 1);
lead_string = measured_copy(sampleline->data, lead_len + 1);
lead_string[lead_len] = '\0';
/* Concatenate all lines of the paragraph into a single line. */
@ -1984,7 +1984,7 @@ void do_justify(bool full_justify)
/* Copy the leading part that is to be used for the new paragraph. */
quote_len = quote_length(first_par_line->data);
lead_len = quote_len + indent_length(first_par_line->data + quote_len);
the_lead = mallocstrncpy(NULL, first_par_line->data, lead_len + 1);
the_lead = measured_copy(first_par_line->data, lead_len + 1);
the_lead[lead_len] = '\0';
/* Copy the leading part that is to be used for the new paragraph after

View File

@ -314,9 +314,10 @@ void *nrealloc(void *ptr, size_t howmuch)
/* Allocate and copy the first n characters of the given src string, after
* freeing the destination. Usage: "dest = mallocstrncpy(dest, src, n);". */
char *mallocstrncpy(char *dest, const char *src, size_t n)
char *measured_copy(const char *src, size_t n)
{
dest = charealloc(dest, n);
char *dest = charalloc(n);
strncpy(dest, src, n);
return dest;
@ -326,13 +327,18 @@ char *mallocstrncpy(char *dest, const char *src, size_t n)
* "dest = mallocstrcpy(dest, src);". */
char *mallocstrcpy(char *dest, const char *src)
{
return mallocstrncpy(dest, src, strlen(src) + 1);
size_t count = strlen(src) + 1;
dest = charealloc(dest, count);
strncpy(dest, src, count);
return dest;
}
/* Return an allocated copy of the given string. */
char *copy_of(const char *string)
{
return mallocstrncpy(NULL, string, strlen(string) + 1);
return measured_copy(string, strlen(string) + 1);
}
/* Free the string at dest and return the string at src. */