Remove duplicate format_size() function
We have two format_size()s, with and without wmem scoped memory. Move the wmem version to wsutil and add a convenience macro to use g_malloc()ed memory.
This commit is contained in:
parent
133b0c583f
commit
925e01b23f
|
@ -660,7 +660,6 @@ libwireshark.so.0 libwireshark0 #MINVER#
|
|||
follow_iterate_followers@Base 2.1.0
|
||||
follow_reset_stream@Base 2.1.0
|
||||
follow_tvb_tap_listener@Base 2.1.0
|
||||
format_size_wmem@Base 3.3.0
|
||||
format_text@Base 1.9.1
|
||||
format_text_chr@Base 1.12.0~rc1
|
||||
format_text_string@Base 3.3.0
|
||||
|
|
|
@ -69,7 +69,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
|
|||
filetime_to_nstime@Base 2.0.0
|
||||
find_codec@Base 3.1.0
|
||||
find_last_pathname_separator@Base 1.12.0~rc1
|
||||
format_size@Base 1.10.0
|
||||
format_size_wmem@Base 3.5.0
|
||||
free_progdirs@Base 2.3.0
|
||||
g_memdup2@Base 3.5.0
|
||||
get_basename@Base 1.12.0~rc1
|
||||
|
@ -273,6 +273,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
|
|||
wmem_strbuf_append_printf@Base 3.5.0
|
||||
wmem_strbuf_append_unichar@Base 3.5.0
|
||||
wmem_strbuf_append_vprintf@Base 3.5.0
|
||||
wmem_strbuf_destroy@Base 3.5.0
|
||||
wmem_strbuf_finalize@Base 3.5.0
|
||||
wmem_strbuf_get_len@Base 3.5.0
|
||||
wmem_strbuf_get_str@Base 3.5.0
|
||||
|
|
|
@ -1689,15 +1689,6 @@ string_replace(const gchar* str, const gchar *old_val, const gchar *new_val)
|
|||
return new_str;
|
||||
}
|
||||
|
||||
gchar*
|
||||
format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags)
|
||||
{
|
||||
gchar *str = format_size(size, flags);
|
||||
gchar *ptr = wmem_strdup(allocator, str);
|
||||
g_free(str);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
||||
*
|
||||
|
|
|
@ -358,24 +358,6 @@ gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
|
|||
WS_DLL_PUBLIC
|
||||
gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
|
||||
|
||||
/**
|
||||
* format_size_wmem:
|
||||
* Based on format_size (wsutil/str_util.h)
|
||||
*
|
||||
* Given a size, return its value in a human-readable format
|
||||
*
|
||||
* Prefixes up to "T/Ti" (tera, tebi) are currently supported.
|
||||
*
|
||||
* @param allocator An enumeration of the different types of available allocators.
|
||||
* @param size The size value
|
||||
* @param flags Flags to control the output (unit of measurement,
|
||||
* SI vs IEC, etc). Unit, prefix and suffix flags may be ORed together.
|
||||
* @return A newly-allocated string representing the value.
|
||||
*/
|
||||
WS_DLL_PUBLIC
|
||||
gchar*
|
||||
format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
|
|
@ -125,24 +125,25 @@ static const char *thousands_grouping_fmt = NULL;
|
|||
|
||||
DIAG_OFF(format)
|
||||
static void test_printf_thousands_grouping(void) {
|
||||
/* test whether g_printf works with "'" flag character */
|
||||
gchar *str = g_strdup_printf("%'d", 22);
|
||||
if (g_strcmp0(str, "22") == 0) {
|
||||
/* test whether wmem_strbuf works with "'" flag character */
|
||||
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
|
||||
wmem_strbuf_append_printf(buf, "%'d", 22);
|
||||
if (g_strcmp0(wmem_strbuf_get_str(buf), "22") == 0) {
|
||||
thousands_grouping_fmt = "%'"G_GINT64_MODIFIER"d";
|
||||
} else {
|
||||
/* Don't use */
|
||||
thousands_grouping_fmt = "%"G_GINT64_MODIFIER"d";
|
||||
}
|
||||
g_free(str);
|
||||
wmem_strbuf_destroy(buf);
|
||||
}
|
||||
DIAG_ON(format)
|
||||
|
||||
/* Given a size, return its value in a human-readable format */
|
||||
/* This doesn't handle fractional values. We might want to make size a double. */
|
||||
gchar *
|
||||
format_size(gint64 size, format_size_flags_e flags)
|
||||
format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags)
|
||||
{
|
||||
GString *human_str = g_string_new("");
|
||||
wmem_strbuf_t *human_str = wmem_strbuf_new(allocator, NULL);
|
||||
int power = 1000;
|
||||
int pfx_off = 0;
|
||||
gboolean is_small = FALSE;
|
||||
|
@ -158,19 +159,19 @@ format_size(gint64 size, format_size_flags_e flags)
|
|||
}
|
||||
|
||||
if (size / power / power / power / power >= 10) {
|
||||
g_string_printf(human_str, thousands_grouping_fmt, size / power / power / power / power);
|
||||
g_string_append(human_str, prefix[pfx_off]);
|
||||
wmem_strbuf_append_printf(human_str, thousands_grouping_fmt, size / power / power / power / power);
|
||||
wmem_strbuf_append(human_str, prefix[pfx_off]);
|
||||
} else if (size / power / power / power >= 10) {
|
||||
g_string_printf(human_str, thousands_grouping_fmt, size / power / power / power);
|
||||
g_string_append(human_str, prefix[pfx_off+1]);
|
||||
wmem_strbuf_append_printf(human_str, thousands_grouping_fmt, size / power / power / power);
|
||||
wmem_strbuf_append(human_str, prefix[pfx_off+1]);
|
||||
} else if (size / power / power >= 10) {
|
||||
g_string_printf(human_str, thousands_grouping_fmt, size / power / power);
|
||||
g_string_append(human_str, prefix[pfx_off+2]);
|
||||
wmem_strbuf_append_printf(human_str, thousands_grouping_fmt, size / power / power);
|
||||
wmem_strbuf_append(human_str, prefix[pfx_off+2]);
|
||||
} else if (size / power >= 10) {
|
||||
g_string_printf(human_str, thousands_grouping_fmt, size / power);
|
||||
g_string_append(human_str, prefix[pfx_off+3]);
|
||||
wmem_strbuf_append_printf(human_str, thousands_grouping_fmt, size / power);
|
||||
wmem_strbuf_append(human_str, prefix[pfx_off+3]);
|
||||
} else {
|
||||
g_string_printf(human_str, thousands_grouping_fmt, size);
|
||||
wmem_strbuf_append_printf(human_str, thousands_grouping_fmt, size);
|
||||
is_small = TRUE;
|
||||
}
|
||||
|
||||
|
@ -178,28 +179,28 @@ format_size(gint64 size, format_size_flags_e flags)
|
|||
case format_size_unit_none:
|
||||
break;
|
||||
case format_size_unit_bytes:
|
||||
g_string_append(human_str, is_small ? " bytes" : "B");
|
||||
wmem_strbuf_append(human_str, is_small ? " bytes" : "B");
|
||||
break;
|
||||
case format_size_unit_bits:
|
||||
g_string_append(human_str, is_small ? " bits" : "b");
|
||||
wmem_strbuf_append(human_str, is_small ? " bits" : "b");
|
||||
break;
|
||||
case format_size_unit_bits_s:
|
||||
g_string_append(human_str, is_small ? " bits/s" : "bps");
|
||||
wmem_strbuf_append(human_str, is_small ? " bits/s" : "bps");
|
||||
break;
|
||||
case format_size_unit_bytes_s:
|
||||
g_string_append(human_str, is_small ? " bytes/s" : "Bps");
|
||||
wmem_strbuf_append(human_str, is_small ? " bytes/s" : "Bps");
|
||||
break;
|
||||
case format_size_unit_packets:
|
||||
g_string_append(human_str, is_small ? " packets" : "packets");
|
||||
wmem_strbuf_append(human_str, is_small ? " packets" : "packets");
|
||||
break;
|
||||
case format_size_unit_packets_s:
|
||||
g_string_append(human_str, is_small ? " packets/s" : "packets/s");
|
||||
wmem_strbuf_append(human_str, is_small ? " packets/s" : "packets/s");
|
||||
break;
|
||||
default:
|
||||
ws_assert_not_reached();
|
||||
}
|
||||
|
||||
ret_val = g_string_free(human_str, FALSE);
|
||||
ret_val = wmem_strbuf_finalize(human_str);
|
||||
return g_strchomp(ret_val);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
#include <glib.h>
|
||||
#include "ws_symbol_export.h"
|
||||
|
||||
#include <wsutil/wmem/wmem.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
@ -105,7 +107,9 @@ typedef enum {
|
|||
* @return A newly-allocated string representing the value.
|
||||
*/
|
||||
WS_DLL_PUBLIC
|
||||
gchar *format_size(gint64 size, format_size_flags_e flags);
|
||||
gchar *format_size_wmem(wmem_allocator_t *allocator, gint64 size, format_size_flags_e flags);
|
||||
|
||||
#define format_size(size, flags) format_size_wmem(NULL, size, flags)
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
gchar printable_char_or_period(gchar c);
|
||||
|
|
|
@ -294,6 +294,15 @@ wmem_strbuf_finalize(wmem_strbuf_t *strbuf)
|
|||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
wmem_strbuf_destroy(wmem_strbuf_t *strbuf)
|
||||
{
|
||||
wmem_allocator_t *allocator = strbuf->allocator;
|
||||
|
||||
wmem_free(allocator, strbuf->str);
|
||||
wmem_free(allocator, strbuf);
|
||||
}
|
||||
|
||||
/*
|
||||
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
||||
*
|
||||
|
|
|
@ -97,6 +97,10 @@ WS_DLL_PUBLIC
|
|||
char *
|
||||
wmem_strbuf_finalize(wmem_strbuf_t *strbuf);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
void
|
||||
wmem_strbuf_destroy(wmem_strbuf_t *strbuf);
|
||||
|
||||
/** @}
|
||||
* @} */
|
||||
|
||||
|
|
Loading…
Reference in New Issue