From nobody Sun Feb 8 08:22:40 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 64B6A329378 for ; Tue, 3 Feb 2026 10:30:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770114616; cv=none; b=r6e9IbMSDonCo5ReeLiK7B4T2rP7/mrT5dHYiRPENlI6EhDTJjkSEb5dFMW/TCKe7tEX+f4yy0y7npfdW7XxwumAzMuUn3TOIf3iu5rbLzNh3sFC4BEGbSqLSgihfND2W9/cMnn7wuYauxgEP6n7BfWCRPDjoY15M6HkFW6r4HE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770114616; c=relaxed/simple; bh=pY6QLuTb6W+U1hT+gHDfF907P5ccENx8g8w9kJbBkpA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RdYf654oTZCgFw6zXKYDvbCeYYMuHbS2tq14qJ2TWAD1n6NMznbDdFi6oWVA5pUjsCDfjBiwloUkw7GKjDd/pu2axTTyL51vj+pOJSp9yV1GWZnBuRrK8WlM3R8UfubU+3K6S5oeewmaWlz4S08W4a6GUT8AWUe6yt7VyEvquGY= 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=YA1aMHZa; arc=none smtp.client-ip=209.85.128.51 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="YA1aMHZa" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4801bc32725so41733345e9.0 for ; Tue, 03 Feb 2026 02:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770114613; x=1770719413; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7iDeq5WaIax/ojpbnczyOMmbdNehWZNWQp2thw5Kwn4=; b=YA1aMHZan2LBaDtohx6WBk4v6me6aXMSqSX2Af2VG1HrMhkEfc6m0zE0Ntf9lU0Hdc szVHnw/05+M4qhXPDU7VMVVwPBp6mZo+zckzIJgLU1q1pbhOfpcgxgcsQdIGxBSFY2fG KC5ciFMnQtdkH+7v0xhGMwOX8vYHs7NI3NFjAKzjCtuHn8pkzxk6hlBEo1Rk5/n4RHwa twZrywpbNb9OXooJTLrMdT1rYhMywBqOSMvxIZtWF0vBOfjfJYO35a/jANAzhHSQs9GH qYujrZWsE/9f3q8EgiTiOzfIa2fhDMmhlnJV6gJ2Dw97nRb9e+WSKwDQEcpxtO1o5CJL Xj+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770114613; x=1770719413; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=7iDeq5WaIax/ojpbnczyOMmbdNehWZNWQp2thw5Kwn4=; b=c1dkOAhQaivHxUiYVfG3RHVU6u1rl1VroP7JFJHbw4qh8dskLiIjMengm5d+hZHURK eOAxYfwEiTvd+66M4eoAuZp/BHIaNwvE9ZvYMlHFtZpV8Hgq/i7PhmrzTL3sWZiUP3Zs E96yV+Img5kWokPjounpIUJYqrtNqWDCeMNirmfFqP23BvXsY6v4Dd//Rc9pxNAcjdF/ kBnGZKkT5fysZtlcmiXDqCBCBcXjYJ6W00h/KctWWJ2dI6n6v8awyirMub7HFUTVVWC3 usFybonr8pRmryyKJuDHeUpOHkAKs+8Ad1L0fNv2LnKD53tKpKT3ngwnHHmFdMEdWSxH +N5A== X-Forwarded-Encrypted: i=1; AJvYcCUc22yMUf6TEbV6wCUTF8S+8YNfhmrl7DKVHBnxet1etQ2542rY2pzY3OSTwHL1CZSs1IWs9FQos7seTqA=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi6mVTMDo+lOacwrbm3XlxcVHFeNCtLv8Eou9UzxDNaFzN50Wy DTlkd7chWRfTzh942C7fOQSAj9/Wbch9PPPDTN7B/D8DUfs1m+b/+d6ojWDhVQ== X-Gm-Gg: AZuq6aIH56P+4j4c0ahMvAV4XPJm4cDV80Gj5h9HYrzJre/SUGn37a4F5Azf/rXuL1r b7utoAsIlJoLcnVOXxvsQUXkvmQqHBcS26sSmhXIwtf5GJo2ifZw6mCkZ2YAYhFOkRruiNl45vI LQFGmkPdZn+qVL5ufkd7JVDtSw5SIP3tSlAp65LT4jLrE4OiCr4jx3mTIU44Etqs01fpghgJpjl CcHvgOM1KdWwJjTLisrdsjloOS2NaS4LnwysYrodppYw1CLK/sAReOesEU/aT1nYBJ+RX3fEFfE dBjlJCknnSTE4fbcItxiEZvOHsACv/AF0zqV1HKXftXuPPeVpsn60BEpig7CILPJabr1D6xLdnq UrF6719oN3pR0FRcjY/wCfpXqoOMtb68iBll6d8W3ZqInPsuOA/Bz2LLo14hH10Kid4l3zZICpw M+jpWD12TvBZT4KcPmMEkRpYhjs7en2Okojv7cJDlyAlZy3CmjcHi6HkS/y6LQiAyEsMzRLdUB X-Received: by 2002:a05:600c:4684:b0:47e:e946:3a57 with SMTP id 5b1f17b1804b1-482db4b4f5dmr172450175e9.36.1770114612635; Tue, 03 Feb 2026 02:30:12 -0800 (PST) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-435e10edf62sm55940729f8f.13.2026.02.03.02.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Feb 2026 02:30:12 -0800 (PST) From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH next 05/12] tools/nolibc/printf: Simplify __nolibc_printf() Date: Tue, 3 Feb 2026 10:29:53 +0000 Message-Id: <20260203103000.20206-6-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260203103000.20206-1-david.laight.linux@gmail.com> References: <20260203103000.20206-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Move the check for the length modifiers into the format processing between the field width and conversion specifier. This lets the loop be simplified and a 'fast scan' for a format start used. If an error is detected (eg an invalid conversion specifier) then copy the invalid format to the output buffer. Reduces code size by about 10% on x86-64. Signed-off-by: David Laight --- tools/include/nolibc/stdio.h | 100 +++++++++++++++++------------------ 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 52cb516ea3db..164d2384978e 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -250,28 +250,52 @@ typedef int (*__nolibc_printf_cb)(void *state, const = char *buf, size_t size); static __attribute__((unused, format(printf, 3, 0))) int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, v= a_list args) { - char escape, lpref, c; + char lpref, c; unsigned long long v; unsigned int written, width; - size_t len, ofs; + size_t len; char tmpbuf[21]; const char *outstr; =20 - written =3D ofs =3D escape =3D lpref =3D 0; + written =3D 0; while (1) { - c =3D fmt[ofs++]; + outstr =3D fmt; + c =3D *fmt++; + if (!c) + break; + width =3D 0; + if (c !=3D '%') { + while (*fmt && *fmt !=3D '%') + fmt++; + len =3D fmt - outstr; + } else { + /* we're in a format sequence */ =20 - if (escape) { - /* we're in an escape sequence, ofs =3D=3D 1 */ - escape =3D 0; + c =3D *fmt++; =20 /* width */ while (c >=3D '0' && c <=3D '9') { width *=3D 10; width +=3D c - '0'; =20 - c =3D fmt[ofs++]; + c =3D *fmt++; + } + + /* Length modifiers */ + if (c =3D=3D 'l') { + lpref =3D 1; + c =3D *fmt++; + if (c =3D=3D 'l') { + lpref =3D 2; + c =3D *fmt++; + } + } else if (c =3D=3D 'j') { + /* intmax_t is long long */ + lpref =3D 2; + c =3D *fmt++; + } else { + lpref =3D 0; } =20 if (c =3D=3D 'c' || c =3D=3D 'd' || c =3D=3D 'u' || c =3D=3D 'x' || c = =3D=3D 'p') { @@ -327,54 +351,30 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *stat= e, const char *fmt, va_list #else outstr =3D strerror(errno); #endif /* NOLIBC_IGNORE_ERRNO */ - } - else if (c =3D=3D '%') { - /* queue it verbatim */ - continue; - } - else { - /* modifiers or final 0 */ - if (c =3D=3D 'l') { - /* long format prefix, maintain the escape */ - lpref++; - } else if (c =3D=3D 'j') { - lpref =3D 2; - } - escape =3D 1; - goto do_escape; + } else { + if (c !=3D '%') + /* Invalid format, output the format string */ + fmt =3D outstr + 1; + /* %% is documented as a 'conversion specifier'. + * Any flags, precision or length modifier are ignored. + */ + width =3D 0; + outstr =3D "%"; } len =3D strlen(outstr); - goto flush_str; } =20 - /* not an escape sequence */ - if (c =3D=3D 0 || c =3D=3D '%') { - /* flush pending data on escape or end */ - escape =3D 1; - lpref =3D 0; - outstr =3D fmt; - len =3D ofs - 1; - flush_str: - while (width > len) { - unsigned int pad_len =3D ((width - len - 1) & 15) + 1; - width -=3D pad_len; - written +=3D pad_len; - if (cb(state, " ", pad_len) !=3D 0) - return -1; - } - if (cb(state, outstr, len) !=3D 0) - return -1; + written +=3D len; =20 - written +=3D len; - do_escape: - if (c =3D=3D 0) - break; - fmt +=3D ofs; - ofs =3D 0; - continue; + while (width > len) { + unsigned int pad_len =3D ((width - len - 1) & 15) + 1; + width -=3D pad_len; + written +=3D pad_len; + if (cb(state, " ", pad_len) !=3D 0) + return -1; } - - /* literal char, just queue it */ + if (cb(state, outstr, len) !=3D 0) + return -1; } =20 if (cb(state, NULL, 0) !=3D 0) --=20 2.39.5