Ticket #3666: 0001-Ticket-3666-Improper-use-of-IEC-and-SI-prefixes-for-.2.patch

File 0001-Ticket-3666-Improper-use-of-IEC-and-SI-prefixes-for-.2.patch, 7.4 KB (added by michael-o, 3 years ago)
  • lib/util.c

    From 89ef5ca1550959db7fa8082ecc116ee38445b0f3 Mon Sep 17 00:00:00 2001
    From: Michael Osipov <1983-01-06@gmx.net>
    Date: Thu, 25 Aug 2016 15:09:03 +0200
    Subject: [PATCH] Ticket #3666: Improper use of IEC and SI prefixes for size in
     size_trunc_len()
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    
    size_trunc_len() has been aligned to properly use either IEC or SI prefixes with
    the unit B (byte). Additionally always put a space between number and unit which
    is required by the norms.
    
    It is important to note that really small buffers have to be bigger than it
    appears because they store bytes and non-Latin scripts need more than one byte
    with UTF-8 to encode them, e.g., the string "1023 МиБ" in Russian requires
    11 bytes + null terminator.
    ---
     lib/util.c                | 31 ++++++++++++++++++-------------
     src/filemanager/filegui.c |  4 ++--
     src/filemanager/info.c    | 12 +++++++-----
     src/filemanager/panel.c   |  9 +++++----
     4 files changed, 32 insertions(+), 24 deletions(-)
    
    diff --git a/lib/util.c b/lib/util.c
    index 304351d..6e193b6 100644
    a b size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool 
    449449#endif 
    450450    }; 
    451451    /* *INDENT-ON* */ 
    452     static const char *const suffix[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", NULL }; 
    453     static const char *const suffix_lc[] = { "", "k", "m", "g", "t", "p", "e", "z", "y", NULL }; 
     452    static const char *const units_iec[] = { "B", "KiB", "MiB", "GiB", NULL }; 
     453    static const char *const units_si[] = { "B", "kB", "MB", "GB", NULL }; 
    454454 
    455     const char *const *sfx = use_si ? suffix_lc : suffix; 
     455    const char *const *sfx = use_si ? units_si : units_iec; 
    456456    int j = 0; 
    457457 
    458458    if (len == 0) 
    459459        len = 9; 
    460460#if SIZEOF_UINTMAX_T == 8 
    461461    /* 20 decimal digits are required to represent 8 bytes */ 
    462     else if (len > 19) 
    463         len = 19; 
     462    else if (len > 21) 
     463        len = 21; 
    464464#else 
    465465    /* 10 decimal digits are required to represent 4 bytes */ 
    466     else if (len > 9) 
    467         len = 9; 
     466    else if (len > 11) 
     467        len = 11; 
    468468#endif 
    469469 
    470470    /* 
    size_trunc_len (char *buffer, unsigned int len, uintmax_t size, int units, gbool 
    489489        { 
    490490            if (j == units) 
    491491            { 
    492                 /* Empty files will print "0" even with minimal width.  */ 
    493                 g_snprintf (buffer, len + 1, "%s", "0"); 
     492                /* Empty files will print "0 B" even with minimal width.  */ 
     493                g_snprintf (buffer, len + 1, "0 %s", _("B")); 
    494494            } 
    495495            else 
    496496            { 
    497                 /* Use "~K" or just "K" if len is 1.  Use "B" for bytes.  */ 
    498                 g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s", (j > 1) ? sfx[j - 1] : "B"); 
     497                /* Use "~KiB/kB" or just "KiB/kB" if len is 1.  Use "B" for bytes.  */ 
     498                g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s", 
     499                            (j > 1) ? _(sfx[j - 1]) : _("B")); 
    499500            } 
    500501            break; 
    501502        } 
    502503 
    503         if (size < power10[len - (j > 0 ? 1 : 0)]) 
     504        /* 
     505         * Offset calculation: 1 for space + 2*3 bytes for scaled units as multibyte 
     506         * encoding with UTF-8 for non-Latin scripts. 
     507         */ 
     508        if (size < power10[len - (1 + 6)]) 
    504509        { 
    505             g_snprintf (buffer, len + 1, "%" PRIuMAX "%s", size, sfx[j]); 
     510            g_snprintf (buffer, len + 1, "%" PRIuMAX " %s", size, _(sfx[j])); 
    506511            break; 
    507512        } 
    508513 
  • src/filemanager/filegui.c

    diff --git a/src/filemanager/filegui.c b/src/filemanager/filegui.c
    index 2b6871a..edf2502 100644
    a b file_progress_show_total (file_op_total_context_t * tctx, file_op_context_t * ct 
    10131013 
    10141014    if (ui->total_bytes_label != NULL) 
    10151015    { 
    1016         size_trunc_len (buffer2, 5, tctx->copied_bytes, 0, panels_options.kilobyte_si); 
     1016        size_trunc_len (buffer2, 11, tctx->copied_bytes, 0, panels_options.kilobyte_si); 
    10171017        if (!ctx->progress_totals_computed) 
    10181018            g_snprintf (buffer, sizeof (buffer), _(" Total: %s "), buffer2); 
    10191019        else 
    10201020        { 
    1021             size_trunc_len (buffer3, 5, ctx->progress_bytes, 0, panels_options.kilobyte_si); 
     1021            size_trunc_len (buffer3, 11, ctx->progress_bytes, 0, panels_options.kilobyte_si); 
    10221022            g_snprintf (buffer, sizeof (buffer), _(" Total: %s/%s "), buffer2, buffer3); 
    10231023        } 
    10241024 
  • src/filemanager/info.c

    diff --git a/src/filemanager/info.c b/src/filemanager/info.c
    index cb5cf6a..888d6fc 100644
    a b info_show_info (WInfo * info) 
    176176            tty_print_string (_("No space information")); 
    177177        else 
    178178        { 
    179             char buffer1[6], buffer2[6]; 
     179            char buffer1[12], buffer2[12]; 
    180180 
    181             size_trunc_len (buffer1, 5, myfs_stats.avail, 1, panels_options.kilobyte_si); 
    182             size_trunc_len (buffer2, 5, myfs_stats.total, 1, panels_options.kilobyte_si); 
     181            size_trunc_len (buffer1, sizeof(buffer1) - 1, myfs_stats.avail, 1, 
     182                            panels_options.kilobyte_si); 
     183            size_trunc_len (buffer2, sizeof(buffer2) - 1, myfs_stats.total, 1, 
     184                            panels_options.kilobyte_si); 
    183185            tty_printf (_("Free space: %s/%s (%d%%)"), buffer1, buffer2, 
    184186                        myfs_stats.total == 0 ? 0 : 
    185187                        (int) (100 * (long double) myfs_stats.avail / myfs_stats.total)); 
    info_show_info (WInfo * info) 
    232234        else 
    233235#endif 
    234236        { 
    235             char buffer[10]; 
    236             size_trunc_len (buffer, 9, st.st_size, 0, panels_options.kilobyte_si); 
     237            char buffer[12]; 
     238            size_trunc_len (buffer, sizeof(buffer) - 1, st.st_size, 0, panels_options.kilobyte_si); 
    237239            tty_printf (_("Size:       %s"), buffer); 
    238240#ifdef HAVE_STRUCT_STAT_ST_BLOCKS 
    239241            tty_printf (ngettext (" (%ld block)", " (%ld blocks)", 
  • src/filemanager/panel.c

    diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c
    index b18cbac..cbaac3e 100644
    a b static panel_field_t panel_fields[] = { 
    195195    } 
    196196    , 
    197197    { 
    198      "size", 7, FALSE, J_RIGHT, 
     198     "size", 8, FALSE, J_RIGHT, 
    199199     /* TRANSLATORS: one single character to represent 'size' sort mode  */ 
    200200     /* TRANSLATORS: no need to translate 'sort', it's just a context prefix  */ 
    201201     N_("sort|s"), 
    static panel_field_t panel_fields[] = { 
    205205    } 
    206206    , 
    207207    { 
    208      "bsize", 7, FALSE, J_RIGHT, 
     208     "bsize", 8, FALSE, J_RIGHT, 
    209209     "", 
    210210     N_("Block Size"), FALSE, FALSE, 
    211211     string_file_size_brief, 
    string_file_size (file_entry_t * fe, int len) 
    515515        format_device_number (buffer, len + 1, fe->st.st_rdev); 
    516516    else 
    517517#endif 
    518         size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si); 
     518        size_trunc_len (buffer, (unsigned int) (len + 3), fe->st.st_size, 0, 
     519                        panels_options.kilobyte_si); 
    519520 
    520521    return buffer; 
    521522} 
    show_free_space (const WPanel * panel) 
    11541155    if (myfs_stats.avail != 0 || myfs_stats.total != 0) 
    11551156    { 
    11561157        const Widget *w = CONST_WIDGET (panel); 
    1157         char buffer1[6], buffer2[6], tmp[BUF_SMALL]; 
     1158        char buffer1[12], buffer2[12], tmp[BUF_SMALL]; 
    11581159 
    11591160        size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1, 
    11601161                        panels_options.kilobyte_si);