FYI I have a patch and I have incorporated some of Alexander's idea.

Difference:

 - Use of both HN_DIVISOR_1000 and HN_IEC_PREFIXES triggers an
assertion.  I think it doesn't make sense to return since this is an
API violation and we should just tell the caller explicitly;
 - DIVISOR_1000 and !1000 cases use just same prefixes, so merge them
while keeping divisor intact;
 - Make prefixes table consistently long.  I have no strong opinion on
this one, though, it's just what my original version used and I can
change it to the way Alexander did if there is an advantage of doing
that way.

(Note, it seems that we use HN_ prefix for both 'scale' and 'flags', I
have sorted them by value but HN_IEC_PREFIXES should really belong to
the flags group).

Cheers,
-- 
Xin LI <delp...@delphij.net> http://www.delphij.net
Index: humanize_number.c
===================================================================
--- humanize_number.c	(revision 220009)
+++ humanize_number.c	(working copy)
@@ -54,29 +54,31 @@
 	assert(buf != NULL);
 	assert(suffix != NULL);
 	assert(scale >= 0);
+	assert(!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES)));
 
 	remainder = 0;
 
-	if (flags & HN_DIVISOR_1000) {
-		/* SI for decimal multiplies */
-		divisor = 1000;
+	if (flags & HN_IEC_PREFIXES) {
+		baselen = 2;
+		divisor = 1024;
 		if (flags & HN_B)
-			prefixes = "B\0k\0M\0G\0T\0P\0E";
+			prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
 		else
-			prefixes = "\0\0k\0M\0G\0T\0P\0E";
+			prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
 	} else {
-		/*
-		 * binary multiplies
-		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
-		 */
-		divisor = 1024;
+		baselen = 1;
+		if (flags & HN_DIVISOR_1000)
+			divisor = 1000;
+		else
+			divisor = 1024;
+
 		if (flags & HN_B)
-			prefixes = "B\0K\0M\0G\0T\0P\0E";
+			prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
 		else
-			prefixes = "\0\0K\0M\0G\0T\0P\0E";
+			prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
 	}
 
-#define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
+#define	SCALE2PREFIX(scale)	(&prefixes[(scale) * 3])
 	maxscale = 7;
 
 	if (scale >= maxscale &&
@@ -91,10 +93,10 @@
 	if (quotient < 0) {
 		sign = -1;
 		quotient = -quotient;
-		baselen = 3;		/* sign, digit, prefix */
+		baselen += 2;		/* sign, digit */
 	} else {
 		sign = 1;
-		baselen = 2;		/* digit, prefix */
+		baselen += 1;		/* digit */
 	}
 	if (flags & HN_NOSPACE)
 		sep = "";
Index: libutil.h
===================================================================
--- libutil.h	(revision 220009)
+++ libutil.h	(working copy)
@@ -223,6 +223,7 @@
 
 #define HN_GETSCALE		0x10
 #define HN_AUTOSCALE		0x20
+#define HN_IEC_PREFIXES		0x40
 
 /* hexdump(3) */
 #define	HD_COLUMN_MASK		0xff
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"

Reply via email to