2 * String utility routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
29 case 'a': case 'A': return 10;
30 case 'b': case 'B': return 11;
31 case 'c': case 'C': return 12;
32 case 'd': case 'D': return 13;
33 case 'e': case 'E': return 14;
34 case 'f': case 'F': return 15;
39 /* Convert all ASCII letters to lower case, in place. */
41 ascii_strdown_inplace(gchar *str)
45 for (s = str; *s; s++)
46 /* What 'g_ascii_tolower (gchar c)' does, this should be slightly more efficient */
47 *s = g_ascii_isupper (*s) ? *s - 'A' + 'a' : *s;
52 /* Convert all ASCII letters to upper case, in place. */
54 ascii_strup_inplace(gchar *str)
58 for (s = str; *s; s++)
59 /* What 'g_ascii_toupper (gchar c)' does, this should be slightly more efficient */
60 *s = g_ascii_islower (*s) ? *s - 'a' + 'A' : *s;
65 /* Check if an entire string is printable. */
67 isprint_string(const gchar *str)
71 /* Loop until we reach the end of the string (a null) */
72 for(pos = 0; str[pos] != '\0'; pos++){
73 if(!g_ascii_isprint(str[pos])){
74 /* The string contains a non-printable character */
79 /* The string contains only printable characters */
83 /* Check if an entire string is digits. */
85 isdigit_string(guchar *str)
89 /* Loop until we reach the end of the string (a null) */
90 for(pos = 0; str[pos] != '\0'; pos++){
91 if(!g_ascii_isdigit(str[pos])){
92 /* The string contains a non-digit character */
97 /* The string contains only digits */
101 #define FORMAT_SIZE_UNIT_MASK 0x00ff
102 #define FORMAT_SIZE_PFX_MASK 0xff00
104 static const char *thousands_grouping_fmt = NULL;
107 static void test_printf_thousands_grouping(void) {
108 /* test whether g_printf works with "'" flag character */
109 gchar *str = g_strdup_printf("%'d", 22);
110 if (g_strcmp0(str, "22") == 0) {
111 thousands_grouping_fmt = "%'"G_GINT64_MODIFIER"d";
114 thousands_grouping_fmt = "%"G_GINT64_MODIFIER"d";
120 /* Given a size, return its value in a human-readable format */
121 /* This doesn't handle fractional values. We might want to make size a double. */
123 format_size(gint64 size, format_size_flags_e flags)
125 GString *human_str = g_string_new("");
128 gboolean is_small = FALSE;
129 static const gchar *prefix[] = {" T", " G", " M", " k", " Ti", " Gi", " Mi", " Ki"};
132 if (thousands_grouping_fmt == NULL)
133 test_printf_thousands_grouping();
135 if ((flags & FORMAT_SIZE_PFX_MASK) == format_size_prefix_iec) {
140 if (size / power / power / power / power >= 10) {
141 g_string_printf(human_str, thousands_grouping_fmt, size / power / power / power / power);
142 g_string_append(human_str, prefix[pfx_off]);
143 } else if (size / power / power / power >= 10) {
144 g_string_printf(human_str, thousands_grouping_fmt, size / power / power / power);
145 g_string_append(human_str, prefix[pfx_off+1]);
146 } else if (size / power / power >= 10) {
147 g_string_printf(human_str, thousands_grouping_fmt, size / power / power);
148 g_string_append(human_str, prefix[pfx_off+2]);
149 } else if (size / power >= 10) {
150 g_string_printf(human_str, thousands_grouping_fmt, size / power);
151 g_string_append(human_str, prefix[pfx_off+3]);
153 g_string_printf(human_str, thousands_grouping_fmt, size);
158 switch (flags & FORMAT_SIZE_UNIT_MASK) {
159 case format_size_unit_none:
161 case format_size_unit_bytes:
162 g_string_append(human_str, is_small ? " bytes" : "B");
164 case format_size_unit_bits:
165 g_string_append(human_str, is_small ? " bits" : "b");
167 case format_size_unit_bits_s:
168 g_string_append(human_str, is_small ? " bits/s" : "bps");
170 case format_size_unit_bytes_s:
171 g_string_append(human_str, is_small ? " bytes/s" : "Bps");
173 case format_size_unit_packets:
174 g_string_append(human_str, is_small ? " packets" : "packets");
176 case format_size_unit_packets_s:
177 g_string_append(human_str, is_small ? " packets/s" : "packets/s");
180 g_assert_not_reached();
183 ret_val = g_string_free(human_str, FALSE);
184 return g_strchomp(ret_val);
188 printable_char_or_period(gchar c)
190 return g_ascii_isprint(c) ? c : '.';
194 * Editor modelines - http://www.wireshark.org/tools/modelines.html
199 * indent-tabs-mode: t
202 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
203 * :indentSize=8:tabSize=8:noTabs=false: