From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E3987C433F5 for ; Thu, 20 Jan 2022 03:16:20 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5282E83434; Thu, 20 Jan 2022 04:16:18 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rWpKGGj6"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DC0C18378B; Thu, 20 Jan 2022 04:16:16 +0100 (CET) Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 58E5983325 for ; Thu, 20 Jan 2022 04:16:13 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pf1-x42d.google.com with SMTP id a5so4138077pfo.5 for ; Wed, 19 Jan 2022 19:16:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=6rItZsilM/yFYuFJSpZiTdB73JHQdjqIO0RXfsVwF4E=; b=rWpKGGj6z3gftjetbt4LHZY3kCzM3PE1F5JqrOK1Lh7W6T5CieV8W4pYd6rVNwEAve Qp7x9WAG9YjE970qGowvU2kq6ljYh6TA2RUVB/lpkLSbibv9mHo0kku+UeP2ft51qhiD nr9uQhikwxMmglAuB97eILG9irK2LILBvEoJmdBZ4o4XdPGZ+cRroSohy1z590cKNMB9 jLD0sungVaAmPE4SGgMRaqvv94sPQ5eJbhSD+23xvFp02hSi3ds75oYHpOztxgoRVlfq yiBoLN8By6VE5xfTebXqjqUW9w6f3XW8UNSrXqtyew1TlAbD03cTDaSi7NAd6MPCbVOM O+ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=6rItZsilM/yFYuFJSpZiTdB73JHQdjqIO0RXfsVwF4E=; b=LqSnxBu6gYsEpvDlqM+gvCX4kD+WN07vEzcW8pW4/BR9qO1AAILPCSbU1oPi+a1EH4 +vaOy8UQkQ7Srq4/CglKmti73EVDroWdfRhHWIoslveRbMQWVMo80aAmw1mFyr3ZjAGe h6X8HeCGtTHBXksUsgOlUKxlPB6GFvXVylabMe1A/IBQxJLCw+tLDzOpTj+uDpqgVBxg h8KYrwSO1jnapIXJyd0ofGG1LpWiifVhXrq/l7QmLnN4uw4082QizaB43GqVz88HSTDm VZTm/MGe3ROdyXXjwVyqFMmSJC+rwZ4ETu6cDb+XTskRgMTPw/eDARfD+923NpheW/gp h25A== X-Gm-Message-State: AOAM532pWatB/TtqUFPPNrFrqtxe4g05nG/mWWU0VhcNSosR7H8fGuh7 9BwEo6bRKyFWwopMridV2Qq1xA== X-Google-Smtp-Source: ABdhPJzz476NPiAMuvbvochcdMQamLbw6T3ih5fsl1f+aSgwAvEyBRhPJhNWzyrVcJIkum4ntm6Ulg== X-Received: by 2002:a05:6a00:18a8:b0:4c1:778b:16fe with SMTP id x40-20020a056a0018a800b004c1778b16femr33787039pfh.58.1642648571154; Wed, 19 Jan 2022 19:16:11 -0800 (PST) Received: from laputa ([2400:4050:c3e1:100:35ef:d002:eb6d:1b5]) by smtp.gmail.com with ESMTPSA id p42sm971857pfw.71.2022.01.19.19.16.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 19:16:10 -0800 (PST) Date: Thu, 20 Jan 2022 12:16:05 +0900 From: AKASHI Takahiro To: Heinrich Schuchardt Cc: Simon Glass , U-Boot Mailing List , Ilias Apalodimas , Daniel Schwierzeck , Dennis Gilmore , Steffen Jaeckel , Lukas Auer , Michal Simek , Tom Rini Subject: Re: [PATCH v3 04/31] lib: Add a way to find the postiion of a trailing number Message-ID: <20220120031605.GC42867@laputa> Mail-Followup-To: AKASHI Takahiro , Heinrich Schuchardt , Simon Glass , U-Boot Mailing List , Ilias Apalodimas , Daniel Schwierzeck , Dennis Gilmore , Steffen Jaeckel , Lukas Auer , Michal Simek , Tom Rini References: <20220119014315.1938157-1-sjg@chromium.org> <20220119014315.1938157-5-sjg@chromium.org> <8d8796be-f5ad-15ed-4a03-97519f9c172c@gmx.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8d8796be-f5ad-15ed-4a03-97519f9c172c@gmx.de> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean On Wed, Jan 19, 2022 at 12:27:09PM +0100, Heinrich Schuchardt wrote: > On 1/19/22 02:42, Simon Glass wrote: > > At present it is not possible to find out which part of the string is the > > number part and which is before it. Add a new variant which provides this > > feature, so we can separate the two in the caller. > > > > Signed-off-by: Simon Glass > > --- > > > > Changes in v3: > > - Change the function to return a pointer to the first digit > > - Add some tests, including one for 'abc123def456' > > > > include/vsprintf.h | 18 ++++++++++++++++++ > > lib/strto.c | 14 ++++++++++++-- > > test/str_ut.c | 13 ++++++++++++- > > 3 files changed, 42 insertions(+), 3 deletions(-) > > > > diff --git a/include/vsprintf.h b/include/vsprintf.h > > index 01d2248e04d..ce7a7aaa1cc 100644 > > --- a/include/vsprintf.h > > +++ b/include/vsprintf.h > > @@ -118,6 +118,24 @@ long trailing_strtol(const char *str); > > */ > > long trailing_strtoln(const char *str, const char *end); > > > > +/** > > + * trailing_strtoln_end() - extract trailing integer from a fixed-length string > > + * > > + * Given a fixed-length string this finds a trailing number on the string > > + * and returns it. For example, "abc123" would return 123. Only the > > + * characters between @str and @end - 1 are examined. If @end is NULL, it is > > + * set to str + strlen(str). > > + * > > + * @str: String to exxamine > > + * @end: Pointer to end of string to examine, or NULL to use the > > + * whole string > > + * @endp: If non-NULL, this is set to point to the character where the > > + * number starts, e.g. for "mmc0" this would be point to the '0'; if no > > + * trailing number is found, it is set to the end of the string > > + * @return training number if found, else -1 > > Return: > > https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html#function-documentation One of other common mistakes(?) that I can see in the repository is a violation of the rule below: ===8<=== Function parameters ~~~~~~~~~~~~~~~~~~~ Each function argument should be described in order, immediately following ^^^^^^^^^^^^^^^^^^^^^ the short function description. Do not leave a blank line between the function description and the arguments, nor between the arguments. ===>8=== For instance, in this patch, > > +/** > > + * trailing_strtoln_end() - extract trailing integer from a fixed-length string > > + * > > + * Given a fixed-length string this finds a trailing number on the string > > + * and returns it. For example, "abc123" would return 123. Only the > > + * characters between @str and @end - 1 are examined. If @end is NULL, it is > > + * set to str + strlen(str). > > + * > > + * @str: String to exxamine (snip) The structure of trailing_strtoln_end() looks like: Doesn't this matter so far in formatting Sphinx texts? -Takahiro Akashi > Best regards > > Heinrich > > > + */ > > +long trailing_strtoln_end(const char *str, const char *end, char const **endp); > > + > > /** > > * panic() - Print a message and reset/hang > > * > > diff --git a/lib/strto.c b/lib/strto.c > > index 58fc10ecaea..72cfef660fc 100644 > > --- a/lib/strto.c > > +++ b/lib/strto.c > > @@ -183,7 +183,7 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base) > > return simple_strtoull(cp, endp, base); > > } > > > > -long trailing_strtoln(const char *str, const char *end) > > +long trailing_strtoln_end(const char *str, const char *end, char const **endp) > > { > > const char *p; > > > > @@ -192,14 +192,24 @@ long trailing_strtoln(const char *str, const char *end) > > p = end - 1; > > if (p > str && isdigit(*p)) { > > do { > > - if (!isdigit(p[-1])) > > + if (!isdigit(p[-1])) { > > + if (endp) > > + *endp = p; > > return dectoul(p, NULL); > > + } > > } while (--p > str); > > } > > + if (endp) > > + *endp = end; > > > > return -1; > > } > > > > +long trailing_strtoln(const char *str, const char *end) > > +{ > > + return trailing_strtoln_end(str, end, NULL); > > +} > > + > > long trailing_strtol(const char *str) > > { > > return trailing_strtoln(str, NULL); > > diff --git a/test/str_ut.c b/test/str_ut.c > > index 058b3594379..5a844347c2b 100644 > > --- a/test/str_ut.c > > +++ b/test/str_ut.c > > @@ -244,7 +244,9 @@ STR_TEST(str_xtoa, 0); > > > > static int str_trailing(struct unit_test_state *uts) > > { > > - char str1[] = "abc123def"; > > + const char str1[] = "abc123def"; > > + const char str2[] = "abc123def456"; > > + const char *end; > > > > ut_asserteq(-1, trailing_strtol("")); > > ut_asserteq(-1, trailing_strtol("123")); > > @@ -259,6 +261,15 @@ static int str_trailing(struct unit_test_state *uts) > > > > ut_asserteq(3, trailing_strtol("a3")); > > > > + ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end)); > > + ut_asserteq(3, end - str1); > > + > > + ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end)); > > + ut_asserteq(7, end - str1); > > + > > + ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end)); > > + ut_asserteq(9, end - str2); > > + > > return 0; > > } > > STR_TEST(str_trailing, 0); >