changeset 14:669266246c6b

rcg.c: simplify string concatenation
author paulo@thepaulopc
date Fri, 12 Nov 2010 00:08:34 -0800
parents 2fb87616ab65
children acf12a2e872d
files rcg.c
diffstat 1 files changed, 7 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/rcg.c	Sat Nov 06 02:57:06 2010 -0700
     1.2 +++ b/rcg.c	Fri Nov 12 00:08:34 2010 -0800
     1.3 @@ -171,11 +171,6 @@
     1.4  			exit(args_error());
     1.5  	}
     1.6  
     1.7 -	size_t CLR_START_len = strlen(CLR_START);
     1.8 -	size_t clr_len = strlen(clr);
     1.9 -	size_t CLR_END_len = strlen(CLR_END);
    1.10 -	size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
    1.11 -
    1.12  	char *out = NULL;
    1.13  
    1.14  	while (fgets(buf, BUFSIZE, stdin))
    1.15 @@ -216,25 +211,17 @@
    1.16  
    1.17  			if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
    1.18  			{
    1.19 -				out_len += CLR_START_len + clr_len + CLR_END_len + CLR_CLEAR_len;
    1.20 +				size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR);
    1.21 +
    1.22 +				out_len += CLR_STRING_len;
    1.23  				out = realloc(out, out_len*sizeof(char));
    1.24  				if (!out)
    1.25  					exit(realloc_error());
    1.26 -				strncpy(&out[out_pos], &buf[buf_pos], so);
    1.27 -				out_pos += so;
    1.28 -				buf_pos += so;
    1.29 -				strncpy(&out[out_pos], CLR_START, CLR_START_len);
    1.30 -				out_pos += CLR_START_len;
    1.31 -				strncpy(&out[out_pos], clr, clr_len);
    1.32 -				out_pos += clr_len;
    1.33 -				strncpy(&out[out_pos], CLR_END, CLR_END_len);
    1.34 -				out_pos += CLR_END_len;
    1.35 -				strncpy(&out[out_pos], &buf[buf_pos], match_len);
    1.36 -				out_pos += match_len;
    1.37 -				strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
    1.38 -				out_pos += CLR_CLEAR_len;
    1.39  
    1.40 -				buf_pos += match_len;
    1.41 +				sprintf(&out[out_pos], "%.*s%s%s%s%.*s%s", so, &buf[buf_pos], CLR_START, clr, CLR_END, match_len, &buf[buf_pos + so], CLR_CLEAR);
    1.42 +
    1.43 +				buf_pos += match_len + so;
    1.44 +				out_pos += CLR_STRING_len + match_len + so;
    1.45  			}
    1.46  			else
    1.47  			{