Skip to contentSkip to conversation

util/uri: Remove is_hex() function

PATCH In review Thomas Huth 3 participants 4 messages Jan 12, 2024 – Jan 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/uri: Remove is_hex() function

We can simply use the g_ascii_isxdigit() from the glib instead.

---
 util/uri.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
util/uri.c +1 -10
index dcb3305236..7411c5ba14 100644
@@ -1561,15 +1561,6 @@ done_cd:
     return 0;
 }
 
-static int is_hex(char c)
-{
-    if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
-        ((c >= 'A') && (c <= 'F'))) {
-        return 1;
-    }
-    return 0;
-}
-
 /**
  * uri_string_unescape:
  * @str:  the string to unescape
@@ -1607,7 +1598,7 @@ char *uri_string_unescape(const char *str, int len, char *target)
     in = str;
     out = ret;
     while (len > 0) {
-        if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
+        if (len > 2 && *in == '%' && g_ascii_isxdigit(in[1]) && g_ascii_isxdigit(in[2])) {
             in++;
             if ((*in >= '0') && (*in <= '9')) {
                 *out = (*in - '0');
Signature · 1 line
2.43.0
#1 lore raw
Quoted 28 lines · Thomas Huth

We can simply use the g_ascii_isxdigit() from the glib instead.

---
 util/uri.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
util/uri.c +0 -9
index dcb3305236..7411c5ba14 100644
@@ -1561,15 +1561,6 @@ done_cd:
     return 0;
 }
 
-static int is_hex(char c)
-{
-    if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
-        ((c >= 'A') && (c <= 'F'))) {
-        return 1;
-    }
-    return 0;
-}
-
 /**
  * uri_string_unescape:
  * @str:  the string to unescape

util/uri.c char *uri_string_unescape(const char *str, int len, char *target) · line 1604

Quoted 8 lines
util/uri.c +1 -1
@@ -1607,7 +1598,7 @@ char *uri_string_unescape(const char *str, int len, char *target)
     in = str;
     out = ret;
     while (len > 0) {
-        if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
+        if (len > 2 && *in == '%' && g_ascii_isxdigit(in[1]) && g_ascii_isxdigit(in[2])) {
             in++;
             if ((*in >= '0') && (*in <= '9')) {
                 *out = (*in - '0');

Suggest to replace *in by in[0] while there, for symmetry.

Long line, easy to break:

           if (len > 2 && in[0] == '%'
               && g_ascii_isxdigit(in[1])
               && g_ascii_isxdigit(in[2])) {
#3 lore raw
Quoted 10 lines · Stefan Weil via

Am 12.01.24 um 07:35 schrieb Markus Armbruster:

Quoted 3 lines
Quoted 1 line · Thomas Huth

We can simply use the g_ascii_isxdigit() from the glib instead.

... or even use unescape_string() from the glib?

https://docs.gtk.org/glib/type_func.Uri.unescape_string.html

Thanks, that sounds like an interesting idea! I'll give it a try when I've got some spare time... not within the next days, though, so if you'd like to have a try instead, let me know!

Thomas