9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions
@ 2021-08-01 20:10 kemal
  2021-08-02 17:56 ` cinap_lenrek
  0 siblings, 1 reply; 3+ messages in thread
From: kemal @ 2021-08-01 20:10 UTC (permalink / raw)
  To: 9front

[-- 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;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions
  2021-08-01 20:10 [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions kemal
@ 2021-08-02 17:56 ` cinap_lenrek
  2021-08-02 18:34   ` cinap_lenrek
  0 siblings, 1 reply; 3+ messages in thread
From: cinap_lenrek @ 2021-08-02 17:56 UTC (permalink / raw)
  To: 9front

applied.

tho i'm sure we can throw alot of the code away and just
do calls to WideCharToMultiByte()/MultiByteToWideChar()
from kernel32.h

--
cinap

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions
  2021-08-02 17:56 ` cinap_lenrek
@ 2021-08-02 18:34   ` cinap_lenrek
  0 siblings, 0 replies; 3+ messages in thread
From: cinap_lenrek @ 2021-08-02 18:34 UTC (permalink / raw)
  To: 9front

ok, deleted r16.c, now using that approach...

--
cinap

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-02 18:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-01 20:10 [9front] (drawterm patch) gui-win32: add support for surrogate pairs in rune16 functions kemal
2021-08-02 17:56 ` cinap_lenrek
2021-08-02 18:34   ` cinap_lenrek

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).