sysdeps: Remove _dbus_string_append_int()

It's a wrapper around snprintf(), so we are not gaining any efficiency
versus _dbus_string_append_printf(), and might as well use the more
general function instead. Doing it this way might even be a little *more*
efficient, since it reduces reallocations; it's certainly more concise.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2023-08-23 12:04:33 +01:00 committed by Ralf Habacker
parent ac20c7a4ec
commit e159f1f406
9 changed files with 9 additions and 79 deletions

View file

@ -416,16 +416,8 @@ create_unique_client_name (BusRegistry *registry,
/* appname:MAJOR-MINOR */
if (!_dbus_string_append (str, ":"))
return FALSE;
if (!_dbus_string_append_int (str, next_major_number))
return FALSE;
if (!_dbus_string_append (str, "."))
return FALSE;
if (!_dbus_string_append_int (str, next_minor_number))
if (!_dbus_string_append_printf (str, ":%d.%d",
next_major_number, next_minor_number))
return FALSE;
next_minor_number += 1;

View file

@ -670,15 +670,9 @@ sha1_handle_first_client_response (DBusAuth *auth,
&tmp2, _dbus_string_get_length (&tmp2)))
goto out;
if (!_dbus_string_append (&tmp2, " "))
if (!_dbus_string_append_printf (&tmp2, " %d ", auth->cookie_id))
goto out;
if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
goto out;
if (!_dbus_string_append (&tmp2, " "))
goto out;
if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES, &error))
{
if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))

View file

@ -566,18 +566,8 @@ _dbus_keyring_reload (DBusKeyring *keyring,
i = 0;
while (i < n_keys)
{
if (!_dbus_string_append_int (&contents,
keys[i].id))
goto nomem;
if (!_dbus_string_append_byte (&contents, ' '))
goto nomem;
if (!_dbus_string_append_printf (&contents, "%" DBUS_INT64_MODIFIER "d",
keys[i].creation_time))
goto nomem;
if (!_dbus_string_append_byte (&contents, ' '))
if (!_dbus_string_append_printf (&contents, "%d %" DBUS_INT64_MODIFIER "d ",
keys[i].id, keys[i].creation_time))
goto nomem;
if (!_dbus_string_hex_encode (&keys[i].secret, 0,

View file

@ -212,9 +212,6 @@ dbus_bool_t _dbus_string_append_len (DBusString *str,
const char *buffer,
int len);
DBUS_PRIVATE_EXPORT
dbus_bool_t _dbus_string_append_int (DBusString *str,
long value);
DBUS_PRIVATE_EXPORT
dbus_bool_t _dbus_string_append_byte (DBusString *str,
unsigned char byte);
DBUS_PRIVATE_EXPORT

View file

@ -273,8 +273,7 @@ _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
return FALSE;
}
if (!_dbus_string_append_int (&pid, pid_to_write) ||
!_dbus_string_append (&pid, "\n"))
if (!_dbus_string_append_printf (&pid, DBUS_PID_FORMAT "\n", pid_to_write))
{
_dbus_string_free (&pid);
_DBUS_SET_OOM (error);

View file

@ -203,8 +203,7 @@ _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
return FALSE;
}
if (!_dbus_string_append_int (&pid, pid_to_write) ||
!_dbus_string_append (&pid, "\n"))
if (!_dbus_string_append_printf (&pid, DBUS_PID_FORMAT "\n", pid_to_write))
{
_dbus_string_free (&pid);
_DBUS_SET_OOM (error);

View file

@ -354,43 +354,6 @@ _dbus_split_paths_and_append (DBusString *dirs,
*
* @{
*/
/**
* Appends an integer to a DBusString.
*
* @param str the string
* @param value the integer value
* @returns #FALSE if not enough memory or other failure.
*/
dbus_bool_t
_dbus_string_append_int (DBusString *str,
long value)
{
/* this calculation is from comp.lang.c faq */
#define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
int orig_len;
int i;
char *buf;
orig_len = _dbus_string_get_length (str);
if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
return FALSE;
buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
snprintf (buf, MAX_LONG_LEN, "%ld", value);
i = 0;
while (*buf)
{
++buf;
++i;
}
_dbus_string_shorten (str, MAX_LONG_LEN - i);
return TRUE;
}
/**
* Parses an integer contained in a DBusString. Either return parameter

View file

@ -534,7 +534,7 @@ _dbus_string_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
if (!_dbus_string_init (&str))
_dbus_test_fatal ("failed to init string");
if (!_dbus_string_append_int (&str, 27))
if (!_dbus_string_append_printf (&str, "%d", 27))
_dbus_test_fatal ("failed to append int");
i = _dbus_string_get_length (&str);

View file

@ -660,11 +660,7 @@ _dbus_misc_test (const char *test_data_dir _DBUS_GNUC_UNUSED)
if (!_dbus_string_init (&str))
_dbus_test_fatal ("no memory");
if (!(_dbus_string_append_int (&str, major) &&
_dbus_string_append_byte (&str, '.') &&
_dbus_string_append_int (&str, minor) &&
_dbus_string_append_byte (&str, '.') &&
_dbus_string_append_int (&str, micro)))
if (!_dbus_string_append_printf (&str, "%d.%d.%d", major, minor, micro))
_dbus_test_fatal ("no memory");
_dbus_test_check (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));