From mboxrd@z Thu Jan 1 00:00:00 1970 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 4DC4236EA8F for ; Sun, 8 Mar 2026 11:37:56 +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=1772969877; cv=none; b=p3DMZeogMPGTmNk68a1zOz/McyXRyqyrJtOh5t65Tc6hPdO1rOq30R7INysiFaiJBJrM0oKJ7CiNM5w3xiPmLI2PREEVAqe4hWYk/zjnDVAzR0FrUzSRJwDK2uUEn4IrMCDjTLNwNNAHQbzYlQQHGLuD/GiHLruj7Yko0xJluNA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772969877; c=relaxed/simple; bh=q3W7SSSz1AN1X8znuW2LGwr0PxR1H+nRFpftRLklxCU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E5pM/wX6rnBqr99WwhWWcd3xpsTQKGvSbzR8x0ObHHjf28YOjwoUEjpZjYfksEouZh4ylGBermMU8ocNr9MbiSVK6gBXEZoY9DPPX37ZFNmK79vHjHOPpcGlVGIgQniX9tQZcbwfDscjlpso8+CLTrCu5SjJuuJGwJ7I9pqdBzc= 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=QclnimMc; 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="QclnimMc" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4853510b4f3so6185055e9.0 for ; Sun, 08 Mar 2026 04:37:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772969875; x=1773574675; 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=7/3sJaQW9mdlB3Ci3VcFKBs5+56i7sSWzFPv2EDoFbw=; b=QclnimMcAvwIcbvf6iuHe6Ib/JfE13ZLS5VtoKzUJD7bt2f3q3JrEpd1bGuWv7OZdT /MqXc62zZ9nKn6asND4Qids02xpTmVaPNehu8iw/FhPu3rZSm1RfLDg3XSq8YWsVIP28 CT+0n2DpzQWQQTIZWU25FkK5+dLUYVdi3ONEfSmj0XEc+xcR721pgMCwc61uC9CCInMf I/aG47WDhrkTD8gU27LChErIR+u3741V7sq0jgxWAnQ01B9x29iZ1kUMDflZq1uhgUpk K/7tlNTk+eJaSoEpwncGPvhpwk0Wrytu9xnZgorYB93GEqQ1sveDpNCR4C70ylmnwANH F7XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772969875; x=1773574675; 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=7/3sJaQW9mdlB3Ci3VcFKBs5+56i7sSWzFPv2EDoFbw=; b=vGEjc6exm8Icw6CWHPd7fH+k/9gixWlgNrQLH5whIZ92q6rs9iJNKhSuUx3NDNYS1U /WKJoCTil6ZZqOqbgLIWjZ0J27h3zCI7t9K0hFLbktPfM6X3/hNJKUVcnuXdV0BBgUn+ RFHC/XEbtZQUJoMdVZd83Ri8kJEIWUg1/BaoSyOpFL/qJZYiWPZjrnWhDypn4Tqjy9Zv uwmCMXXDh0+7e1gmSUvn2xauiM/1z5jtJTw+Mw66waxQOWkG8XaIsNF1XYQCBMHOahrN z6cMW/fYDxjjA5aMiZj1akCZCUzWI+lvQFShAk8H1Hcz7SG34SZX4ozoMp9AVZ8uzqvb /ugQ== X-Forwarded-Encrypted: i=1; AJvYcCXaEjpw5tWn0Y4yLVxRPUzDPBg7znWdiAP0i8XtckPQtmxCoBBqsZq69fCftvGy3Gc7cK1ZOI9ZJx5+3u8=@vger.kernel.org X-Gm-Message-State: AOJu0YzW12fCMRawMPp0ZEuNBdXk9eIV5yt35YnRRxVTZB/jcOTzRwgF QAGWRoB7+NegAuHpYxFaT//o0WoE+tcew3pO8c9uls2A236RH7da8Pr1 X-Gm-Gg: ATEYQzw8QK/dWK/0DX7DoCfn5sQoD21gcmGaigRrqov8x1D1JkVBJrvNnowUCmGsKTa tfHkLBJZiOKn2qQXQaTwsPmrH8JwTlPgQs03+x8wy+5MaKFydHVKmrCYlA+Of3XrkuCxTtgf98E rbxRb/zpZjkFrSTBAlrxk2inbOyrfTCBiFhf1Lgic8gc+8Wvbyzna4VOXZetga0Wclk7DbhXmkA xNm4cFiKp0roiMIE8M0VLTVsJdwRq8TqpN6Ac0YoQ8QgtekWa7ydGNEGmJrkK8mY4mOZQaY2pdc KTjJBqzHVxHobV8nyNc7aH24dmWPOUwoTlVpDAfD226VLXmtgcDq6YqMppborbV8KtBmWpPaX4j qK9R1G3eSAiEJv7t1YZRMVQdbOfaIr93kdGwfctRa7+G7r/Wk0LZnGQurqXUlZN6cAN4cX0raJo 4kyTJ4kw8la5uEVGJXRbyJ4iuqn73g7BXEwkb6UzKnvP3Yfwd8LZr+myz1LNy+9TO0N+x8WeghL SAurmkYccsk X-Received: by 2002:a05:600c:c4a3:b0:483:9139:4c1d with SMTP id 5b1f17b1804b1-4852692c943mr137749125e9.14.1772969874423; Sun, 08 Mar 2026 04:37:54 -0700 (PDT) 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 5b1f17b1804b1-485237dd017sm63621855e9.2.2026.03.08.04.37.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2026 04:37:54 -0700 (PDT) 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 v5 next 07/17] tools/nolibc/printf: Use goto and reduce indentation Date: Sun, 8 Mar 2026 11:37:32 +0000 Message-Id: <20260308113742.12649-8-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260308113742.12649-1-david.laight.linux@gmail.com> References: <20260308113742.12649-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: 8bit From: David Laight Upcoming changes will need to use goto to jump to the code that outputs characters. Use 'goto do_output' to output a known number of characters. Use 'goto do_strlen_output' to output a '\0' terminated string. Removes a level of indentation from the format processing code. The change is best reviewed using 'git diff -b' after applying it. Acked-by: Willy Tarreau Signed-off-by: David Laight --- Unchanged for v5. For v4: - Output a single '%' from the format string. New patch for v3. Makes the final code look better and there is less to change if done early. tools/include/nolibc/stdio.h | 170 +++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 78 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index b3cfed162eb6..710a4bce5e81 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -329,103 +329,117 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list fmt++; /* Output characters from the format string. */ len = fmt - outstr; - } else { - /* we're in a format sequence */ + goto do_output; + } - ch = *fmt++; + /* we're in a format sequence */ - /* width */ - while (ch >= '0' && ch <= '9') { - width *= 10; - width += ch - '0'; + ch = *fmt++; - ch = *fmt++; - } + /* width */ + while (ch >= '0' && ch <= '9') { + width *= 10; + width += ch - '0'; + + ch = *fmt++; + } - /* Length modifiers */ + /* Length modifiers */ + if (ch == 'l') { + lpref = 1; + ch = *fmt++; if (ch == 'l') { - lpref = 1; - ch = *fmt++; - if (ch == 'l') { - lpref = 2; - ch = *fmt++; - } - } else if (ch == 'j') { - /* intmax_t is long long */ lpref = 2; ch = *fmt++; - } else { - lpref = 0; } + } else if (ch == 'j') { + /* intmax_t is long long */ + lpref = 2; + ch = *fmt++; + } else { + lpref = 0; + } - if (ch == 'c' || ch == 'd' || ch == 'u' || ch == 'x' || ch == 'p') { - char *out = outbuf; + if (ch == 'c' || ch == 'd' || ch == 'u' || ch == 'x' || ch == 'p') { + char *out = outbuf; - if (ch == 'p') + if (ch == 'p') + v = va_arg(args, unsigned long); + else if (lpref) { + if (lpref > 1) + v = va_arg(args, unsigned long long); + else v = va_arg(args, unsigned long); - else if (lpref) { - if (lpref > 1) - v = va_arg(args, unsigned long long); - else - v = va_arg(args, unsigned long); - } else - v = va_arg(args, unsigned int); - - if (ch == 'd') { - /* sign-extend the value */ - if (lpref == 0) - v = (long long)(int)v; - else if (lpref == 1) - v = (long long)(long)v; - } + } else + v = va_arg(args, unsigned int); - switch (ch) { - case 'c': - out[0] = v; - out[1] = 0; - break; - case 'd': - i64toa_r(v, out); - break; - case 'u': - u64toa_r(v, out); - break; - case 'p': - *(out++) = '0'; - *(out++) = 'x'; - __nolibc_fallthrough; - default: /* 'x' and 'p' above */ - u64toh_r(v, out); - break; - } - outstr = outbuf; + if (ch == 'd') { + /* sign-extend the value */ + if (lpref == 0) + v = (long long)(int)v; + else if (lpref == 1) + v = (long long)(long)v; } - else if (ch == 's') { - outstr = va_arg(args, char *); - if (!outstr) - outstr="(null)"; + + switch (ch) { + case 'c': + out[0] = v; + out[1] = 0; + break; + case 'd': + i64toa_r(v, out); + break; + case 'u': + u64toa_r(v, out); + break; + case 'p': + *(out++) = '0'; + *(out++) = 'x'; + __nolibc_fallthrough; + default: /* 'x' and 'p' above */ + u64toh_r(v, out); + break; } - else if (ch == 'm') { + outstr = outbuf; + goto do_strlen_output; + } + + if (ch == 's') { + outstr = va_arg(args, char *); + if (!outstr) + outstr="(null)"; + goto do_strlen_output; + } + + if (ch == 'm') { #ifdef NOLIBC_IGNORE_ERRNO - outstr = "unknown error"; + outstr = "unknown error"; #else - outstr = strerror(errno); + outstr = strerror(errno); #endif /* NOLIBC_IGNORE_ERRNO */ - } else { - if (ch != '%') { - /* Invalid format: back up to output the format characters */ - fmt = outstr + 1; - /* and output a '%' now. */ - } - /* %% is documented as a 'conversion specifier'. - * Any flags, precision or length modifier are ignored. - */ - width = 0; - outstr = "%"; - } - len = strlen(outstr); + goto do_strlen_output; } + if (ch != '%') { + /* Invalid format: back up to output the format characters */ + fmt = outstr + 1; + /* and output a '%' now. */ + } + /* %% is documented as a 'conversion specifier'. + * Any flags, precision or length modifier are ignored. + */ + len = 1; + width = 0; + outstr = fmt - 1; + goto do_output; + +do_strlen_output: + /* Open coded strlen() (slightly smaller). */ + for (len = 0;; len++) + if (!outstr[len]) + break; + +do_output: written += len; width -= len; -- 2.39.5