qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: qemu-devel <qemu-devel@nongnu.org>, Stefan Weil <sw@weilnetz.de>,
	 qemu-trivial <qemu-trivial@nongnu.org>
Subject: Re: [PATCH 2/5] util/uri: Simplify uri_string_unescape()
Date: Tue, 23 Jan 2024 11:25:38 +0100	[thread overview]
Message-ID: <CABgObfZRmavOuv-1iXEyjfcMnKYBGwytYTY7cATpNA14QtHM=g@mail.gmail.com> (raw)
In-Reply-To: <20240122191753.103118-3-thuth@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2781 bytes --]

Il lun 22 gen 2024, 20:18 Thomas Huth <thuth@redhat.com> ha scritto:

> uri_string_unescape() basically does the same as the glib function
> g_uri_unescape_string(), with just an additional length parameter.
>

You can replace it altogether with g_uri_unescape_segment.

Paolo

So we can simplify this function a lot by limiting the length with
> g_strndup() first and then by calling g_uri_unescape_string() instead
> of walking through the string manually.
>
> Suggested-by: Stefan Weil <stefan.weil@weilnetz.de>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  util/uri.c | 49 +++----------------------------------------------
>  1 file changed, 3 insertions(+), 46 deletions(-)
>
> diff --git a/util/uri.c b/util/uri.c
> index 33b6c7214e..2a75f535ba 100644
> --- a/util/uri.c
> +++ b/util/uri.c
> @@ -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
> @@ -1585,8 +1576,7 @@ static int is_hex(char c)
>   */
>  char *uri_string_unescape(const char *str, int len)
>  {
> -    char *ret, *out;
> -    const char *in;
> +    g_autofree char *lstr = NULL;
>
>      if (str == NULL) {
>          return NULL;
> @@ -1594,42 +1584,9 @@ char *uri_string_unescape(const char *str, int len)
>      if (len <= 0) {
>          len = strlen(str);
>      }
> -    if (len < 0) {
> -        return NULL;
> -    }
> -
> -    ret = g_malloc(len + 1);
> +    lstr = g_strndup(str, len);
>
> -    in = str;
> -    out = ret;
> -    while (len > 0) {
> -        if ((len > 2) && (*in == '%') && (is_hex(in[1])) &&
> (is_hex(in[2]))) {
> -            in++;
> -            if ((*in >= '0') && (*in <= '9')) {
> -                *out = (*in - '0');
> -            } else if ((*in >= 'a') && (*in <= 'f')) {
> -                *out = (*in - 'a') + 10;
> -            } else if ((*in >= 'A') && (*in <= 'F')) {
> -                *out = (*in - 'A') + 10;
> -            }
> -            in++;
> -            if ((*in >= '0') && (*in <= '9')) {
> -                *out = *out * 16 + (*in - '0');
> -            } else if ((*in >= 'a') && (*in <= 'f')) {
> -                *out = *out * 16 + (*in - 'a') + 10;
> -            } else if ((*in >= 'A') && (*in <= 'F')) {
> -                *out = *out * 16 + (*in - 'A') + 10;
> -            }
> -            in++;
> -            len -= 3;
> -            out++;
> -        } else {
> -            *out++ = *in++;
> -            len--;
> -        }
> -    }
> -    *out = 0;
> -    return ret;
> +    return g_uri_unescape_string(lstr, NULL);
>  }
>
>  /**
> --
> 2.43.0
>
>

[-- Attachment #2: Type: text/html, Size: 4368 bytes --]

  parent reply	other threads:[~2024-01-23 10:26 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22 19:17 [PATCH 0/5] util/uri: Simplify the code, remove unused functions Thomas Huth
2024-01-22 19:17 ` [PATCH 1/5] util/uri: Remove the unused "target" argument from uri_string_unescape() Thomas Huth
2024-01-22 20:45   ` Stefan Weil via
2024-01-23  5:45   ` Philippe Mathieu-Daudé
2024-01-22 19:17 ` [PATCH 2/5] util/uri: Simplify uri_string_unescape() Thomas Huth
2024-01-22 21:22   ` Stefan Weil via
2024-01-23  6:01     ` Thomas Huth
2024-01-23 10:25   ` Paolo Bonzini [this message]
2024-01-23 11:21     ` Thomas Huth
2024-01-22 19:17 ` [PATCH 3/5] util/uri: Remove the uri_string_escape() function Thomas Huth
2024-01-22 20:59   ` Stefan Weil via
2024-01-23  6:03     ` Thomas Huth
2024-01-22 19:17 ` [PATCH 4/5] util/uri: Remove unused functions uri_resolve() and uri_resolve_relative() Thomas Huth
2024-01-22 21:03   ` Stefan Weil via
2024-01-22 19:17 ` [PATCH 5/5] util/uri: Remove unused macros ISA_RESERVED() and ISA_GEN_DELIM() Thomas Huth
2024-01-22 21:04   ` Stefan Weil via
2024-01-23  5:50   ` Philippe Mathieu-Daudé
2024-01-22 19:22 ` [PATCH 0/5] util/uri: Simplify the code, remove unused functions Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABgObfZRmavOuv-1iXEyjfcMnKYBGwytYTY7cATpNA14QtHM=g@mail.gmail.com' \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=sw@weilnetz.de \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).