From nobody Sun May 5 11:40:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1491065181461135.01832509552548; Sat, 1 Apr 2017 09:46:21 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8E0C7C04B927; Sat, 1 Apr 2017 16:46:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1E63C4FA20; Sat, 1 Apr 2017 16:46:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0DB1F18523C7; Sat, 1 Apr 2017 16:46:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v31GkFUr011611 for ; Sat, 1 Apr 2017 12:46:15 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7A7EDA05C4; Sat, 1 Apr 2017 16:46:15 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-54.ams2.redhat.com [10.36.116.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id E7D1717C1C for ; Sat, 1 Apr 2017 16:46:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8E0C7C04B927 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8E0C7C04B927 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Sat, 1 Apr 2017 18:46:08 +0200 Message-Id: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] util: add support for parsing roman numerals X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Sat, 01 Apr 2017 16:46:20 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" In loving memory of the glorious Roman Empire, add support for parsing roman numerals to all the positive virStrToLong variants (those that reject negative numbers). Now you can finally do: CCLVI --- src/util/virstring.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 78 insertions(+) diff --git a/src/util/virstring.c b/src/util/virstring.c index 69abc26..81c2052 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -313,6 +313,69 @@ virStringListGetFirstWithPrefix(char **strings, return NULL; } =20 +const char *digits =3D "IVXLCDM"; +const unsigned values[] =3D { 1, 5, 10, 50, 100, 500, 1000 }; + +static int +virRomanStrToLong_ul(char const *s, unsigned long *result) +{ + unsigned long ret =3D 0; + char const *cur; + + if (*s =3D=3D '\0') + return -1; + + for (cur =3D s; *cur !=3D '\0'; cur++) { + size_t pos; + char const *lookup; + + lookup =3D strchr(digits, *cur); + if (!lookup) + return -1; + pos =3D lookup - digits; + + if (*(cur + 1) !=3D '\0' && strchr(digits + pos + 1, *(cur + 1))) { + ret -=3D values[pos]; + } else { + if (ret > ULONG_MAX - values[pos]) + return -1; + ret +=3D values[pos]; + } + } + *result =3D ret; + return 0; +} + +static int +virRomanStrToLong_ull(char const *s, unsigned long long *result) +{ + unsigned long long ret =3D 0; + char const *cur; + + if (*s =3D=3D '\0') + return -1; + + for (cur =3D s; *cur !=3D '\0'; cur++) { + size_t pos; + char const *lookup; + + lookup =3D strchr(digits, *cur); + if (!lookup) + return -1; + pos =3D lookup - digits; + + if (*(cur + 1) !=3D '\0' && strchr(digits + pos + 1, *(cur + 1))) { + ret -=3D values[pos]; + } else { + if (ret > ULLONG_MAX - values[pos]) + return -1; + ret +=3D values[pos]; + } + } + *result =3D ret; + return 0; +} + /* Like strtol, but produce an "int" result, and check more carefully. Return 0 upon success; return -1 to indicate failure. When END_PTR is NULL, the byte after the final valid digit must be NUL. @@ -379,6 +442,11 @@ virStrToLong_uip(char const *s, char **end_ptr, int ba= se, unsigned int *result) char *p; bool err =3D false; =20 + if (virRomanStrToLong_ul(s, &val) =3D=3D 0) { + *result =3D val; + return 0; + } + errno =3D 0; val =3D strtoul(s, &p, base); /* exempt from syntax-check */ err =3D (memchr(s, '-', p - s) || @@ -441,6 +509,11 @@ virStrToLong_ulp(char const *s, char **end_ptr, int ba= se, char *p; int err; =20 + if (virRomanStrToLong_ul(s, &val) =3D=3D 0) { + *result =3D val; + return 0; + } + errno =3D 0; val =3D strtoul(s, &p, base); /* exempt from syntax-check */ err =3D (memchr(s, '-', p - s) || @@ -504,6 +577,11 @@ virStrToLong_ullp(char const *s, char **end_ptr, int b= ase, char *p; int err; =20 + if (virRomanStrToLong_ull(s, &val) =3D=3D 0) { + *result =3D val; + return 0; + } + errno =3D 0; val =3D strtoull(s, &p, base); /* exempt from syntax-check */ err =3D (memchr(s, '-', p - s) || --=20 2.10.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list