Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/gen goodbye short-live commaize_number(3), if you l...



details:   https://anonhg.NetBSD.org/src/rev/3d4431e547a3
branches:  trunk
changeset: 763270:3d4431e547a3
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Mar 16 00:50:28 2011 +0000

description:
goodbye short-live commaize_number(3), if you liked it:
#define commaize_number(s, l, n) snprintf(s, l, "%'llu", (unsigned long long)n)

diffstat:

 lib/libc/gen/Makefile.inc      |    4 +-
 lib/libc/gen/commaize_number.3 |   89 ------------------------------------
 lib/libc/gen/commaize_number.c |  101 -----------------------------------------
 3 files changed, 2 insertions(+), 192 deletions(-)

diffs (215 lines):

diff -r 8172fce9eb85 -r 3d4431e547a3 lib/libc/gen/Makefile.inc
--- a/lib/libc/gen/Makefile.inc Wed Mar 16 00:48:34 2011 +0000
+++ b/lib/libc/gen/Makefile.inc Wed Mar 16 00:50:28 2011 +0000
@@ -1,11 +1,11 @@
-#      $NetBSD: Makefile.inc,v 1.175 2011/03/15 03:47:04 erh Exp $
+#      $NetBSD: Makefile.inc,v 1.176 2011/03/16 00:50:28 christos Exp $
 #      from: @(#)Makefile.inc  8.6 (Berkeley) 5/4/95
 
 # gen sources
 .PATH: ${ARCHDIR}/gen ${.CURDIR}/gen
 
 SRCS+=         _errno.c alarm.c alphasort.c arc4random.c assert.c basename.c clock.c \
-       closedir.c closefrom.c commaize_number.c \
+       closedir.c closefrom.c \
        confstr.c ctermid.c ctype_.c daemon.c \
        dehumanize_number.c devname.c dirname.c disklabel.c err.c errx.c \
        errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \
diff -r 8172fce9eb85 -r 3d4431e547a3 lib/libc/gen/commaize_number.3
--- a/lib/libc/gen/commaize_number.3    Wed Mar 16 00:48:34 2011 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,89 +0,0 @@
-.\"    $NetBSD: commaize_number.3,v 1.2 2011/03/15 08:33:35 wiz Exp $
-.\"
-.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
-.\" All rights reserved.
-.\"
-.\" This code is derived from software contributed to The NetBSD Foundation
-.\" by Eric Haszlakiewicz.
-.\"
-.\" Redistribution and use in source and binary forms, with or without
-.\" modification, are permitted provided that the following conditions
-.\" are met:
-.\" 1. Redistributions of source code must retain the above copyright
-.\"    notice, this list of conditions and the following disclaimer.
-.\" 2. Redistributions in binary form must reproduce the above copyright
-.\"    notice, this list of conditions and the following disclaimer in the
-.\"    documentation and/or other materials provided with the distribution.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
-.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
-.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-.\" POSSIBILITY OF SUCH DAMAGE.
-.\"
-.Dd March 12, 2011
-.Dt COMMAIZE_NUMBER 3
-.Os
-.Sh NAME
-.Nm commaize_number
-.Nd format a number into a human readable form by grouping the digits
-.Sh SYNOPSIS
-.In stdlib.h
-.Ft int
-.Fn commaize_number "char *buffer" "size_t len" "int64_t number"
-.Sh DESCRIPTION
-The
-.Fn commaize_number
-function formats the signed 64-bit quantity given in
-.Fa number
-into
-.Fa buffer ,
-which must be at least
-.Fa len
-bytes long.
-.Pp
-If the formatted number would be too long to fit into
-.Fa buffer ,
-then an error is returned.
-.Pp
-The function name is slightly mistaken,
-since actually, the locale specific thousands separator is used, which
-might not be comma.
-.Sh RETURN VALUES
-.Fn commaize_number
-returns the number of characters stored in
-.Fa buffer
-(excluding the terminating
-.Dv NUL )
-upon success, or \-1 upon failure with
-.Va errno
-set to indicate the error.
-.Sh ERRORS
-.Fn commaize_number
-will fail and nothing will be written to
-.Fa buffer
-if:
-.Bl -tag -width Er
-.It Bq Er ENOMEM
-The formatted number, including commas and terminating
-.Dv NUL ,
-is too long to fit in
-.Fa len
-bytes.
-.El
-.Sh SEE ALSO
-.Xr ls 1 ,
-.Xr setlocale 3 ,
-.Xr humanize_number 9
-.Sh HISTORY
-.Fn commaize_number
-first appeared in
-.Nx 6.0 .
-.Sh AUTHORS
-.An Eric Haszlakiewicz Aq erh%NetBSD.org@localhost
diff -r 8172fce9eb85 -r 3d4431e547a3 lib/libc/gen/commaize_number.c
--- a/lib/libc/gen/commaize_number.c    Wed Mar 16 00:48:34 2011 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,101 +0,0 @@
-/*     $NetBSD: commaize_number.c,v 1.1 2011/03/15 03:47:04 erh Exp $ */
-
-/*-
- * Copyright (c) 2011 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Eric Haszlakiewicz.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-#include <errno.h>
-#include <inttypes.h>
-#include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-/**
- * Format a number with commas (or another locale specific separator) between
- * every three digits.
- *
- * Uses localeconv to figure out which separator to use, but defaults
- * to a comma if the locale specific thousands separator isn't set.
- */
-int commaize_number(char *buf, size_t len, int64_t number)
-{
-    char commabuf[22];  /* 64 bits == 20 digits, +1 for sign, +1 for NUL */
-       const char *thousands_sep = ",";
-       unsigned int separator_len = 1;
-       struct lconv *localeinfo;
-
-       int nchars, ndigits;
-       int chars_needed;
-
-       int dest_offset;
-       int ii;
-
-       if ((nchars = snprintf(commabuf, sizeof(commabuf), "%" PRId64, number)) 
-           >= (int)sizeof(commabuf))
-       {
-               errno = ENOMEM;
-               return -1;
-       }
-
-       localeinfo = localeconv();
-       if (localeinfo && localeinfo->thousands_sep &&
-           localeinfo->thousands_sep[0])
-       {
-               thousands_sep = localeinfo->thousands_sep;
-               separator_len = strlen(localeinfo->thousands_sep);
-       }
-
-       ndigits = nchars;
-       if (commabuf[0] == '-')
-               ndigits--;
-       chars_needed = nchars + separator_len * ((ndigits - 1) / 3);
-       if (chars_needed + 1 > (int)len)
-       {
-               errno = ENOMEM;
-               return -1;
-       }
-
-       buf[chars_needed] = '\0';
-
-       dest_offset = chars_needed - 1;
-       for (ii = 1 ; ii <= nchars ; ii++)
-       {
-               buf[dest_offset] = commabuf[nchars - ii];
-
-               if ((ii % 3) == 0 && ii < (int)nchars && commabuf[nchars - ii - 1] != '-')
-               {
-                       memcpy(&buf[dest_offset - separator_len], thousands_sep, separator_len);
-                       dest_offset -= separator_len;
-               }
-
-               dest_offset--;
-       }
-       return chars_needed;
-}
-



Home | Main Index | Thread Index | Old Index