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
4 4
5OFILES=\5OFILES=\
6 screen.$O\6 screen.$O\
7 r16.$O
8 7
9default: $(LIB)8default: $(LIB)
10$(LIB): $(OFILES)9$(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
14enum
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
31Rune16*
32runes16dup(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
43int
44runes16len(Rune16 *r)
45{
46 int n;
47
48 n = 0;
49 while(*r++ != 0)
50 n++;
51 return n;
52}
53
54char*
55runes16toutf(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
91int
92rune16nlen(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
113Rune16*
114utftorunes16(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
134int
135runes16cmp(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
152wchar_t *
153widen(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
165char *
166narrowen(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
178int
179widebytes(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 @@
1typedef unsigned short Rune16;
2
3wchar_t *widen(char *s);
4char *narrowen(wchar_t *ws);
5int widebytes(wchar_t *ws);
6int runes16len(Rune16*);
7int rune16nlen(Rune16*, int);
8Rune16* runes16dup(Rune16*);
9Rune16* utftorunes16(Rune16*, char*, int);
10char* runes16toutf(char*, Rune16*, int);
11int runes16cmp(Rune16*, Rune16*);
gui-win32/screen.c+7−8
@@ -14,7 +14,6 @@ · 13 unmodified lines
14#include <memdraw.h>14#include <memdraw.h>
15#include "screen.h"15#include "screen.h"
16#include "keyboard.h"16#include "keyboard.h"
17#include "r16.h"
18 17
19Memimage *gscreen;18Memimage *gscreen;
20Screeninfo screen;19Screeninfo screen;
@@ -557,14 +556,14 @@ setcolor(ulong index, ulong red, ulong green, ulong blue) · 536 unmodified lines
557char*556char*
558clipreadunicode(HANDLE h)557clipreadunicode(HANDLE h)
559{558{
560 Rune16 *p;559 wchar_t *p;
561 int n;560 int n;
562 char *q;561 char *q;
563 562
564 p = GlobalLock(h);563 p = GlobalLock(h);
565 n = rune16nlen(p, runes16len(p)+1);564 n = WideCharToMultiByte(CP_UTF8, 0, p, -1, 0, 0, 0, 0);
566 q = malloc(n);565 q = malloc(n+1);
567 runes16toutf(q, p, n);566 WideCharToMultiByte(CP_UTF8, 0, p, -1, q, n, 0, 0);
568 GlobalUnlock(h);567 GlobalUnlock(h);
569 568
570 return q;569 return q;
@@ -642,7 +641,7 @@ clipwrite(char *buf) · 71 unmodified lines
642{641{
643 HANDLE h;642 HANDLE h;
644 char *p;643 char *p;
645 Rune16 *rp;644 wchar_t *rp;
646 char *crbuf;645 char *crbuf;
647 int n;646 int n;
648 647
@@ -659,11 +658,11 @@ clipwrite(char *buf) · 10 unmodified lines
659 658
660 crbuf = addcr(buf, &n);659 crbuf = addcr(buf, &n);
661 660
662 h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune));661 h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(rp[0]));
663 if(h == NULL)662 if(h == NULL)
664 panic("out of memory");663 panic("out of memory");
665 rp = GlobalLock(h);664 rp = GlobalLock(h);
666 utftorunes16(rp, crbuf, n+1);665 MultiByteToWideChar(CP_UTF8, 0, crbuf, -1, rp, n+1);
667 GlobalUnlock(h);666 GlobalUnlock(h);
668 667
669 SetClipboardData(CF_UNICODETEXT, h);668 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