[glib/wip/format_size_value_units] gutils: Add flags to g_format_size_full() to return only value or only units



commit 0fade4a9d1a86a0a1f1c32d2e81536f197d300c6
Author: Milan Crha <mcrha redhat com>
Date:   Tue Mar 8 18:04:07 2022 +0100

    gutils: Add flags to g_format_size_full() to return only value or only units
    
    This allows to split the formatted size into the value and into the units,
    when can be shown differently in the UI.
    
    Relate to https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1391

 glib/gutils.c          | 90 +++++++++++++++++++++++++++++++++-----------------
 glib/gutils.h          |  4 ++-
 glib/tests/fileutils.c | 24 ++++++++++++++
 3 files changed, 87 insertions(+), 31 deletions(-)
---
diff --git a/glib/gutils.c b/glib/gutils.c
index 457ae0955..11248686f 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -2803,6 +2803,12 @@ g_format_size (guint64 size)
  *     Network and storage sizes should be reported in the normal SI units.
  * @G_FORMAT_SIZE_BITS: set the size as a quantity in bits, rather than
  *     bytes, and return units in bits. For example, ‘Mb’ rather than ‘MB’.
+ * @G_FORMAT_SIZE_ONLY_VALUE: return only value, without unit; this should
+ *     not be used together with @G_FORMAT_SIZE_LONG_FORMAT
+ *     nor @G_FORMAT_SIZE_ONLY_UNIT. Since: 2.72
+ * @G_FORMAT_SIZE_ONLY_UNIT: return only unit, without value; this should
+ *     not be used together with @G_FORMAT_SIZE_LONG_FORMAT
+ *     nor @G_FORMAT_SIZE_ONLY_VALUE. Since: 2.72
  *
  * Flags to modify the format of the string returned by g_format_size_full().
  */
@@ -2846,59 +2852,59 @@ g_format_size_full (guint64          size,
   const struct Format formats[4][6] = {
     {
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { KILOBYTE_FACTOR, N_("%.1f kB") },
+      { KILOBYTE_FACTOR, N_("kB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { MEGABYTE_FACTOR, N_("%.1f MB") },
+      { MEGABYTE_FACTOR, N_("MB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { GIGABYTE_FACTOR, N_("%.1f GB") },
+      { GIGABYTE_FACTOR, N_("GB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { TERABYTE_FACTOR, N_("%.1f TB") },
+      { TERABYTE_FACTOR, N_("TB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { PETABYTE_FACTOR, N_("%.1f PB") },
+      { PETABYTE_FACTOR, N_("PB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { EXABYTE_FACTOR,  N_("%.1f EB") }
+      { EXABYTE_FACTOR,  N_("EB") }
     },
     {
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { KIBIBYTE_FACTOR, N_("%.1f KiB") },
+      { KIBIBYTE_FACTOR, N_("KiB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { MEBIBYTE_FACTOR, N_("%.1f MiB") },
+      { MEBIBYTE_FACTOR, N_("MiB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { GIBIBYTE_FACTOR, N_("%.1f GiB") },
+      { GIBIBYTE_FACTOR, N_("GiB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { TEBIBYTE_FACTOR, N_("%.1f TiB") },
+      { TEBIBYTE_FACTOR, N_("TiB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { PEBIBYTE_FACTOR, N_("%.1f PiB") },
+      { PEBIBYTE_FACTOR, N_("PiB") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { EXBIBYTE_FACTOR, N_("%.1f EiB") }
+      { EXBIBYTE_FACTOR, N_("EiB") }
     },
     {
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { KILOBYTE_FACTOR, N_("%.1f kb") },
+      { KILOBYTE_FACTOR, N_("kb") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { MEGABYTE_FACTOR, N_("%.1f Mb") },
+      { MEGABYTE_FACTOR, N_("Mb") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { GIGABYTE_FACTOR, N_("%.1f Gb") },
+      { GIGABYTE_FACTOR, N_("Gb") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { TERABYTE_FACTOR, N_("%.1f Tb") },
+      { TERABYTE_FACTOR, N_("Tb") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { PETABYTE_FACTOR, N_("%.1f Pb") },
+      { PETABYTE_FACTOR, N_("Pb") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { EXABYTE_FACTOR,  N_("%.1f Eb") }
+      { EXABYTE_FACTOR,  N_("Eb") }
     },
     {
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { KIBIBYTE_FACTOR, N_("%.1f Kib") },
+      { KIBIBYTE_FACTOR, N_("Kib") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { MEBIBYTE_FACTOR, N_("%.1f Mib") },
+      { MEBIBYTE_FACTOR, N_("Mib") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { GIBIBYTE_FACTOR, N_("%.1f Gib") },
+      { GIBIBYTE_FACTOR, N_("Gib") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { TEBIBYTE_FACTOR, N_("%.1f Tib") },
+      { TEBIBYTE_FACTOR, N_("Tib") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { PEBIBYTE_FACTOR, N_("%.1f Pib") },
+      { PEBIBYTE_FACTOR, N_("Pib") },
       /* Translators: Keep the no-break space between %.1f and the unit symbol */
-      { EXBIBYTE_FACTOR, N_("%.1f Eib") }
+      { EXBIBYTE_FACTOR, N_("Eib") }
     }
   };
 
@@ -2907,7 +2913,7 @@ g_format_size_full (guint64          size,
 
   string = g_string_new (NULL);
 
-  switch (flags & ~G_FORMAT_SIZE_LONG_FORMAT)
+  switch (flags & ~(G_FORMAT_SIZE_LONG_FORMAT | G_FORMAT_SIZE_ONLY_VALUE | G_FORMAT_SIZE_ONLY_UNIT))
     {
     case G_FORMAT_SIZE_DEFAULT:
       index = FORMAT_BYTES;
@@ -2928,24 +2934,35 @@ g_format_size_full (guint64          size,
 
   if (size < formats[index][0].factor)
     {
-      const char * format;
+      const char * units;
 
       if (index == FORMAT_BYTES || index == FORMAT_BYTES_IEC)
         {
-          format = g_dngettext (GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size);
+          units = g_dngettext (GETTEXT_PACKAGE, "byte", "bytes", (guint) size);
         }
       else
         {
-          format = g_dngettext (GETTEXT_PACKAGE, "%u bit", "%u bits", (guint) size);
+          units = g_dngettext (GETTEXT_PACKAGE, "bit", "bits", (guint) size);
         }
 
-      g_string_printf (string, format, (guint) size);
+      if ((flags & G_FORMAT_SIZE_ONLY_UNIT) != 0)
+        g_string_append (string, units);
+      else if ((flags & G_FORMAT_SIZE_ONLY_VALUE) != 0)
+        g_string_printf (string, "%u", (guint) size);
+      else
+        {
+         /* Translators: The first "%u" is replaced with the value, the "%s" with a unit of the value.
+           * The order can be changed with "%$2s %$1u". */
+          g_string_printf (string, C_ ("format-size", "%u %s"), (guint) size, units);
+        }
 
       flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
     }
   else
     {
       const gsize n = G_N_ELEMENTS (formats[index]);
+      const gchar * units;
+      gdouble value;
       gsize i;
 
       /*
@@ -2964,7 +2981,20 @@ g_format_size_full (guint64          size,
             }
         }
 
-      g_string_printf (string, _(f->string), (gdouble) size / (gdouble) f->factor);
+      units = _(f->string);
+      value = (gdouble) size / (gdouble) f->factor;
+
+      if ((flags & G_FORMAT_SIZE_ONLY_UNIT) != 0)
+        g_string_append (string, units);
+      else if ((flags & G_FORMAT_SIZE_ONLY_VALUE) != 0)
+        g_string_printf (string, "%.1f", value);
+      else
+        {
+         /* Translators: The first "%.1f" is replaced with the value, the "%s" with a unit of the value.
+           * The order can be changed with "%$2s %$1.1f". Keep the no-break space between the value and
+           * the unit symbol */
+          g_string_printf (string, C_ ("format-size", "%.1f %s"), value, units);
+        }
     }
 
   if (flags & G_FORMAT_SIZE_LONG_FORMAT)
diff --git a/glib/gutils.h b/glib/gutils.h
index 08c187040..f9ac8010c 100644
--- a/glib/gutils.h
+++ b/glib/gutils.h
@@ -307,7 +307,9 @@ typedef enum
   G_FORMAT_SIZE_DEFAULT     = 0,
   G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
   G_FORMAT_SIZE_IEC_UNITS   = 1 << 1,
-  G_FORMAT_SIZE_BITS        = 1 << 2
+  G_FORMAT_SIZE_BITS        = 1 << 2,
+  G_FORMAT_SIZE_ONLY_VALUE  = 1 << 3,
+  G_FORMAT_SIZE_ONLY_UNIT   = 1 << 4
 } GFormatSizeFlags;
 
 GLIB_AVAILABLE_IN_2_30
diff --git a/glib/tests/fileutils.c b/glib/tests/fileutils.c
index ae25578f9..ceb943bee 100644
--- a/glib/tests/fileutils.c
+++ b/glib/tests/fileutils.c
@@ -932,8 +932,14 @@ test_format_size_for_display (void)
   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0\302\240EB");
 
   check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_VALUE), "0");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_UNIT), "bytes");
   check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_VALUE), "1");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_UNIT), "byte");
   check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_VALUE), "2");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_UNIT), "bytes");
 
   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0\302\240KiB");
   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0\302\240MiB");
@@ -945,11 +951,19 @@ test_format_size_for_display (void)
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4\302\240MiB");
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5\302\240MB");
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5\302\240MB (238472938 
bytes)");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_VALUE), "227.4");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_ONLY_UNIT), "MiB");
 
 
   check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS), "0 bits");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_VALUE), "0");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_UNIT), "bits");
   check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS), "1 bit");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_VALUE), "1");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_UNIT), "bit");
   check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS), "2 bits");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_VALUE), "2");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_UNIT), "bits");
 
   check_string (g_format_size_full (2000ULL, G_FORMAT_SIZE_BITS), "2.0\302\240kb");
   check_string (g_format_size_full (2000ULL * 1000, G_FORMAT_SIZE_BITS), "2.0\302\240Mb");
@@ -960,11 +974,19 @@ test_format_size_for_display (void)
 
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS), "238.5\302\240Mb");
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_LONG_FORMAT), 
"238.5\302\240Mb (238472938 bits)");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_VALUE), "238.5");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_ONLY_UNIT), "Mb");
 
 
   check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "0 bits");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_VALUE), "0");
+  check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_UNIT), "bits");
   check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "1 bit");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_VALUE), "1");
+  check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_UNIT), "bit");
   check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), "2 bits");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_VALUE), "2");
+  check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_UNIT), "bits");
 
   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), 
"2.0\302\240Kib");
   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), 
"2.0\302\240Mib");
@@ -975,6 +997,8 @@ test_format_size_for_display (void)
 
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS), 
"227.4\302\240Mib");
   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_LONG_FORMAT), "227.4\302\240Mib (238472938 bits)");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_VALUE), "227.4");
+  check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS | 
G_FORMAT_SIZE_ONLY_UNIT), "Mib");
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]