Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lua_tstring.c « src - github.com/windirstat/lua-winreg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1ede73275315de865e7be5642537af74eef028c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <limits.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "luamacro.h"

size_t lua_utf8towcsZ(lua_State *L, const char *s, int len){
	wchar_t UNCH = 0;
    size_t cchWC = 0;                 // # of wchar_t code points generated
    const unsigned char * pUTF8 = (const unsigned char *)s;
    const unsigned char * pEnd = (const unsigned char *)(s + len);
	luaL_Buffer b;
	luaL_buffinit(L, &b);
    while (pUTF8 < pEnd){
        //  See if there are any trail bytes.
        if (*pUTF8 < 0xC0) {// 192
            //  Found ASCII.
			UNCH = pUTF8[0];
        }else if (pUTF8[0] < 0xE0){ //224
			if ((pUTF8[1] & 0xC0) == 0x80) {
				/* A two-byte-character lead-byte not followed by trail-byte represents itself.*/
				UNCH = (wchar_t) (((pUTF8[0] & 0x1F) << 6) | (pUTF8[1] & 0x3F));
				pUTF8 += 1;
			}else{
				/* A two-byte-character lead-byte not followed by trail-byte represents itself.	 */
				UNCH = pUTF8[0];
			}
		}else if (pUTF8[0] < 0xF0) {//240
			if (((pUTF8[1] & 0xC0) == 0x80) && ((pUTF8[2] & 0xC0) == 0x80)) {
				/* Three-byte-character lead byte followed by two trail bytes.*/
				UNCH = (wchar_t) (((pUTF8[0] & 0x0F) << 12) | ((pUTF8[1] & 0x3F) << 6) | (pUTF8[2] & 0x3F));
				pUTF8 += 2;
			}else{
				/* Three-byte-character lead byte followed by two trail bytes.*/
				UNCH = pUTF8[0];
			}
		}else{
			UNCH = pUTF8[0];
		}
		luaL_addlstring(&b, (const char*)&UNCH, sizeof(wchar_t));
        cchWC++;
        pUTF8++;
    }
	luaL_addchar(&b, '\0');// the other '\0' will be added by lua
	luaL_pushresult(&b);
    //  Return the number of wchar_t characters written.
    return (cchWC);
}

size_t lua_utf8towcs(lua_State *L, const char *s, int len){
	wchar_t UNCH = 0;
    size_t cchWC = 0;                 // # of wchar_t code points generated
    const unsigned char * pUTF8 = (const unsigned char *)s;
    const unsigned char * pEnd = (const unsigned char *)(s + len);
	luaL_Buffer b;
	luaL_buffinit(L, &b);
    while (pUTF8 < pEnd){
        //  See if there are any trail bytes.
        if (*pUTF8 < 0xC0) {// 192
            //  Found ASCII.
			UNCH = pUTF8[0];
        }else if (pUTF8[0] < 0xE0){ //224
			if ((pUTF8[1] & 0xC0) == 0x80) {
				/* A two-byte-character lead-byte not followed by trail-byte represents itself.*/
				UNCH = (wchar_t) (((pUTF8[0] & 0x1F) << 6) | (pUTF8[1] & 0x3F));
				pUTF8 += 1;
			}else{
				/* A two-byte-character lead-byte not followed by trail-byte represents itself.	 */
				UNCH = pUTF8[0];
			}
		}else if (pUTF8[0] < 0xF0) {//240
			if (((pUTF8[1] & 0xC0) == 0x80) && ((pUTF8[2] & 0xC0) == 0x80)) {
				/* Three-byte-character lead byte followed by two trail bytes.*/
				UNCH = (wchar_t) (((pUTF8[0] & 0x0F) << 12) | ((pUTF8[1] & 0x3F) << 6) | (pUTF8[2] & 0x3F));
				pUTF8 += 2;
			}else{
				/* Three-byte-character lead byte followed by two trail bytes.*/
				UNCH = pUTF8[0];
			}
		}else{
			UNCH = pUTF8[0];
		}
		luaL_addlstring(&b, (const char*)&UNCH, sizeof(wchar_t));
        cchWC++;
        pUTF8++;
    }
	luaL_pushresult(&b);
    //  Return the number of wchar_t characters written.
    return (cchWC);
}

//  Constant Declarations.
#define ASCII             0x007f
#define UTF8_2_MAX        0x07ff  // max UTF8 2-byte sequence (32 * 64 = 2048)
#define UTF8_1ST_OF_2     0xc0    // 110x xxxx
#define UTF8_1ST_OF_3     0xe0    // 1110 xxxx
#define UTF8_TRAIL        0x80    // 10xx xxxx
#define HIGER_6_BIT(u)    ((u) >> 12)
#define MIDDLE_6_BIT(u)   (((u) & 0x0fc0) >> 6)
#define LOWER_6_BIT(u)    ((u) & 0x003f)

#ifndef HIBYTE
#define BYTE unsigned char
#define HIBYTE(w)   ((BYTE) (((wchar_t) (w) >> 8) & 0xFF))  
#define LOBYTE(w)   ((BYTE) (w))  
#endif

//  Maps a wchar_t character string to its UTF-8 string counterpart.
size_t lua_wcstoutf8(lua_State *L, const wchar_t *s, size_t cchSrc){
	const wchar_t * lpWC = s;
    size_t cchU8 = 0;                // # of UTF8 chars generated

	luaL_Buffer b;
	luaL_buffinit(L, &b);

    while (cchSrc--)   {
        if (*lpWC <= ASCII){
            //  Found ASCII.
			luaL_addchar(&b, (char)*lpWC);
            cchU8++;
		}else if (*lpWC <= UTF8_2_MAX){
            //  Found 2 byte sequence if < 0x07ff (11 bits).
            //  Use upper 5 bits in first byte.
            //  Use lower 6 bits in second byte.
			luaL_addchar(&b, (char)(UTF8_1ST_OF_2 | (*lpWC >> 6)));
			luaL_addchar(&b, (char)(UTF8_TRAIL    | LOWER_6_BIT(*lpWC)));
			cchU8 += 2;
        }else{
            //  Found 3 byte sequence.
            //  Use upper  4 bits in first byte.
            //  Use middle 6 bits in second byte.
            //  Use lower  6 bits in third byte.
			luaL_addchar(&b, (char)(UTF8_1ST_OF_3 | (*lpWC >> 12)));
			luaL_addchar(&b, (char)(UTF8_TRAIL    | MIDDLE_6_BIT(*lpWC)));
			luaL_addchar(&b, (char)(UTF8_TRAIL    | LOWER_6_BIT(*lpWC)));
			cchU8 += 3;
        }
        lpWC++;
    }
	luaL_pushresult(&b);
    //  Return the number of UTF-8 characters written.
    return (cchU8);
}

size_t lua_chartowcsZ(lua_State *L, const char *s, int len){
	luaL_Buffer b;
    size_t cchWC = 0;
	wchar_t UNCH = 0;
    const unsigned char * pCur = (const unsigned char *)s;
    const unsigned char * pEnd = (const unsigned char *)(s + len);
	luaL_buffinit(L, &b);
    while (pCur < pEnd){
		UNCH = pCur[0];
		luaL_addlstring(&b, (const char*)&UNCH, sizeof(wchar_t));
        cchWC++;
        pCur++;
	}
	luaL_addchar(&b, '\0');// the other '\0' will be added by lua
	luaL_pushresult(&b);
    //  Return the number of wchar_t characters written.
    return (cchWC);
}
int wc2utf8(char *d, wchar_t ch){
	if (ch < 0x80) {
		*d++ = (char)ch;
		return 1;
	}
	if (ch < 0x800) {
		*d++ = (char)(( ch >>  6)         | 0xc0);
		*d++ = (char)(( ch        & 0x3f) | 0x80);
		return 2;
	}
	{
		*d++ = (char)(( ch >> 12)         | 0xe0);
		*d++ = (char)(((ch >>  6) & 0x3f) | 0x80);
		*d++ = (char)(( ch        & 0x3f) | 0x80);
		return 3;
	}
}
// push a wchar_t character value convert equiv utf8 chars
void lua_pushutf8_from_wchar(lua_State *L, wchar_t ch){
	char buf[4] = {0,0,0,0};
	lua_pushlstring(L, buf, wc2utf8(buf, ch));
}
// add a wchar_t character to buffer converted to equiv utf8 chars
void lua_addutf8_from_wchar(luaL_Buffer * pB, wchar_t ch){
	luaL_addsize(pB, wc2utf8(luaL_prepbuffer (pB), ch));
}
// gets an int character value, if string is >= 2 chars checks if utf
wchar_t lua_checkwchar_from_utf8(lua_State *L, int i){
	const char* psz = luaL_checkstring(L, i);
    if ((psz[0] && psz[1] == 0) || psz[0] == 0) {
		return psz[0];// single character
    }else if ((psz[0] >= 0xC0) && (psz[0] < 0xE0)
		&&	(psz[1] & 0xC0) == 0x80
		&&	(psz[2] == 0)// Two-byte-character lead-byte followed by a trail-byte.
	){	return  (wchar_t)(((psz[0] & 0x1F) << 6) | (psz[1] & 0x3F));
	}else if ((psz[0] >= 0xE0) && (psz[0] < 0xF0)
		&&	((psz[1] & 0xC0) == 0x80)
		&&	((psz[2] & 0xC0) == 0x80)
		&&	(psz[3] == 0)// Three-utf-character lead utf followed by two trail bytes.
	){	return (wchar_t)(((psz[0] & 0x0F) << 12) | ((psz[1] & 0x3F) << 6) | (psz[2] & 0x3F));
	}else{
		luaL_argerror(L, i, "character expected");
	}
	return 0;
}
// lua utf8 string to wide string
const wchar_t *lua_tolwcs_from_utf8(lua_State *L, int narg, size_t* l){
	size_t ulen = 0;
	const char * psz;
	narg = lua_absindex(L, narg);
#if LUA_VERSION_NUM >= 501
	psz = lua_tolstring(L, narg, &ulen);
#else
	psz = lua_tostring(L, narg);
	ulen = lua_strlen(L, narg);
#endif
	if(psz){
		ulen = (size_t)lua_utf8towcsZ(L, psz, (int)ulen);
		if(l)*l = ulen;
		lua_replace (L, narg);
		return (const wchar_t *)lua_tostring(L, narg);
	}else{
		return NULL;
	}
}
// lua utf8 string to wide string, with len
const wchar_t *lua_checklwcs_from_utf8(lua_State *L, int narg, size_t* l){
	size_t ulen = 0;
	const char * psz;
	narg = lua_absindex(L, narg);
	psz = luaL_checklstring(L, narg, &ulen);
	ulen = (size_t)lua_utf8towcsZ(L, psz, (int)ulen);
	if(l)*l = ulen;
	lua_replace (L, narg);
	return (const wchar_t *)lua_tostring(L, narg);
}
// lua utf8 string to wide string, with len, optional
const wchar_t *lua_optlwcs_from_utf8(lua_State *L, int narg, const wchar_t *def, size_t *len){
	if (lua_isnoneornil(L, narg)) {
		if (len)
			*len = (def ? wcslen(def) : 0);
		return def;
	}
	else return lua_checklwcs_from_utf8(L, narg, len);
}
// lua push wide string, convert to utf8
void lua_pushutf8_from_wcs (lua_State *L, const wchar_t *s) {
  if (s == NULL)
    lua_pushnil(L);
  else
    lua_wcstoutf8(L, s, wcslen(s));
}


size_t lua_cstowcsZ(lua_State *L, const char *s, int len){
	wchar_t UNCH = 0;
    size_t cchWC = 0;                 // # of wchar_t code points generated
    const unsigned char * pStr = (const unsigned char *)s;
    const unsigned char * pEnd = (const unsigned char *)(s + len);
	luaL_Buffer b;
	luaL_buffinit(L, &b);

    while (pStr < pEnd){
		UNCH = *pStr;
		luaL_addlstring(&b, (const char*)&UNCH, sizeof(wchar_t));
        cchWC++;
        pStr++;
    }
	luaL_addchar(&b, '\0');// the other '\0' will be added by lua
	luaL_pushresult(&b);
    //  Return the number of wchar_t characters written.
    return (cchWC);
}
// Lua char string to wide string, with len
const wchar_t *lua_checklwcs_from_char(lua_State *L, int narg, size_t* l){
	size_t ulen = 0;
	const char * psz;
	narg = lua_absindex(L, narg);
	psz = luaL_checklstring(L, narg, &ulen);
	ulen = (size_t)lua_cstowcsZ(L, psz, (int)ulen);
	if(l)*l = ulen;
	lua_replace (L, narg);
	return (const wchar_t *)lua_tostring(L, narg);
}
// Lua char string to wide string, with len, optional
const wchar_t *lua_optlwcs_from_char (lua_State *L, int narg, const wchar_t *def, size_t *len){
	if (lua_isnoneornil(L, narg)) {
		if (len)
			*len = (def ? wcslen(def) : 0);
		return def;
	}
	else return lua_checklwcs_from_char(L, narg, len);
}
//  Maps a wchar_t character string to its char string counterpart.
size_t lua_wcstochar(lua_State *L, const wchar_t *s, size_t cchSrc){
	const wchar_t * lpWC = s;
    size_t cch = 0;                // # of UTF8 chars generated

	luaL_Buffer b;
	luaL_buffinit(L, &b);

    while (cchSrc--)   {
        if (*lpWC <= 0x00ff){
			luaL_addchar(&b, (char)*lpWC);
		}else{
			luaL_addchar(&b, '?');// no choice
        }
		cch++;
        lpWC++;
    }
	luaL_pushresult(&b);
    //  Return the number of char characters written.
    return (cch);
}







/*
size_t lua_utf8towcsZ2(lua_State *L, const char *s, int len){
    size_t cchWC = 0;                 // # of wchar_t code points generated
    const unsigned char * pUTF8 = (const unsigned char *)s;
    const unsigned char * pEnd = (const unsigned char *)(s + len);
    wchar_t * pBuf = NULL;
	const wchar_t * pBuf0 = NULL;
	const wchar_t * pBuf1 = NULL;
	luaL_Buffer b;
	luaL_buffinit(L, &b);
    while (pUTF8 < pEnd){
		pBuf = (wchar_t *)luaL_prepbuffer(&b);
		pBuf0 = pBuf;
		pBuf1 = pBuf + (LUAL_BUFFERSIZE-sizeof(wchar_t));
		while(pUTF8 < pEnd && pBuf <= pBuf1){
			//  See if there are any trail bytes.
			if (*pUTF8 < 0xC0) {// 192
				//  Found ASCII.
				*pBuf++ = pUTF8[0];
			}else if (pUTF8[0] < 0xE0){ //224
				if ((pUTF8[1] & 0xC0) == 0x80) {
					// A two-byte-character lead-byte not followed by trail-byte represents itself.
					*pBuf++ = (wchar_t) (((pUTF8[0] & 0x1F) << 6) | (pUTF8[1] & 0x3F));
					pUTF8 += 1;
				}else{
					// A two-byte-character lead-byte not followed by trail-byte represents itself.
					*pBuf++ = pUTF8[0];
				}
			}else if (pUTF8[0] < 0xF0) {//240
				if (((pUTF8[1] & 0xC0) == 0x80) && ((pUTF8[2] & 0xC0) == 0x80)) {
					// Three-byte-character lead byte followed by two trail bytes.
					*pBuf++ = (wchar_t) (((pUTF8[0] & 0x0F) << 12) | ((pUTF8[1] & 0x3F) << 6) | (pUTF8[2] & 0x3F));
					pUTF8 += 2;
				}else{
					// Three-byte-character lead byte followed by two trail bytes.
					*pBuf++ = pUTF8[0];
				}
			}else{
				*pBuf++ = pUTF8[0];
			}
			cchWC++;
			pUTF8++;
		}
		luaL_addsize(&b, (size_t)(pBuf0 - pBuf));
    }
	luaL_addchar(&b, '\0');// the other '\0' will be added by lua
	luaL_pushresult(&b);
    //  Return the number of wchar_t characters written.
    return (cchWC);
}//*/