From nobody Wed Nov 27 07:33:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1723207312000496.5328254001887; Fri, 9 Aug 2024 05:41:52 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id DE6301551; Fri, 9 Aug 2024 08:41:50 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 345F0147F; Fri, 9 Aug 2024 08:41:01 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 06E241395; Fri, 9 Aug 2024 08:40:57 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 8A2FD1425 for ; Fri, 9 Aug 2024 08:40:56 -0400 (EDT) Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-656-aOfUehA0M8-DbjQB5cU49g-1; Fri, 09 Aug 2024 08:40:54 -0400 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B723F1955D47 for ; Fri, 9 Aug 2024 12:40:53 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.19]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D128C19560A3 for ; Fri, 9 Aug 2024 12:40:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723207256; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f7bxVicU5niepOTVXPkH0BLFabH66P1ffQe+Rlw7Slo=; b=C3LJQc+2Lalo2duxy9LZD68lGZB7AHjTAusqV5qDq18xxhDiNFvlOgeEHyPX0+5I8TBQjC HoWT+UbaDN8ZoBZZdOnTJOeXHONdtkfis19gUNoVbT34r12y7V6awEwluonZMBjpojeGeH dWczzx4k0YqxrpoKHv2cVldfPnZh6tk= X-MC-Unique: aOfUehA0M8-DbjQB5cU49g-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH v2 1/3] glibcompat: "Backport" 'g_string_replace' Date: Fri, 9 Aug 2024 14:40:47 +0200 Message-ID: <1e5d45164ed60882e9d43046d2b1eff27527f3eb.1723207140.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JKC7WGIPGE6UL2FN3TOEEMUCVO5KIA5E X-Message-ID-Hash: JKC7WGIPGE6UL2FN3TOEEMUCVO5KIA5E X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1723207312836116600 Content-Type: text/plain; charset="utf-8" Backport the implementation of 'g_string_replace' until we require at least glib-2.68 Signed-off-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/glibcompat.c | 65 ++++++++++++++++++++++++++++++++++++++++ src/util/glibcompat.h | 10 +++++++ 3 files changed, 76 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d15d6a6a9d..0accca442a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1864,6 +1864,7 @@ vir_g_fsync; vir_g_source_unref; vir_g_strdup_printf; vir_g_strdup_vprintf; +vir_g_string_replace; # util/viracpi.c diff --git a/src/util/glibcompat.c b/src/util/glibcompat.c index d8912b323b..98dcfab389 100644 --- a/src/util/glibcompat.c +++ b/src/util/glibcompat.c @@ -155,3 +155,68 @@ void vir_g_source_unref(GSource *src, GMainContext *ct= x) } #endif + + +/** + * Adapted (to pass syntax check) from 'g_string_replace' from + * glib-2.81.1. Drop once minimum glib is bumped to 2.68. + * + * g_string_replace: + * @string: a #GString + * @find: the string to find in @string + * @replace: the string to insert in place of @find + * @limit: the maximum instances of @find to replace with @replace, or `0`= for + * no limit + * + * Replaces the string @find with the string @replace in a #GString up to + * @limit times. If the number of instances of @find in the #GString is + * less than @limit, all instances are replaced. If @limit is `0`, + * all instances of @find are replaced. + * + * If @find is the empty string, since versions 2.69.1 and 2.68.4 the + * replacement will be inserted no more than once per possible position + * (beginning of string, end of string and between characters). This did + * not work correctly in earlier versions. + * + * Returns: the number of find and replace operations performed. + * + * Since: 2.68 + */ +guint +vir_g_string_replace(GString *string, + const gchar *find, + const gchar *replace, + guint limit) +{ + gsize f_len, r_len, pos; + gchar *cur, *next; + guint n =3D 0; + + g_return_val_if_fail(string !=3D NULL, 0); + g_return_val_if_fail(find !=3D NULL, 0); + g_return_val_if_fail(replace !=3D NULL, 0); + + f_len =3D strlen(find); + r_len =3D strlen(replace); + cur =3D string->str; + + while ((next =3D strstr(cur, find)) !=3D NULL) { + pos =3D next - string->str; + g_string_erase(string, pos, f_len); + g_string_insert(string, pos, replace); + cur =3D string->str + pos + r_len; + n++; + /* Only match the empty string once at any given position, to + * avoid infinite loops */ + if (f_len =3D=3D 0) { + if (cur[0] =3D=3D '\0') + break; + else + cur++; + } + if (n =3D=3D limit) + break; + } + + return n; +} diff --git a/src/util/glibcompat.h b/src/util/glibcompat.h index 2542b4d5dc..3518023a41 100644 --- a/src/util/glibcompat.h +++ b/src/util/glibcompat.h @@ -85,6 +85,16 @@ char *vir_g_strdup_vprintf(const char *msg, va_list args) void vir_g_source_unref(GSource *src, GMainContext *ctx); + +/* Drop once we require glib-2.68 at minimum */ +guint +vir_g_string_replace(GString *string, + const gchar *find, + const gchar *replace, + guint limit); +#undef g_string_replace +#define g_string_replace vir_g_string_replace + #if !GLIB_CHECK_VERSION(2, 73, 2) # if (defined(__has_attribute) && __has_attribute(__noinline__)) || G_GNUC= _CHECK_VERSION (2, 96) # if defined (__cplusplus) && __cplusplus >=3D 201103L --=20 2.45.2