From 5319f36788c6d6e3333fd89fe7e6ffb2f6574214 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 17 Aug 2022 12:30:38 +0200 Subject: [PATCH] util/string.h: support 'int' for 'ascii_to' This patch complements the 'long' version of the 'ascii_to' conversion function by an 'int' version. Fixes #4583 --- repos/base/include/util/string.h | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/repos/base/include/util/string.h b/repos/base/include/util/string.h index 683070132e..f82fbabacb 100644 --- a/repos/base/include/util/string.h +++ b/repos/base/include/util/string.h @@ -459,20 +459,21 @@ namespace Genode { /** - * Read signed long value from string + * Read signed value from string * * \return number of consumed characters */ - inline size_t ascii_to(const char *s, long &result) + template + inline size_t ascii_to_signed(const char *s, T &result) { - int i = 0; + size_t i = 0; /* read sign */ int sign = (*s == '-') ? -1 : 1; if (*s == '-' || *s == '+') { s++; i++; } - unsigned long value = 0; + T value = 0; size_t const j = ascii_to_unsigned(s, value, 0); @@ -484,6 +485,28 @@ namespace Genode { } + /** + * Read signed long value from string + * + * \return number of consumed characters + */ + inline size_t ascii_to(const char *s, long &result) + { + return ascii_to_signed(s, result); + } + + + /** + * Read signed integer value from string + * + * \return number of consumed characters + */ + inline size_t ascii_to(const char *s, int &result) + { + return ascii_to_signed(s, result); + } + + /** * Read 'Number_of_bytes' value from string and handle the size suffixes *