9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Amavect <amavect@gmail.com>
To: 9front@9front.org
Subject: [9front] [PATCH] silence drawterm runestrchr while(assign) warning
Date: Sun, 22 May 2022 18:04:36 -0500	[thread overview]
Message-ID: <20220522180436.6c17d808@spruce.localdomain> (raw)

[-- Attachment #1: Type: text/plain, Size: 1359 bytes --]

Hi,

I want to shut up drawterm compile warnings without resorting to -Wno.
I'll rationalize this with reducing function sizes by bytes,
"improving readability", and micro optimizations for kencc.

Here are some basic transformations on strchr and runestrchr.
It reduces 9front function byte sizes on the arches I tested
(amd64 0x4f->0x43, arm 0x50->0x40, power 0x68->0x5c).
It shuts up a gcc warning about runestrchr while(c1 = *s++)
(when compiled without -Wno-parentheses)
Attached are patches for 9front and drawterm.
Below are the full functions for criticism.

Thanks,
Amavect


char*
strchr(char *s, int c)
{
	char r;

	if(c == 0)
		while(*s++)
			;
	else
		while((r = *s++) != c)
			if(r == 0)
				return 0;
	return s-1;
}


Rune*
runestrchr(Rune *s, Rune c)
{
	Rune r;

	if(c == 0)
		while(*s++)
			;
	else
		while((r = *s++) != c)
			if(r == 0)
				return 0;
	return s-1;
}


unit test:

#include <u.h>
#include <libc.h>

void
main(void)
{
	char *v = "foo bar ss";
	char *e;
	Rune *c = L"foo βαρ ß";
	Rune *t;

	e = strchr(v, L'z');
	assert(e == nil);
	e = strchr(v, L'a');
	assert(e == v+5);
	e = strchr(v, 0);
	assert(e == v+10);

	t = runestrchr(c, L'z');
	assert(t == nil);
	t = runestrchr(c, L'α');
	assert(t == c+5);
	t = runestrchr(c, 0);
	assert(t == c+9);
}

[-- Attachment #2.1: Type: text/plain, Size: 318 bytes --]

from postmaster@9front:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-patch
	Content-Transfer-Encoding: 7bit
	Content-Disposition: attachment; filename=strchr.9front.patch

[-- Attachment #2.2: strchr.9front.patch.suspect --]
[-- Type: application/octet-stream, Size: 1063 bytes --]

From: Amavect <amavect@gmail.com>
Date: Sun, 22 May 2022 22:38:11 +0000
Subject: [PATCH] shorten strchr and runestrchr

---
diff f87c5f7f43ce3ab4e160b9985b30eb548ee75646 345def163036bc7b6f9048d43aac1b7d0a3bac69
--- a/sys/src/libc/port/runestrchr.c	Fri May 20 01:58:13 2022
+++ b/sys/src/libc/port/runestrchr.c	Sun May 22 17:38:11 2022
@@ -4,17 +4,14 @@
 Rune*
 runestrchr(Rune *s, Rune c)
 {
-	Rune c0 = c;
-	Rune c1;
+	Rune r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }
--- a/sys/src/libc/port/strchr.c	Fri May 20 01:58:13 2022
+++ b/sys/src/libc/port/strchr.c	Sun May 22 17:38:11 2022
@@ -4,17 +4,14 @@
 char*
 strchr(char *s, int c)
 {
-	char c0 = c;
-	char c1;
+	char r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }


[-- Attachment #3.1: Type: text/plain, Size: 350 bytes --]

from postmaster@9front:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-patch
	Content-Transfer-Encoding: 7bit
	Content-Disposition: attachment;
 filename=0001-silence-runestrchr-while-assign-warning.patch

[-- Attachment #3.2: 0001-silence-runestrchr-while-assign-warning.patch.suspect --]
[-- Type: application/octet-stream, Size: 750 bytes --]

 From ff49e28ad04453061925882a1b2b7a1aec7d88eb Mon Sep 17 00:00:00 2001
From: Amavect <amavect@gmail.com>
Date: Sun, 22 May 2022 17:48:37 -0500
Subject: [PATCH] silence runestrchr while(assign) warning

---
 libc/runestrchr.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/libc/runestrchr.c b/libc/runestrchr.c
index af7fc4e..e4c757d 100644
--- a/libc/runestrchr.c
+++ b/libc/runestrchr.c
@@ -4,17 +4,14 @@
 Rune*
 runestrchr(Rune *s, Rune c)
 {
-	Rune c0 = c;
-	Rune c1;
+	Rune r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }
-- 
2.36.0



             reply	other threads:[~2022-05-22 23:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-22 23:04 Amavect [this message]
2022-05-30  1:02 ` [9front] " Amavect
2022-05-30  5:15 ` [9front] " ori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220522180436.6c17d808@spruce.localdomain \
    --to=amavect@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).