Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Dec 2023 04:19:23 GMT
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: fff086cfeafc - stable/13 - strfmon: style fixes
Message-ID:  <202312160419.3BG4JNX7059459@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=fff086cfeafc110f94a40eb340ddc8b13d8df0cd

commit fff086cfeafc110f94a40eb340ddc8b13d8df0cd
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2023-11-30 23:28:21 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-12-16 04:18:48 +0000

    strfmon: style fixes
    
    (cherry picked from commit 56a0d5444d6f39302f3476b61c1b81ed39abe589)
---
 lib/libc/stdlib/strfmon.c | 197 +++++++++++++++++++++++-----------------------
 1 file changed, 99 insertions(+), 98 deletions(-)

diff --git a/lib/libc/stdlib/strfmon.c b/lib/libc/stdlib/strfmon.c
index d770b6f32bd1..1c9eb3f45d74 100644
--- a/lib/libc/stdlib/strfmon.c
+++ b/lib/libc/stdlib/strfmon.c
@@ -34,6 +34,7 @@
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
+
 #include <ctype.h>
 #include <errno.h>
 #include <limits.h>
@@ -58,7 +59,7 @@
 
 /* internal macros */
 #define	PRINT(CH) do {						\
-	if (dst >= s + maxsize)				\
+	if (dst >= s + maxsize)					\
 		goto e2big_error;				\
 	*dst++ = CH;						\
 } while (0)
@@ -69,9 +70,9 @@
 		PRINT(*tmps++);					\
 } while (0)
 
-#define	GET_NUMBER(VAR, LOC) do {					\
+#define	GET_NUMBER(VAR, LOC) do {				\
 	VAR = 0;						\
-	while (isdigit_l((unsigned char)*fmt, LOC)) {			\
+	while (isdigit_l((unsigned char)*fmt, LOC)) {		\
 		if (VAR > INT_MAX / 10)				\
 			goto e2big_error;			\
 		VAR *= 10;					\
@@ -102,22 +103,22 @@ static char *__format_grouped_double(double, int *, int, int, int,
     struct lconv *, locale_t);
 
 static ssize_t
-vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
-    const char * __restrict format, va_list ap)
+vstrfmon_l(char *__restrict s, size_t maxsize, locale_t loc,
+    const char *__restrict format, va_list ap)
 {
 	char		*dst;		/* output destination pointer */
 	const char	*fmt;		/* current format poistion pointer */
 	struct lconv	*lc;		/* pointer to lconv structure */
 	char		*asciivalue;	/* formatted double pointer */
 
-	int		flags;		/* formatting options */
-	int		pad_char;	/* padding character */
-	int		pad_size;	/* pad size */
-	int		width;		/* field width */
-	int		left_prec;	/* left precision */
-	int		right_prec;	/* right precision */
-	double		value;		/* just value */
-	char		space_char = ' '; /* space after currency */
+	int		flags;			/* formatting options */
+	int		pad_char;		/* padding character */
+	int		pad_size;		/* pad size */
+	int		width;			/* field width */
+	int		left_prec;		/* left precision */
+	int		right_prec;		/* right precision */
+	double		value;			/* just value */
+	char		space_char = ' ';	/* space after currency */
 
 	char		cs_precedes,	/* values gathered from struct lconv */
 			sep_by_space,
@@ -127,6 +128,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 
 	char		*tmpptr;	/* temporary vars */
 	int		sverrno;
+
 	FIX_LOCALE(loc);
 
 	lc = localeconv_l(loc);
@@ -135,7 +137,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 	asciivalue = NULL;
 	currency_symbol = NULL;
 
-	while (*fmt) {
+	while (*fmt != 0) {
 		/* pass nonformating characters AS IS */
 		if (*fmt != '%')
 			goto literal;
@@ -145,13 +147,13 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 		/* "%%" mean just '%' */
 		if (*(fmt + 1) == '%') {
 			fmt++;
-	literal:
+literal:
 			PRINT(*fmt++);
 			continue;
 		}
 
 		/* set up initial values */
-		flags = (NEED_GROUPING|LOCALE_POSN);
+		flags = NEED_GROUPING | LOCALE_POSN;
 		pad_char = ' ';		/* padding character is "space" */
 		pad_size = 0;		/* no padding initially */
 		left_prec = -1;		/* no left precision specified */
@@ -161,32 +163,32 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 		/* Flags */
 		while (1) {
 			switch (*++fmt) {
-				case '=':	/* fill character */
-					pad_char = *++fmt;
-					if (pad_char == '\0')
-						goto format_error;
-					continue;
-				case '^':	/* not group currency  */
-					flags &= ~(NEED_GROUPING);
-					continue;
-				case '+':	/* use locale defined signs */
-					if (flags & SIGN_POSN_USED)
-						goto format_error;
-					flags |= (SIGN_POSN_USED|LOCALE_POSN);
-					continue;
-				case '(':	/* enclose negatives with () */
-					if (flags & SIGN_POSN_USED)
-						goto format_error;
-					flags |= (SIGN_POSN_USED|PARENTH_POSN);
-					continue;
-				case '!':	/* suppress currency symbol */
-					flags |= SUPPRESS_CURR_SYMBOL;
-					continue;
-				case '-':	/* alignment (left)  */
-					flags |= LEFT_JUSTIFY;
-					continue;
-				default:
-					break;
+			case '=':	/* fill character */
+				pad_char = *++fmt;
+				if (pad_char == '\0')
+					goto format_error;
+				continue;
+			case '^':	/* not group currency */
+				flags &= ~(NEED_GROUPING);
+				continue;
+			case '+':	/* use locale defined signs */
+				if (flags & SIGN_POSN_USED)
+					goto format_error;
+				flags |= (SIGN_POSN_USED | LOCALE_POSN);
+				continue;
+			case '(':	/* enclose negatives with () */
+				if (flags & SIGN_POSN_USED)
+					goto format_error;
+				flags |= (SIGN_POSN_USED | PARENTH_POSN);
+				continue;
+			case '!':	/* suppress currency symbol */
+				flags |= SUPPRESS_CURR_SYMBOL;
+				continue;
+			case '-':	/* alignment (left) */
+				flags |= LEFT_JUSTIFY;
+				continue;
+			default:
+				break;
 			}
 			break;
 		}
@@ -194,7 +196,8 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 		/* field Width */
 		if (isdigit_l((unsigned char)*fmt, loc)) {
 			GET_NUMBER(width, loc);
-			/* Do we have enough space to put number with
+			/*
+			 * Do we have enough space to put number with
 			 * required width ?
 			 */
 			if ((unsigned int)width >= maxsize - (dst - s))
@@ -215,22 +218,24 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 			if (!isdigit_l((unsigned char)*++fmt, loc))
 				goto format_error;
 			GET_NUMBER(right_prec, loc);
-			if ((unsigned int)right_prec >= maxsize - (dst - s) -
-			    left_prec)
+			if ((unsigned int)right_prec >=
+			    maxsize - (dst - s) - left_prec)
 				goto e2big_error;
 		}
 
 		/* Conversion Characters */
 		switch (*fmt++) {
-			case 'i':	/* use international currency format */
-				flags |= USE_INTL_CURRENCY;
-				break;
-			case 'n':	/* use national currency format */
-				flags &= ~(USE_INTL_CURRENCY);
-				break;
-			default:	/* required character is missing or
-					   premature EOS */
-				goto format_error;
+		case 'i':	/* use international currency format */
+			flags |= USE_INTL_CURRENCY;
+			break;
+		case 'n':	/* use national currency format */
+			flags &= ~(USE_INTL_CURRENCY);
+			break;
+		default:	/*
+				 * required character is missing or
+				 * premature EOS
+				 */
+			goto format_error;
 		}
 
 		if (currency_symbol != NULL)
@@ -246,7 +251,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 			currency_symbol = strdup(lc->currency_symbol);
 
 		if (currency_symbol == NULL)
-			goto end_error;			/* ENOMEM. */
+			goto end_error;	/* ENOMEM. */
 
 		/* value itself */
 		value = va_arg(ap, double);
@@ -268,15 +273,17 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 
 		if (asciivalue != NULL)
 			free(asciivalue);
-		asciivalue = __format_grouped_double(value, &flags,
-		    left_prec, right_prec, pad_char, lc, loc);
+		asciivalue = __format_grouped_double(value, &flags, left_prec,
+		    right_prec, pad_char, lc, loc);
 		if (asciivalue == NULL)
-			goto end_error;		/* errno already set     */
-						/* to ENOMEM by malloc() */
+			goto end_error;	/*
+					 * errno already set to ENOMEM by
+					 * malloc()
+					 */
 
 		/* set some variables for later use */
-		__setup_vars(flags, &cs_precedes, &sep_by_space,
-		    &sign_posn, &signstr, lc);
+		__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
+		    &signstr, lc);
 
 		/*
 		 * Description of some LC_MONETARY's values:
@@ -307,7 +314,6 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 		 *       $currency_symbol
 		 * = 3 - the sign string precedes the $currency_symbol
 		 * = 4 - the sign string succeeds the $currency_symbol
-		 *
 		 */
 
 		tmpptr = dst;
@@ -353,12 +359,10 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 			}
 
 			if (!(flags & SUPPRESS_CURR_SYMBOL)) {
-				if ((sign_posn == 3 && sep_by_space == 2)
-				    || (sep_by_space == 1
-				    && (sign_posn == 0
-				    || sign_posn == 1
-				    || sign_posn == 2
-				    || sign_posn == 4)))
+				if ((sign_posn == 3 && sep_by_space == 2) ||
+				    (sep_by_space == 1 &&
+				     (sign_posn == 0 || sign_posn == 1 ||
+				      sign_posn == 2 || sign_posn == 4)))
 					PRINT(space_char);
 				PRINTS(currency_symbol);
 				if (sign_posn == 4) {
@@ -399,7 +403,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
 	PRINT('\0');
 	free(asciivalue);
 	free(currency_symbol);
-	return (dst - s - 1);	/* return size of put data except trailing '\0' */
+	return (dst - s - 1);	/* size of put data except trailing '\0' */
 
 e2big_error:
 	errno = E2BIG;
@@ -419,15 +423,15 @@ end_error:
 }
 
 static void
-__setup_vars(int flags, char *cs_precedes, char *sep_by_space,
-    char *sign_posn, char **signstr, struct lconv *lc)
+__setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn,
+    char **signstr, struct lconv *lc)
 {
 	if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
 		*cs_precedes = lc->int_n_cs_precedes;
 		*sep_by_space = lc->int_n_sep_by_space;
 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
-		*signstr = (lc->negative_sign[0] == '\0') ? "-"
-		    : lc->negative_sign;
+		*signstr = (lc->negative_sign[0] == '\0') ? "-" :
+		    lc->negative_sign;
 	} else if (flags & USE_INTL_CURRENCY) {
 		*cs_precedes = lc->int_p_cs_precedes;
 		*sep_by_space = lc->int_p_sep_by_space;
@@ -437,8 +441,8 @@ __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
 		*cs_precedes = lc->n_cs_precedes;
 		*sep_by_space = lc->n_sep_by_space;
 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
-		*signstr = (lc->negative_sign[0] == '\0') ? "-"
-		    : lc->negative_sign;
+		*signstr = (lc->negative_sign[0] == '\0') ? "-" :
+		    lc->negative_sign;
 	} else {
 		*cs_precedes = lc->p_cs_precedes;
 		*sep_by_space = lc->p_sep_by_space;
@@ -461,8 +465,8 @@ __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
 	char cs_precedes, sep_by_space, sign_posn, *signstr;
 	int left_chars = 0;
 
-	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
-	    &signstr, lc);
+	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr,
+	    lc);
 
 	if (cs_precedes != 0) {
 		left_chars += strlen(cur_symb);
@@ -471,17 +475,17 @@ __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
 	}
 
 	switch (sign_posn) {
-		case 0:
-			if (flags & IS_NEGATIVE)
-				left_chars++;
-			break;
-		case 1:
+	case 0:
+		if (flags & IS_NEGATIVE)
+			left_chars++;
+		break;
+	case 1:
+		left_chars += strlen(signstr);
+		break;
+	case 3:
+	case 4:
+		if (cs_precedes != 0)
 			left_chars += strlen(signstr);
-			break;
-		case 3:
-		case 4:
-			if (cs_precedes != 0)
-				left_chars += strlen(signstr);
 	}
 	return (left_chars);
 }
@@ -511,8 +515,8 @@ get_groups(int size, char *grouping)
 
 /* convert double to locale-encoded string */
 static char *
-__format_grouped_double(double value, int *flags,
-    int left_prec, int right_prec, int pad_char, struct lconv *lc, locale_t loc)
+__format_grouped_double(double value, int *flags, int left_prec, int right_prec,
+    int pad_char, struct lconv *lc, locale_t loc)
 {
 
 	char		*rslt;
@@ -530,7 +534,7 @@ __format_grouped_double(double value, int *flags,
 	size_t		decimal_point_size;
 	size_t		thousands_sep_size;
 
-	int groups = 0;
+	int		groups = 0;
 
 	grouping = lc->mon_grouping;
 	decimal_point = lc->mon_decimal_point;
@@ -593,10 +597,8 @@ __format_grouped_double(double value, int *flags,
 		avalue_size -= (right_prec + 1);
 	}
 
-	if ((*flags & NEED_GROUPING) &&
-	    thousands_sep_size > 0 &&
-	    *grouping != CHAR_MAX &&
-	    *grouping > 0) {
+	if ((*flags & NEED_GROUPING) && thousands_sep_size > 0 &&
+	    *grouping != CHAR_MAX && *grouping > 0) {
 		while (avalue_size > (int)*grouping) {
 			GRPCPY(*grouping);
 			GRPSEP;
@@ -639,8 +641,7 @@ __format_grouped_double(double value, int *flags,
 }
 
 ssize_t
-strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
-    ...)
+strfmon(char *__restrict s, size_t maxsize, const char *__restrict format, ...)
 {
 	ssize_t ret;
 	va_list ap;
@@ -653,8 +654,8 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
 }
 
 ssize_t
-strfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
-    const char * __restrict format, ...)
+strfmon_l(char *__restrict s, size_t maxsize, locale_t loc,
+    const char *__restrict format, ...)
 {
 	ssize_t ret;
 	va_list ap;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202312160419.3BG4JNX7059459>