From nobody Tue Nov 1 00:46:27 2022 X-Original-To: dev-commits-src-branches@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 4N1WYH3BQqz4gxxP; Tue, 1 Nov 2022 00:46:27 +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 4N1WYH2jNpz3QbD; Tue, 1 Nov 2022 00:46:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667263587; 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=5U+FbETY3A8GQt94s+eaKlkTbNQ1Vk3UBT9OWIl1iec=; b=usRzt3dHA8aDii8njjjCmX6BMxi4tOhap3SoHRw/PkuWBaE0r3jyVe74ShEYNbOyRiBGfv GtIlQ7PXX9ZeOkWjdaqKXstRDAf3Fw2iMHT4NUdbS7U6KqsgEmelkaDsgxdCKg68pDiuH2 7YV2ljLaBNDh3XjcKEgraoWLTUGQpBvdSnKfaF3vtHH//kqJQJpo3Hic7z3MknqQJYDBKv 7iVcU2B+XsV7u36l4kucpA4kXGKAolesSZpH6hqaYIbsE+t5JAqD5IhUQ49TiGXJhF4mM/ 0SLon/RmDpia3IP45K6KHpRrmiRlMyIZ/iZf92pmIqqKDttyveeJkSQ3kH27EQ== 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 4N1WYH1ZqTzwmb; Tue, 1 Nov 2022 00:46:27 +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 2A10kRP7041929; Tue, 1 Nov 2022 00:46:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2A10kRs4041928; Tue, 1 Nov 2022 00:46:27 GMT (envelope-from git) Date: Tue, 1 Nov 2022 00:46:27 GMT Message-Id: <202211010046.2A10kRs4041928@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 27744c741dbd - stable/13 - strfmon: Code cleanup List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 27744c741dbd38bc0c7c9bd1daffb532f2b4a1fe Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667263587; 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=5U+FbETY3A8GQt94s+eaKlkTbNQ1Vk3UBT9OWIl1iec=; b=exduCgQS3FLVCeA778+8EhIryhPftlWR+ZQDcVHV43MpDWIfO7A2u/OxmwCE7eqiC2qGIu q9Z37M8CxdCDu+slpZOJO5N2LfKoErsbdRoPzNifH8swbsCF4kK+Wi70uqIPAw4/wGnR9J OVoMECUQqPN/PaPNAQWdQ7zHwlb8Dbg3QXnLLzU8SetetEQefWR0YJr/zPr0O16jeaZpXT rrI7ACECrG9u3gPHsE/jZGXXDJyxyUEQ5NN5JT/rkepZ3VpRRfDCkXY58dMqNuxXaJNakl 6Gxb9BQ5xZfi3Bnn02/SlmDDiOUWnPkEdH3pnS/5Ext4Q81TCYDhfxSdn7/qzQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1667263587; a=rsa-sha256; cv=none; b=V3GjcCo+cw/5cE89X33V/nf5WonpRU9zyzI8HPbBE4kSyhaP0OKHm1PQsWoh+cecBTwYvf zt+wV1SjRpA9GlvTM0R+gsICCWMCKDwsu1a0m4kfhM+Zfa1zIinaVALOoXGD8i3I8scGL9 Ww9IF39nN7I+cPAtUbDKhYLn2jP1aVcUSxOtUj5/Hru4W/GmHL/0gUPUVHinsG5Ybjf9Zs zzhBW8ltbeBBO/v6rwFrJdTaZNJqvGBeGBdRwp/k6BDkRtgzaWb9JoR0EotooMiWxEGMXV vvvHR5INC8FLveORf6igdn3SQEaaZWK//3MVpKX6mqHGlPrOySNWduZo0j/J6A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=27744c741dbd38bc0c7c9bd1daffb532f2b4a1fe commit 27744c741dbd38bc0c7c9bd1daffb532f2b4a1fe Author: Jose Luis Duran AuthorDate: 2022-10-14 17:05:22 +0000 Commit: Konstantin Belousov CommitDate: 2022-11-01 00:44:41 +0000 strfmon: Code cleanup (cherry picked from commit f81dfea2912dbc0560587ab534a3d8549dbbb95b) --- 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 d646ec0b001a..1b1dc28e6b6e 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); +}