From: markus schnalke Date: Tue, 6 Sep 2016 18:10:28 +0000 (+0200) Subject: Factor trim format function out X-Git-Tag: mmh-0.4~65 X-Git-Url: http://git.marmaro.de/?p=mmh;a=commitdiff_plain;h=18591f8e001ecedbee48a51c1d1f08ebaa1c15c8;hp=fc9279e818dfc96c63a5d75a89080cc68cfe1170 Factor trim format function out Also clarify that it trims both, leading and trailing whitespace. (Note that it is a special trim function. We cannot simply replace it with sbr/trim.c. Nonetheless, its readability is somehow bad.) --- diff --git a/h/fmt_compile.h b/h/fmt_compile.h index f034c6c..21e21e2 100644 --- a/h/fmt_compile.h +++ b/h/fmt_compile.h @@ -22,7 +22,7 @@ #define FT_LS_CFIND 15 /* set "str" to context_find(text) */ #define FT_LS_DECODECOMP 16 /* set "str" to decoded component text */ #define FT_LS_DECODE 17 /* decode "str" as RFC-2047 header */ -#define FT_LS_TRIM 18 /* trim trailing white space from "str" */ +#define FT_LS_TRIM 18 /* trim white space from "str" */ #define FT_LV_COMP 19 /* set "value" to comp (as dec. num) */ #define FT_LV_COMPFLAG 20 /* set "value" to comp flag word */ #define FT_LV_LIT 21 /* set "value" to literal num */ diff --git a/man/mh-format.man5 b/man/mh-format.man5 index 703e633..3935872 100644 --- a/man/mh-format.man5 +++ b/man/mh-format.man5 @@ -292,7 +292,7 @@ decode expr string decode \fIstr\fR as RFC-2047 (MIME-encoded) component and print it unquote expr string remove RFC-2822 quotes from \fIstr\fR unmailto expr string remove `mailto:' and < > from \fIstr\fR -trim expr trim trailing white-space from \fIstr\fR +trim expr trim white-space from \fIstr\fR putstr expr print \fIstr\fR putstrf expr print \fIstr\fR in a fixed width putnum expr print \fInum\fR diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 9ad7b8a..37bf16a 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -36,6 +36,7 @@ struct mailname fmt_mnull; static int match(char *, char *); static char *get_x400_friendly(char *, char *, int); static int get_x400_comp(char *, char *, char *, int); +static char *fmt_trim(char *, int); /* @@ -259,6 +260,39 @@ get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len) return 1; } +static char * +fmt_trim(char *str, int width) +{ + char *xp; + int ljust, i; + char buffer[BUFSIZ]; + + strncpy(buffer, str, sizeof(buffer)); + buffer[sizeof(buffer)-1] = '\0'; + str = buffer; + while (isspace(*str)) { + str++; + } + ljust = 0; + if ((i = width) < 0) { + i = -i; + ljust++; + } + + if (!ljust && i > 0 && (int)strlen(str) > i) { + str[i] = '\0'; + } + xp = str; + xp += strlen(str) - 1; + while (xp > str && isspace(*xp)) { + *xp-- = '\0'; + } + if (ljust && i > 0 && (int)strlen(str) > i) { + str += strlen(str) - i; + } + return str; +} + struct format * fmt_scan(struct format *format, char *scanl, int width, int *dat) { @@ -462,27 +496,7 @@ fmt_scan(struct format *format, char *scanl, int width, int *dat) case FT_LS_TRIM: if (str) { - unsigned char *xp; - - strncpy(buffer, str, sizeof(buffer)); - buffer[sizeof(buffer)-1] = '\0'; - str = buffer; - while (isspace(*str)) - str++; - ljust = 0; - if ((i = fmt->f_width) < 0) { - i = -i; - ljust++; - } - - if (!ljust && i > 0 && (int)strlen(str) > i) - str[i] = '\0'; - xp = str; - xp += strlen(str) - 1; - while (xp > str && isspace(*xp)) - *xp-- = '\0'; - if (ljust && i > 0 && (int)strlen(str) > i) - str += strlen(str) - i; + str = fmt_trim(str, fmt->f_width); } break;