From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 497C63502B6 for ; Fri, 30 Jan 2026 08:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769760880; cv=none; b=Hkb6TlSwmhoyebmJQfdSb8Qq9CiEVd6l1I9ymYgGSDv3UsAeGaKxbcdK41neXlt64bo6GS1pBHwsAVQmxvABkPMXI/LrgW2CwMEn2+Ld75CVDrBwnRUWsvsTgvKIA9HxBAsid9DQM3E58da312QpHGxDWrHGMJbKkj2CFvbD6wc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769760880; c=relaxed/simple; bh=ufS97yRuKyzrXHxr+rThknIkodfEIda9O9yhU7NKBR4=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bYwaV27tcVSXjozJJbjP79goQrV5cparWnXsRBb6onC5JCgZQzne/nJFRRhxVkQAygTJnwQBrRh/h7oaGeu1hEIbI+78wI5mVVNYbJbhM6MEFM+0tMsHRaSaDyErVxY0ZR4e9T9ZgKTC0yIxIhn7axofBGxK9916yN1WPPF8nCQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZyZw/f7D; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZyZw/f7D" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-47ee76e8656so23877545e9.0 for ; Fri, 30 Jan 2026 00:14:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769760877; x=1770365677; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=B3+9Kg6y+p19Zo62sboS9rhiYnK17Xjo9c+6/wq2i0c=; b=ZyZw/f7DPhG36LM1QkP/B93SFfzk0e8L03rUbTbkMzr4HCrq9qrKSrbpdNEBjQWvfh lfxUYmUDn3q7UQv00aoLLb2PdxlMV+woMFOYTZ+0Y6VW9bWnDa+8jh20lXGc1nDrWc8r InC8bRJ2m2b95U9GMVQflgYQw2qvLXpo4Xq9TvQeEbZrsQI3LOajEt0SjT9U9xAuWwSv sfsdkBNE+FW28FPrO4CIM7cPCSKz8tmdRek7SpajsNAKo/8uxynjYUmp3oOwJxT/13jd EgGwTRwWNxop49jeW5SNDbB7KIQnTWmxAtr2xgFJu3Oz7qg28CDAcmwnijuA3ZtZaWNc JoYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769760877; x=1770365677; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=B3+9Kg6y+p19Zo62sboS9rhiYnK17Xjo9c+6/wq2i0c=; b=rx9V6EJ9OshelLgmVMAOimNKka63k0q1uTg+AWZZlMcirk9Sc2+kZIIGodDRzWuaxh fi6IU/KjRSfOFrEd6VRgIXS9pyg0LaVE2AmpNcOLiYGKNpBLNNYDuLOAystuoHFePIWX R1zQuJGm9Bg9mqAmUSd0+DyI1C6l75N7OjLZmRS7EJUZW5T4f3SRTjQgbjili3/8F7nd T8LO7fqOEMxO0b8oMHuDcZjNS989tujjwEV2qtghGuyuQ7Kkr3X9KO40F/yA5xSvotng DqChfGj5NcNBEvpgWJTRIa4V12K5VpBNdvatRSPz0z0frjDNpQRfqIYjFRCt2XbIx9xi FKEg== X-Forwarded-Encrypted: i=1; AJvYcCWZDDT1/kudNxPbIGoH43e8tRQAPu8oTVUhAwF7IVfgl10DYaoGci7kA1oZskCBhkMwAEHvYiVpzdBLklw=@vger.kernel.org X-Gm-Message-State: AOJu0YygYV6qdwMYA6LfPULntnq/MgLacX5OIeuGFyjv+0KyWCWpv5IM 2Mp5I53YeBYbZGQExLADDcWDRj7Fi6G1o7/fC45Q/FaDOiOUReWknM4B X-Gm-Gg: AZuq6aL/TYwVSNZ6XdM8dov9pdWOXs41sBtLmBo81/qaiMvV6YoHgSk91HBzbfmAzKV v34q7dJ+0oJ4BtKyjbGJA0UPJL+Aa3MHEysjWUqNhnqZOVlEAYxXoMGDc7Bf8V2SkStyfQQMC8+ ANlai14HamwVGWDiliVodWF/8eDxu/+y5Z+egqarv1AiRcnTYATYJ92m8T2lU2YoEjPedv+uIjG a0CBS5gMd11iZ+8Xrst949Hqdp66eOy9o9AXsbooP9+EXklwJuRPwJ4T2rQ/1IgYQdXTDfmSmup DKLjSi1NQ1SWoDysWWhwqt+CKXQMzcPm+rbVZ/l9PpqLonRWYc6ZU1ICx1gb9gu9EdNJodKjrcA gYNwKifnxoAuGtZbS/JQWiLAemsBSaKjzowEYAe6Z6mOcU8DR8cnjKTsjeKGnWD8aYTNH1GwFhC pLqxQaV4aZ9kTDlMTLj36gF8r1YqRmF6eyCjG8pjA1U3M7qbsYYTo5 X-Received: by 2002:a05:600c:a08c:b0:477:6374:6347 with SMTP id 5b1f17b1804b1-482db491ef7mr24268485e9.22.1769760877156; Fri, 30 Jan 2026 00:14:37 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-435e132368csm18614543f8f.31.2026.01.30.00.14.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 00:14:36 -0800 (PST) Date: Fri, 30 Jan 2026 08:14:35 +0000 From: David Laight To: "licheng.li" Cc: Willy Tarreau , Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] tools/nolibc: support left alignment (-) in printf Message-ID: <20260130081435.632f5fe9@pumpkin> In-Reply-To: <20260130071847.28715-2-im.lechain@gmail.com> References: <20260130071847.28715-1-im.lechain@gmail.com> <20260130071847.28715-2-im.lechain@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 30 Jan 2026 15:18:46 +0800 "licheng.li" wrote: > From: Cheng Li > > Currently, __nolibc_printf() in nolibc parses the width field but always > pads with spaces on the left. It ignores the '-' flag (left alignment). > > This patch implements support for the '-' flag > to forces left alignment by padding spaces on the right. > > The implementation reuses the padding character logic to handle both > cases. You don't have two cases any more. > Logic behavior: > - "%5d" -> " -5" (unchanged, right align) > - "%-5d" -> "-5 " (new, left align) > > Suggested-by: Willy Tarreau > Signed-off-by: Cheng Li > --- > v3 changes: > - Removed pad zeros support because of bug for signed number and pointer > v2 changes: > - Adopted optimization suggestions from Willy Tarreau: > - Incremented 'written' counter at the start of loops. > - Reordered loop checks to optimize compiler register usage. > - Updated commit message to explicitly mention zero-padding ('0') support. > --- > tools/include/nolibc/stdio.h | 19 ++++++++++++++++--- > 1 file changed, 16 insertions(+), 3 deletions(-) > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > index 1f16dab2ac88..f31b77f61d3b 100644 > --- a/tools/include/nolibc/stdio.h > +++ b/tools/include/nolibc/stdio.h > @@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size); > static __attribute__((unused, format(printf, 4, 0))) > int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args) > { > - char escape, lpref, c; > + char escape, lpref, padc, c; > unsigned long long v; > unsigned int written, width; > size_t len, ofs, w; > @@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char > while (1) { > c = fmt[ofs++]; > width = 0; > + padc = ' '; > > if (escape) { > /* we're in an escape sequence, ofs == 1 */ > escape = 0; > > + if (c == '-') { > + padc = c; > + c = fmt[ofs++]; > + } You only need a 'left align' flag. > + > /* width */ > while (c >= '0' && c <= '9') { > width *= 10; > @@ -358,13 +364,20 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char > if (n) { > w = len < n ? len : n; > n -= w; > - while (width-- > w) { > + while (width > w && padc != '-') { > + written += 1; > if (cb(state, " ", 1) != 0) > return -1; > - written += 1; > + width--; > } > if (cb(state, outstr, w) != 0) > return -1; > + while (width > w) { > + written += 1; > + if (cb(state, " ", 1) != 0) > + return -1; > + width--; > + } I'd swap the code so the short bit is replicated - should be smaller: if (padc == '-' && cb(state, outstr, w) != 0) return -1; while (width > w) { written += 1; if (cb(state, " ", 1) != 0) return -1; width--; } if (padc != '-' && cb(state, outstr, w) != 0) return -1; There is also a slight inconsistency between 'width--' and 'written += 1'. I'd use the postfix operator for both, but that is a matter of taste. David > } > > written += len;