From nobody Tue Oct 25 21:51:22 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Mxly23g2hz4g5vd; Tue, 25 Oct 2022 21:51:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Mxly23CWXz3g4t; Tue, 25 Oct 2022 21:51:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666734682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Sleka36Xoac34AT46ubPJWq2jbASyB4NIC0tSaUcNk=; b=ZLkqTAqhAaY9qE5xw8lkw986acGD4f2rvdpTXkWV7URlNlMGCyhGPy1EORoHJTCFmFcw1O cum6cPHGL28DTO5b6/Bk+hf3uZzvrxDtEgu6ZksKJ4HjiPqoJcJgmRBgQzoj76YjGuBsgm gGgoesJoijCxM39hcT1255iDEF8/RjqaSKLbqslM4m9nzUcDmLVQ4kKLgLTuHAnTT41KhK H8u+XdO5r1gnO4n1j7vAEjgTEzdAuQE58qskVuIdFTR4GShk08hmZlR+5r5eD1hX9URYWl 8eDMuCPZIYhzBVDBKnaUkjMGyuYj023a2vIxm1HndqNcEeHRMCPu5CQdvKhC2w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Mxly21wg9ztMW; Tue, 25 Oct 2022 21:51:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 29PLpM9a085872; Tue, 25 Oct 2022 21:51:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29PLpMQS085871; Tue, 25 Oct 2022 21:51:22 GMT (envelope-from git) Date: Tue, 25 Oct 2022 21:51:22 GMT Message-Id: <202210252151.29PLpMQS085871@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: f81dfea2912d - main - strfmon: Code cleanup List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f81dfea2912dbc0560587ab534a3d8549dbbb95b Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666734682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Sleka36Xoac34AT46ubPJWq2jbASyB4NIC0tSaUcNk=; b=knqThcgj42xjkOYyNmtWzVheauPlOTDtQQqVTWRjogy+3Am0j0EogAbnjo+A4+rgklbwOO Wp7sTNNcE5yfep0a4YKIccmg53bJuu5akqhJ1Kej1VnBO/tFH8yVXvRqcMFbuS3CnUxXRi InUkRZuounUZjvQ7v6wXRMyDu7XRO4l0M7BY+iN9g3iNAqje+6q2fFAG2ICmsuy7c8q7V9 0Q/ok5RXAGtARusrq3yIDmdk9/a8MHXfriCgD+o8gUTOdl58mnGiZZl5WXJ0QCIgqG4z+d WaQF8XRSH3J++8rFt6L0Rf9YiD9taf3Cm47TGazm1bbZfoUgSV60aLgfwrWLQA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666734682; a=rsa-sha256; cv=none; b=qITi4ubiScjZtIJkiLYpE9QwxmfIYEdddQSyb7G5ZynesvvC+NcqjN5ASOHaMJppc4EzWc X3w2BenYSRc/NdmuTvWCT8PxCUzUIkSIMYiBuUqo23yKSe77hO2WbN4ofThRXaew695w2T IJ8CewyKT+LfGyCB1wK+gC+GoZf4+vIM/jEcKh3rg3/FK3vVZVhwrLO9/27967+YJMS4N4 G4l9jbJSGv1wEdTvL4ywAKtbZpRh9INxWJp4vlmJpdCpiZ52omqsMl9HiWPtA8dVjNBgFy 9xVJdtqnR4uMVOrsqvHJ03vupBTEGzTPLmTo1bzFmkZ9fvZ2vS5CFjYWAQc1fA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=f81dfea2912dbc0560587ab534a3d8549dbbb95b commit f81dfea2912dbc0560587ab534a3d8549dbbb95b Author: Jose Luis Duran AuthorDate: 2022-10-14 17:05:22 +0000 Commit: Konstantin Belousov CommitDate: 2022-10-25 21:40:16 +0000 strfmon: Code cleanup No functional change intended. Not claiming full style(9) compliance. Reviewed by: kib PR: 267282 Github PR: #619 MFC after: 1 week --- lib/libc/stdlib/strfmon.c | 132 ++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/lib/libc/stdlib/strfmon.c b/lib/libc/stdlib/strfmon.c index 5829dd2d801c..4bedf1950c8a 100644 --- a/lib/libc/stdlib/strfmon.c +++ b/lib/libc/stdlib/strfmon.c @@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include #include + #include "xlocale_private.h" /* internal flags */ @@ -55,22 +56,22 @@ __FBSDID("$FreeBSD$"); #define SUPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */ #define LEFT_JUSTIFY 0x20 /* left justify */ #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ -#define IS_NEGATIVE 0x80 /* is argument value negative ? */ +#define IS_NEGATIVE 0x80 /* is argument value negative ? */ /* internal macros */ -#define PRINT(CH) do { \ - if (dst >= s + maxsize) \ +#define PRINT(CH) do { \ + if (dst >= s + maxsize) \ goto e2big_error; \ *dst++ = CH; \ } while (0) -#define PRINTS(STR) do { \ +#define PRINTS(STR) do { \ char *tmps = STR; \ while (*tmps != '\0') \ PRINT(*tmps++); \ } while (0) -#define GET_NUMBER(VAR) do { \ +#define GET_NUMBER(VAR) do { \ VAR = 0; \ while (isdigit((unsigned char)*fmt)) { \ if (VAR > INT_MAX / 10) \ @@ -83,15 +84,15 @@ __FBSDID("$FreeBSD$"); } \ } while (0) -#define GRPCPY(howmany) do { \ +#define GRPCPY(howmany) do { \ int i = howmany; \ while (i-- > 0) { \ avalue_size--; \ - *--bufend = *(avalue+avalue_size+padded); \ + *--bufend = *(avalue + avalue_size + padded); \ } \ } while (0) -#define GRPSEP do { \ +#define GRPSEP do { \ bufend -= thousands_sep_size; \ memcpy(bufend, thousands_sep, thousands_sep_size); \ groups++; \ @@ -103,11 +104,11 @@ static char *__format_grouped_double(double, int *, int, int, int); static ssize_t vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, - const char * __restrict format, va_list ap) + 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 *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 */ @@ -129,7 +130,6 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, int sverrno; FIX_LOCALE(loc); - lc = localeconv_l(loc); dst = s; fmt = format; @@ -145,7 +145,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, /* '%' found ! */ /* "%%" mean just '%' */ - if (*(fmt+1) == '%') { + if (*(fmt + 1) == '%') { fmt++; literal: PRINT(*fmt++); @@ -287,25 +287,25 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, * = 0 - symbol succeeds the value * * p_sep_by_space & n_sep_by_space - * + * * = 0 - no space separates $currency_symbol * from the value for a monetary quantity with a - * non-negative value + * non-negative value * = 1 - space separates the symbol from the value * = 2 - space separates the symbol and the sign string, * if adjacent. - * + * * p_sign_posn & n_sign_posn - * + * * = 0 - parentheses enclose the quantity and the - * $currency_symbol - * = 1 - the sign string precedes the quantity and the * $currency_symbol - * = 2 - the sign string succeeds the quantity and the + * = 1 - the sign string precedes the quantity and the + * $currency_symbol + * = 2 - the sign string succeeds the quantity and the * $currency_symbol * = 3 - the sign string precedes the $currency_symbol * = 4 - the sign string succeeds the $currency_symbol - * + * */ tmpptr = dst; @@ -378,11 +378,11 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, while (dst - tmpptr < width) PRINT(' '); } else { - pad_size = dst-tmpptr; - memmove(tmpptr + width-pad_size, tmpptr, + pad_size = dst - tmpptr; + memmove(tmpptr + width - pad_size, tmpptr, pad_size); - memset(tmpptr, ' ', width-pad_size); - dst += width-pad_size; + memset(tmpptr, ' ', width - pad_size); + dst += width - pad_size; } } } @@ -408,35 +408,11 @@ end_error: errno = sverrno; return (-1); } -ssize_t -strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format, - ...) -{ - size_t ret; - va_list ap; - va_start(ap, format); - ret = vstrfmon_l(s, maxsize, loc, format, ap); - va_end(ap); - return ret; -} - -ssize_t -strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, - ...) -{ - size_t ret; - va_list ap; - va_start(ap, format); - ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap); - va_end(ap); - return ret; -} - static void __setup_vars(int flags, char *cs_precedes, char *sep_by_space, - char *sign_posn, char **signstr) { - + char *sign_posn, char **signstr) +{ struct lconv *lc = localeconv(); if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { @@ -473,8 +449,8 @@ __setup_vars(int flags, char *cs_precedes, char *sep_by_space, } static int -__calc_left_pad(int flags, char *cur_symb) { - +__calc_left_pad(int flags, char *cur_symb) +{ char cs_precedes, sep_by_space, sign_posn, *signstr; int left_chars = 0; @@ -499,8 +475,8 @@ __calc_left_pad(int flags, char *cur_symb) { } static int -get_groups(int size, char *grouping) { - +get_groups(int size, char *grouping) +{ int chars = 0; if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ @@ -524,7 +500,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) { + int left_prec, int right_prec, int pad_char) +{ char *rslt; char *avalue; @@ -561,10 +538,10 @@ __format_grouped_double(double value, int *flags, /* fill right_prec with default value */ if (right_prec == -1) { - if (*flags & USE_INTL_CURRENCY) - right_prec = lc->int_frac_digits; - else - right_prec = lc->frac_digits; + if (*flags & USE_INTL_CURRENCY) + right_prec = lc->int_frac_digits; + else + right_prec = lc->frac_digits; if (right_prec == CHAR_MAX) /* POSIX locale ? */ right_prec = 2; @@ -598,7 +575,7 @@ __format_grouped_double(double value, int *flags, if (right_prec > 0) { bufend -= right_prec; - memcpy(bufend, avalue + avalue_size+padded-right_prec, + memcpy(bufend, avalue + avalue_size + padded - right_prec, right_prec); bufend -= decimal_point_size; memcpy(bufend, decimal_point, decimal_point_size); @@ -630,10 +607,9 @@ __format_grouped_double(double value, int *flags, if (avalue_size != 0) GRPCPY(avalue_size); padded -= groups; - } else { bufend -= avalue_size; - memcpy(bufend, avalue+padded, avalue_size); + memcpy(bufend, avalue + padded, avalue_size); /* decrease assumed $decimal_point */ if (right_prec == 0) padded -= decimal_point_size; @@ -650,3 +626,31 @@ __format_grouped_double(double value, int *flags, free(avalue); return (rslt); } + +ssize_t +strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, + ...) +{ + size_t ret; + va_list ap; + + va_start(ap, format); + ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap); + va_end(ap); + + return (ret); +} + +ssize_t +strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, + const char * __restrict format, ...) +{ + size_t ret; + va_list ap; + + va_start(ap, format); + ret = vstrfmon_l(s, maxsize, loc, format, ap); + va_end(ap); + + return (ret); +}