From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id D437B3870C18 for ; Tue, 5 Dec 2023 15:56:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D437B3870C18 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D437B3870C18 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::52a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701791781; cv=none; b=X6SwsDy0ihJO5BMCeiM0GI/wVEch/lGNuj2PqLomV1QPdOLWRWy4OkAV4PpHRR4lH0rzYil278pqs2SEHJ/c5VsHT71dUdKOznEmLH7yfKfwhFCO2nDWLCBHFvfcqZ01HUhMGotXZI/+0+Odkx44TwfR+1afU8VIqsEPL6y8IIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701791781; c=relaxed/simple; bh=P7k6XisY63KsnfhPwn97ySVDC1uPT/4QPixEoodBDpw=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=nTMnKlZEgQWzNAFsLANjCJ3s4pMykYr6SUsdnbMw0mGwDyXVXzOOASildmRo9gVpPkbfUVDFDUcOZD3sOWfUe8jfKUwjvEJjo7NgiTBZnc7REKpkP+HUG8L3MYU2YN5IBL8KfpaQbY0YxLfZQqlFHbcp4h2/SQZuWjpQWCpBwDo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-54c5ed26cf6so5767029a12.3 for ; Tue, 05 Dec 2023 07:56:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701791778; x=1702396578; darn=sourceware.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=7GNuZvQ6zM01l6BAMljD94HGo+NTkrtnjiTaXhU6dqQ=; b=eVxAgCV2juEuy0i6JN7vc3HkFjJNEnbq2eX4aJSqIsR6zf9VpEiktIZXwCu4oiNYKt j2XNfVOGekJNQ27OLgvpRnJkzdEbrwydn46DYei4fWzJb7zUbtnjSGdzAlM4LxxkNvD5 1pKnO8u22qaCvgi2AtWCPPLecHYkGRhl7QVze8lWVK9RiL1WFHUfurjMCn5uf9Rv+cu9 iX2WfCUfj7dU97c70sJZ/Vv1QJdMGIRBROxqxla5Y+0/rQy4cRP9zqb3e5qZuAsZ8oe7 Bai/RcYAbf87OQPZFAI/JTwjIuNA5NqWJ/gcvJzGe0rK6c7qECGcd3GDISFtqasaeqy4 +Pug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701791778; x=1702396578; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7GNuZvQ6zM01l6BAMljD94HGo+NTkrtnjiTaXhU6dqQ=; b=jHZvRjLYrtzJfIS400cNq7DuIGeDkxeVEi9hK6w2OJ3fz6MF0d8voVVldZQFFn3eFS 62VEFzX+tNx2QbbRneqP4VbL6PWU+mzQxZkU1XQVyT4bjpV0h0OQvPovQ0V+bY2UVb0t 9fWNfEEk0ZqaNv1JD/iKxoymwMIz8zWTYTMJVy7hpYhhQRXN17kFgliZbay98C4+w+bt ETIFxL4Ab72313Fcqlom17KbDlojO7ms03M5xX7LTIYYJRUcVhIxKM9zbfTEZT3pwpW4 J48yFDKhkBoSWqfXOucUXgOZaqTfqaKbc62xXX5JNJH+oKTRTwhnltaxUarZbwGkEPwy 7LdQ== X-Gm-Message-State: AOJu0YyZ6PcNdOdr54A7/FXpLbl+enrCRb9nggLN1hhGrcxNGOP6tM83 9nRKEkNufEsElnbe67cKIWuQhvM/GME8SL4ONn3x0pWUe8XraeJm X-Google-Smtp-Source: AGHT+IFukM+FZ+TiDVnt/5tirziaz/pSUMuBabqg5M+mo6FMvlQO7qB0C8UEFgUZruoWEh4lM0qNzIMw2NvR+hDdeL0= X-Received: by 2002:a17:906:e099:b0:a18:f82c:65d2 with SMTP id gh25-20020a170906e09900b00a18f82c65d2mr708032ejb.34.1701791777870; Tue, 05 Dec 2023 07:56:17 -0800 (PST) MIME-Version: 1.0 References: <20231205145839.474295-1-tirtajames45@gmail.com> In-Reply-To: From: James Date: Tue, 5 Dec 2023 22:52:41 +0700 Message-ID: Subject: Re: [PATCH] strchr, strchrnul: implement strchr() as strchrnul() To: newlib@sourceware.org, James Tirta Halim Cc: "vinschen@redhat.com" Content-Type: multipart/alternative; boundary="00000000000066d5c5060bc546f5" X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --00000000000066d5c5060bc546f5 Content-Type: text/plain; charset="UTF-8" Currently, strchrnul() is implemented as strchr() and falls back to a strlen() to find the null-terminator if the character is not found, scanning the string twice. However, strchr() is going to scan the whole string anyway and discard the pointer to the null-terminator if the character is not found, returning NULL. Instead, we can just implement strchr() with discarding strchrnul()'s pointer to null-terminator returning NULL and by that avoid calling strlen() in strchrnul() if a character is not found. I made a typo in the strchr(), it should be: return s1 && *s1 ? (char *) s1 : NULL; which should be: return *s1 ? (char *) s1 : NULL; since strchrnul will never return NULL. We can avoid the strchrnul() function call in strchr() by implementing it in a separate header like str-two-way.h and including them in both files. The same could be done with the strlen() part in strchr() since the implementations look to be the same. --00000000000066d5c5060bc546f5--