From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 9084F2DF3CC for ; Fri, 30 Jan 2026 15:00:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769785244; cv=none; b=WW8WJ71z0qZT/YFnDUn22oNo3D3Z5Bof6wvakShrU329RTEGp9Mb7bFuzhsqMfpq7GdpSE0cVtFDwlq4u/qmeRBcpvXO9eWCJKF3JobkhG3NWM9b+WJktNRDFGnWyEHhzFLZKDVmmzZqvuDDOfFItsh0GU/STdQyRFTtRbpQHdo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769785244; c=relaxed/simple; bh=vNofPKAdFI5ZmN6TEWMJHAj42TdARZ9eHvZqduAddI4=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RExZToQBnFgfuR2fTLyJUgOg+oh8ivUTSP0HsgWGaG+eC9KHTyTrbDwAGGqK3/LZJy4RMo14TGyowm8R3te/fmvM/B/4a83yBIO052YF/oxiEqovJFjdGEZoRA7pFz/AcklzKGLKsuGt+E52FZqH9Dsoan2z8FeGPzbtfDGbxRM= 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=nca/+UXQ; arc=none smtp.client-ip=209.85.128.53 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="nca/+UXQ" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-47ee07570deso18946425e9.1 for ; Fri, 30 Jan 2026 07:00:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769785241; x=1770390041; 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=C5mTv0+k/zjcfw/xGG2YeMVFv1tj6sVsypQRgpk4lsM=; b=nca/+UXQIqiALLm9iFFEyjrxWn03e4y9EJEOKQroMLVRtQ7+09QvQcKmAcQswjKovI RK32gqHrInTSJnkTqtCODDqOAp02LIwvM5LTQwRV2qFO/c+EB8c7QOVxak2Oq1RO13e+ LYUYlghIEmO6z4E2WtPHa5boKA1J9swJ7FaFkyhi3OzU4WftlJXJHzpzwZB8KIkJxiXm uMOVtL/XqOQGxdi8jIo5a4puhzFlpInLLCHaZT8xkr17xNCLSXSXeRN8oyUsj2tEaGIl 2V478Z3O10Rs26nj9ZpOimUePNbu/HHsTU6KodvBgV6AFlq618HUiGamYy1lPc1HRx/X 4/mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769785241; x=1770390041; 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=C5mTv0+k/zjcfw/xGG2YeMVFv1tj6sVsypQRgpk4lsM=; b=dN0iJcDXzyE2vSrLBmIyl5f3zyHWNcIveh6ujfevGCkkpSPSWl8ypupKsO3BOM26KU qTj2Qy6U4FKRegDLJ7OTbxBPQT3qYrYAkYuGafSIKAFmBnPL7tGgZD/7VLuSWlZDvYaC YPnDdIaCapbOJxJJCbPaH0m/FQ65fefeko6W+AmKXgmcIz2bMigJylfQdLbCmdV99hpD Jgvd9fxSTlfknMMCw7/XGFYcl0iKz2JZ171aC6r9zPuqgyk+0RYX0GkKXq+81D7jyLeK 9C/fdl5m4FA7Y4jkCzZLJe+ASr1pD2ZjXg3Pyoh2skfUBy8kclYaNF+5+67olv7L/CMx x/zA== X-Forwarded-Encrypted: i=1; AJvYcCXyOu9ZIuC3T4RaeE48sRq1eHwnrUTeGL3GNPmDgglF37NPqvclCpHWW3rlsd8COtu/5hAZiQPXFp9ZaLk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw3aVZO1NOsjCg6U2S1MkU6q0uNmsglAovqnULmLQvvLeNZsITR BCGBQg+LKa/jrHZC2r50t2GQnWDAn+qWTtdYdkfse8n+fGtUaI7c5pBjhQivQg== X-Gm-Gg: AZuq6aI1C5fWl2C3WGLktczlUGYF55mpsHP58nyRudhGRrsnstgSHbf9QDR/8J5morG UXfg9rYU9G7rr+VQfP/qOg2JGRAU53XmuUabV93OdG8eZLNtbrOsVdo8dyCrJM3OqEZWk2b51pV HEcwd9UBUjGTyVRjdD86+ZBcxKoi5aSK++0gfZuPCERfw8ID4rAO2UEMdelnsGG54BVeraiZKn8 j0tMPSbwbTXnlNSTzNk0MYDNsRAigPL6uLZZfY6aMjwFzABQUyFKKTPFuQXGZ0GLhRpHzNtTqRJ dd94qQFBGrkimanxJEyibBc37tj8gyq5Phov88HLhdvJZfHJ2APRUkO75SRBZrD8QgiNCGJIc5e mDnomGd+nGHGNkQN1bTBwkrxbA5ge4oux1zQ4mESo/WDq6Ui82JNP2nZEnfUnkvfRS0lKV1PIuy EFuG4KqbV+7h2QqIxGuRf7DoSvTgGCb8PlsVItK73MMMFOL2pDb+nU X-Received: by 2002:a05:600c:1d03:b0:477:63a4:88fe with SMTP id 5b1f17b1804b1-482db4521e1mr41110675e9.2.1769785240408; Fri, 30 Jan 2026 07:00:40 -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 5b1f17b1804b1-482da903a30sm28546265e9.1.2026.01.30.07.00.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 07:00:40 -0800 (PST) Date: Fri, 30 Jan 2026 15:00:39 +0000 From: David Laight To: "licheng.li" Cc: Willy Tarreau , Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/2] tools/nolibc: support left alignment (-) in printf Message-ID: <20260130150039.33fb29ba@pumpkin> In-Reply-To: <20260130102218.1324-2-im.lechain@gmail.com> References: <20260130102218.1324-1-im.lechain@gmail.com> <20260130102218.1324-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 18:22:17 +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. > > Logic behavior: > - "%5d" -> " -5" (unchanged, right align) > - "%-5d" -> "-5 " (new, left align) > > Suggested-by: David Laight > Suggested-by: Willy Tarreau > Signed-off-by: Cheng Li Looks reasonable. Reviewed-by: David Laight David > --- > v4 changes: > - Adopted optimization suggestions from David Laight > 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 | 17 +++++++++++++---- > 1 file changed, 13 insertions(+), 4 deletions(-) > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > index 1f16dab2ac88..1197928a1364 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++]; > + } > + > /* width */ > while (c >= '0' && c <= '9') { > width *= 10; > @@ -358,12 +364,15 @@ 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) { > + if (padc == '-' && cb(state, outstr, w) != 0) > + return -1; > + while (width > w) { > + written++; > if (cb(state, " ", 1) != 0) > return -1; > - written += 1; > + width--; > } > - if (cb(state, outstr, w) != 0) > + if (padc != '-' && cb(state, outstr, w) != 0) > return -1; > } >