History

gui-win32: delete utf-16 routines, just use WideCharToMultiByte()/MultiByteToWideChar() kernel32 functions

cinap_lenrek committed 5 years ago

Commit a130d441 parent dbe5ea14 files · +7 −206Browse files

Unified · Split

4 files
  1. gui-win32/Makefile +0 −1
  2. gui-win32/r16.c +0 −186
  3. gui-win32/r16.h +0 −11
  4. gui-win32/screen.c +7 −8
gui-win32/Makefile+0−1
@@ -4,7 +4,6 @@ LIB=libgui.a · 3 unmodified lines
44
55 OFILES=\
66 screen.$O\
7− r16.$O
87
98 default: $(LIB)
109 $(LIB): $(OFILES)
gui-win32/r16.c+0−186
@@ -1,186 +0,0 @@
1−#define _WIN32_WINNT 0x0500
2−#include <windows.h>
3−
4−#include "u.h"
5−#include "lib.h"
6−#include "r16.h"
7−
8−#define Bit(i) (7-(i))
9−/* N 0's preceded by i 1's, T(Bit(2)) is 1100 0000 */
10−#define T(i) (((1 << (Bit(i)+1))-1) ^ 0xFF)
11−/* 0000 0000 0000 0111 1111 1111 */
12−#define RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1)
13−
14−enum
15−{
16− Bitx = Bit(1),
17−
18− Tx = T(1), /* 1000 0000 */
19− Rune1 = (1<<(Bit(0)+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */
20−
21− Maskx = (1<<Bitx)-1, /* 0011 1111 */
22− Testx = Maskx ^ 0xFF, /* 1100 0000 */
23−
24− SurrogateMax = 0xDFFF,
25− HiSurrogate = 0xD800,
26− LoSurrogate = 0xDC00,
27−
28− Bad = Runeerror,
29−};
30−
31−Rune16*
32−runes16dup(Rune16 *r)
33−{
34− int n;
35− Rune16 *s;
36−
37− n = runes16len(r) + 1;
38− s = calloc(n, sizeof(Rune16));
39− memcpy(s, r, n * sizeof(Rune16));
40− return s;
41−}
42−
43−int
44−runes16len(Rune16 *r)
45−{
46− int n;
47−
48− n = 0;
49− while(*r++ != 0)
50− n++;
51− return n;
52−}
53−
54−char*
55−runes16toutf(char *p, Rune16 *r, int nc)
56−{
57− char *op, *ep;
58− int n, c;
59− Rune rc;
60−
61− op = p;
62− ep = p + nc;
63− while(c = *r++){
64− n = 1;
65− if(c >= Runeself)
66− n = runelen(c);
67− if(p + n >= ep)
68− break;
69− if(c < Runeself){
70− *p++ = c;
71− continue;
72− }
73− rc = c;
74− if(c >= LoSurrogate && c <= SurrogateMax)
75− rc = Bad;
76− else if(c >= HiSurrogate && c <= 0xDBFF){ /* decode a surrogate pair properly */
77− if(p + n+1 >= ep)
78− rc = Bad;
79− else if((c = *r) >= LoSurrogate && c <= SurrogateMax){
80− rc = 0x10000 | (*(r-1) - HiSurrogate) << 10 | (c - LoSurrogate);
81− r++;
82− }else
83− rc = Bad;
84− }
85− p += runetochar(p, &rc);
86− }
87− *p = '\0';
88− return op;
89−}
90−
91−int
92−rune16nlen(Rune16 *r, int nrune)
93−{
94− int nb, i;
95− Rune c;
96−
97− nb = 0;
98− while(nrune--) {
99− c = *r++;
100− if(c <= Rune1){
101− nb++;
102− } else {
103− for(i = 2; i < UTFmax + 1; i++)
104− if(c <= RuneX(i) || i == UTFmax){
105− nb += i;
106− break;
107− }
108− }
109− }
110− return nb;
111−}
112−
113−Rune16*
114−utftorunes16(Rune16 *r, char *p, int nc)
115−{
116− Rune16 *or, *er;
117− Rune rc;
118−
119− or = r;
120− er = r + nc;
121− while(*p != '\0' && r + 1 < er){
122− p += chartorune(&rc, p);
123− if(rc >= 0x10000){ /* got to encode it in a surrogate pair */
124− rc -= 0x10000;
125− *r++ = (rc >> 10)+HiSurrogate;
126− *r++ = (rc & 0x3FF)+LoSurrogate;
127− }else
128− *r++ = rc;
129− }
130− *r = '\0';
131− return or;
132−}
133−
134−int
135−runes16cmp(Rune16 *s1, Rune16 *s2)
136−{
137− Rune16 r1, r2;
138−
139− for(;;) {
140− r1 = *s1++;
141− r2 = *s2++;
142− if(r1 != r2) {
143− if(r1 > r2)
144− return 1;
145− return -1;
146− }
147− if(r1 == 0)
148− return 0;
149− }
150−}
151−
152−wchar_t *
153−widen(char *s)
154−{
155− int n;
156− wchar_t *ws;
157−
158− n = utflen(s) + 1;
159− ws = calloc(n, sizeof(wchar_t));
160− utftorunes16(ws, s, n);
161− return ws;
162−}
163−
164−
165−char *
166−narrowen(wchar_t *ws)
167−{
168− char *s;
169− int n;
170−
171− n = widebytes(ws);
172− s = malloc(n);
173− runes16toutf(s, ws, n);
174− return s;
175−}
176−
177−
178−int
179−widebytes(wchar_t *ws)
180−{
181− int n = 0;
182−
183− while(*ws)
184− n += runelen(*ws++);
185− return n+1;
186−}
gui-win32/r16.h+0−11
@@ -1,11 +0,0 @@
1−typedef unsigned short Rune16;
2−
3−wchar_t *widen(char *s);
4−char *narrowen(wchar_t *ws);
5−int widebytes(wchar_t *ws);
6−int runes16len(Rune16*);
7−int rune16nlen(Rune16*, int);
8−Rune16* runes16dup(Rune16*);
9−Rune16* utftorunes16(Rune16*, char*, int);
10−char* runes16toutf(char*, Rune16*, int);
11−int runes16cmp(Rune16*, Rune16*);
gui-win32/screen.c+7−8
@@ -14,7 +14,6 @@ · 13 unmodified lines
1414 #include <memdraw.h>
1515 #include "screen.h"
1616 #include "keyboard.h"
17−#include "r16.h"
1817
1918 Memimage *gscreen;
2019 Screeninfo screen;
@@ -557,14 +556,14 @@ setcolor(ulong index, ulong red, ulong green, ulong blue) · 536 unmodified lines
557556 char*
558557 clipreadunicode(HANDLE h)
559558 {
560− Rune16 *p;
559+ wchar_t *p;
561560 int n;
562561 char *q;
563562
564563 p = GlobalLock(h);
565− n = rune16nlen(p, runes16len(p)+1);
566− q = malloc(n);
567− runes16toutf(q, p, n);
564+ n = WideCharToMultiByte(CP_UTF8, 0, p, -1, 0, 0, 0, 0);
565+ q = malloc(n+1);
566+ WideCharToMultiByte(CP_UTF8, 0, p, -1, q, n, 0, 0);
568567 GlobalUnlock(h);
569568
570569 return q;
@@ -642,7 +641,7 @@ clipwrite(char *buf) · 71 unmodified lines
642641 {
643642 HANDLE h;
644643 char *p;
645− Rune16 *rp;
644+ wchar_t *rp;
646645 char *crbuf;
647646 int n;
648647
@@ -659,11 +658,11 @@ clipwrite(char *buf) · 10 unmodified lines
659658
660659 crbuf = addcr(buf, &n);
661660
662− h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));
661+ h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(rp[0]));
663662 if(h == NULL)
664663 panic("out of memory");
665664 rp = GlobalLock(h);
666− utftorunes16(rp, crbuf, n+1);
665+ MultiByteToWideChar(CP_UTF8, 0, crbuf, -1, rp, n+1);
667666 GlobalUnlock(h);
668667
669668 SetClipboardData(CF_UNICODETEXT, h);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t