9front - general discussion about 9front
 help / color / mirror / Atom feed
From: kemal <kemalinanc8@gmail.com>
To: 9front@9front.org
Subject: [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions
Date: Sun, 1 Aug 2021 20:10:58 +0000	[thread overview]
Message-ID: <CABO6shewwnL=E3RCZ9=RQVO9d67V=o9Xn5Wie=sO4FLLaCYOOw@mail.gmail.com> (raw)

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

hello,

this patch adds support for surrogate pairs in rune16 functions.
this way unicode code points between U+10000 and U+10FFFF
can be correctly encoded and decoded.

i wrote this patch because runes like emojis gets corrupt
when written to the snarf buffer, due to clip(read|write) using rune16
functions and those functions not being capable of handling those
runes.

patch is attached.

(also fuck utf16)

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2209 bytes --]

From: kemal <kemali13@protonmail.com>
Date: Sun, 01 Aug 2021 19:36:34 +0000
Subject: [PATCH] gui-win32: add support for surrogate pairs in rune16 functions


this was required to encode and decode characthers between U+10000-U+10FFFF
properly.
---
diff c97fe4693f6112504d6f13fab46f7cc8b27685c1 23878a23b80c9004045aebe9ac7b71c26c60f550
--- a/gui-win32/r16.c	Mon Jun 28 22:29:39 2021
+++ b/gui-win32/r16.c	Sun Aug  1 22:36:34 2021
@@ -21,8 +21,9 @@
 	Maskx	= (1<<Bitx)-1,		/* 0011 1111 */
 	Testx	= Maskx ^ 0xFF,		/* 1100 0000 */
 
-	SurrogateMin	= 0xD800,
 	SurrogateMax	= 0xDFFF,
+	HiSurrogate		= 0xD800,
+	LoSurrogate		= 0xDC00,
 
 	Bad	= Runeerror,
 };
@@ -34,8 +35,8 @@
 	Rune16 *s;
 
 	n = runes16len(r) + 1;
-	s = malloc(n * sizeof(Rune16));
-	memmove(s, r, n * sizeof(Rune16));
+	s = calloc(n, sizeof(Rune16));
+	memcpy(s, r, n * sizeof(Rune16));
 	return s;
 }
 
@@ -59,17 +60,29 @@
 
 	op = p;
 	ep = p + nc;
-	while(c = *r++) {
+	while(c = *r++){
 		n = 1;
 		if(c >= Runeself)
 			n = runelen(c);
 		if(p + n >= ep)
 			break;
-		rc = c;
-		if(c < Runeself)
+		if(c < Runeself){
 			*p++ = c;
-		else
-			p += runetochar(p, &rc);
+			continue;
+		}
+		rc = c;
+		if(c >= LoSurrogate && c <= SurrogateMax)
+			rc = Bad;
+		else if(c >= HiSurrogate && c <= 0xDBFF){ /* decode a surrogate pair properly */
+			if(p + n+1 >= ep)
+				rc = Bad;
+			else if((c = *r) >= LoSurrogate && c <= SurrogateMax){
+				rc = 0x10000 | (*(r-1) - HiSurrogate) << 10 | (c - LoSurrogate);
+				r++;
+			}else
+				rc = Bad;
+		}
+		p += runetochar(p, &rc);
 	}
 	*p = '\0';
 	return op;
@@ -107,7 +120,12 @@
 	er = r + nc;
 	while(*p != '\0' && r + 1 < er){
 		p += chartorune(&rc, p);
-		*r++ = rc;	/* we'll ignore surrogate pairs */
+		if(rc >= 0x10000){ /* got to encode it in a surrogate pair */
+			rc -= 0x10000;
+			*r++ = (rc >> 10)+HiSurrogate;
+			*r++ = (rc & 0x3FF)+LoSurrogate;
+		}else
+			*r++ = rc;
 	}
 	*r = '\0';
 	return or;
@@ -138,7 +156,7 @@
 	wchar_t *ws;
 
 	n = utflen(s) + 1;
-	ws = malloc(n*sizeof(wchar_t));
+	ws = calloc(n, sizeof(wchar_t));
 	utftorunes16(ws, s, n);
 	return ws;
 }
@@ -162,7 +180,7 @@
 {
 	int n = 0;
 
-	while (*ws)
+	while(*ws)
 		n += runelen(*ws++);
 	return n+1;
 }

             reply	other threads:[~2021-08-02  9:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-01 20:10 kemal [this message]
2021-08-02 17:56 ` cinap_lenrek
2021-08-02 18:34   ` cinap_lenrek

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='CABO6shewwnL=E3RCZ9=RQVO9d67V=o9Xn5Wie=sO4FLLaCYOOw@mail.gmail.com' \
    --to=kemalinanc8@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).