Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48D9EC4338F for ; Sat, 24 Jul 2021 15:05:23 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id B48CE60E9C for ; Sat, 24 Jul 2021 15:05:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B48CE60E9C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 929BA82F1A; Sat, 24 Jul 2021 17:05:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="CwjkSMUF"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6A0F081E53; Sat, 24 Jul 2021 17:04:43 +0200 (CEST) Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) (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 19EB182E3F for ; Sat, 24 Jul 2021 17:03:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x22b.google.com with SMTP id z26so5352758oih.10 for ; Sat, 24 Jul 2021 08:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tfw2XXxfZivz+5SeLlCAzNKakNVcVKK4tYL4dA7Hvos=; b=CwjkSMUFxOUJYZ1MThm71NuVlcaN56rBJLVC66NF0DvupX0Uoth6lgKePv4aVhAIkU 0pRqjHulg5DXtKc0ybEYcZXPFm35LdlqPfQNMzvNc+RlqciIxRUCCMhUV5fm9ZWVUNlE H8ua9luWlYowG9JpEfuNx8bnzeqLvapHobvDU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tfw2XXxfZivz+5SeLlCAzNKakNVcVKK4tYL4dA7Hvos=; b=Od6V33TjdiVEzXT5zq1GxVKc5IS2+W/rf0v67nBXGszJ5P4fVFgVsS4zxz3hKSgy7+ +wsTNAaAaLTm7nLgOcsGaLDtsoWIWNIia6Oe7WqWXDDHH55PhSV+HLCuydN6asPPMoGF 193yc1ymtCiu1M3fplZ8DJW5Rc5CYf/jpobKqtt+ELwL4vTYnqVe4+w9s0731qFJFscT T/sZyiztdpUBJ0g4lrQ/rfNBVjcnid6xeqVnLEZkRRMh8ddWmCmbdb5HcDYeGFv/CsZA RDPy8aRvdvaCs2zELDvJSDyAM6I62+RX/6xL4kb7IUNq5BoHhoDjOC2BECRUtjxebSYG A+NA== X-Gm-Message-State: AOAM5314r1TCoZlpWpsiUjeQPuIlkd+2VH4kQM7nwE+OMnCwlCnLeLmA XEdhdlaXg+mD6jry6hCVYmo36EPiyVzCffTO X-Google-Smtp-Source: ABdhPJw6FWpxo/6jH48T1eLFb/AHnRzQdtWtMFKWSLjTdCHIz+dB9qsvl16J30G0Jf+Tr7VTmlwh+Q== X-Received: by 2002:aca:f410:: with SMTP id s16mr6109981oih.22.1627139034503; Sat, 24 Jul 2021 08:03:54 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id w190sm6704082oif.17.2021.07.24.08.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Jul 2021 08:03:54 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , "Jonathan A . Kollasch" , Marek Vasut , Heinrich Schuchardt , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Eugeniu Rosca , Roland Gaudig , Stefan Herbrechtsmeier , Sean Anderson , Simon Glass Subject: [PATCH v2 08/14] lib: Move common digit-parsing code into a function Date: Sat, 24 Jul 2021 09:03:35 -0600 Message-Id: <20210724150341.243074-9-sjg@chromium.org> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog In-Reply-To: <20210724150341.243074-1-sjg@chromium.org> References: <20210724150341.243074-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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 The code to convert a character into a digit is repeated twice in this file. Factor it out into a separate function. This also makes the code a little easier to read. Signed-off-by: Simon Glass --- (no changes since v1) lib/strto.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/strto.c b/lib/strto.c index ab6867a963f..c3f0f8e8c6c 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -30,16 +30,33 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) return s; } +/** + * decode_digit() - Decode a single character into its numeric digit value + * + * This ignore case + * + * @ch: Character to convert (expects '0'..'9', 'a'..'f' or 'A'..'F') + * @return value of digit (0..0xf) or 255 if the character is invalid + */ +static uint decode_digit(int ch) +{ + if (!isxdigit(ch)) + return 256; + + ch = tolower(ch); + + return ch <= '9' ? ch - '0' : ch - 'a' + 0xa; +} + ulong simple_strtoul(const char *cp, char **endp, uint base) { ulong result = 0; - ulong value; + uint value; cp = _parse_integer_fixup_radix(cp, &base); - while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp) - ? toupper(*cp) : *cp)-'A'+10) < base) { - result = result*base + value; + while (value = decode_digit(*cp), value < base) { + result = result * base + value; cp++; } @@ -136,12 +153,12 @@ unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base) unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) { - unsigned long long result = 0, value; + unsigned long long result = 0; + uint value; cp = _parse_integer_fixup_radix(cp, &base); - while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0' - : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) { + while (value = decode_digit(*cp), value < base) { result = result * base + value; cp++; } -- 2.32.0.432.gabb21c7263-goog