| ︙ | | | ︙ | |
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
|
Qrrt KhBb e4vF 58NR
8PTF dYk2 Sozc HexX
4Q69 jbdQ Asrs P7B
why i cancel use zig:
gabarages in output code.
zig perfecly crash without printing stack trace in non standart situations.
Zig does not try fix console flags before print stack trace.
zig can crash to segfault like c.
tagged enum does not sense.
c: tagged_enum.tag = ANOTHER_TAG;
zig: tagged_enum = .{.ANOTHER_TAG = tagged_enum.LAST_USED_TAG.data};
operator ".?" does not sense:
c: while(link) { i++; link = link->next; }
zig: while(link) |_| { i += 1; link = link.?.next; }
zig idiomatic: while(link) |unwrapped_link|{ i += 1; link = unwrapped_link.next; }
nullable types are inconvenient.
c: if (link) i++;
zig idiomatic: if (link) |_| i += 1;
zig but C pointer: if (@intFromPtr(link) != @intFromPtr(c.NULL)) i += 1;
*/
// TODO
/*
remove name buffer_len. use buffer_max_size and buffer_used instead.
fix bold in folding mode. why one upper line is skipped?
update OsConsoleInput_Handle // remove C variant.
*/
// macro
// enums
// compilation target
// os
#define __TargetOs_windows__ 1
#define __TargetOs_linux__ 2
|
|
<
<
|
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
|
Qrrt KhBb e4vF 58NR
8PTF dYk2 Sozc HexX
4Q69 jbdQ Asrs P7B
why i cancel use zig:
gabarages in output code.
zig perfecly crash without printing stack trace in non standart situations. (main feature of zig)
Zig does not try fix console flags before print stack trace.
zig can crash to segfault like c.
tagged enum does not sense.
c: tagged_enum.tag = ANOTHER_TAG;
zig: tagged_enum = .{.ANOTHER_TAG = tagged_enum.LAST_USED_TAG.data};
operator ".?" does not sense:
c: while(link) { i++; link = link->next; }
zig: while(link) |_| { i += 1; link = link.?.next; }
zig idiomatic: while(link) |unwrapped_link|{ i += 1; link = unwrapped_link.next; }
nullable types are inconvenient.
c: if (link) i++;
zig idiomatic: if (link) |_| i += 1;
zig but C pointer: if (@intFromPtr(link) != @intFromPtr(c.NULL)) i += 1;
*/
// TODO
/*
remove name buffer_len. use buffer_max_size and buffer_used instead.
*/
// macro
// enums
// compilation target
// os
#define __TargetOs_windows__ 1
#define __TargetOs_linux__ 2
|
| ︙ | | | ︙ | |
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#define __TerminalInput_size__ 32
// __App__
#define __AppEditorViewsEasyMotionHorizontal_step__ 3
#define __AppEditorViewsEasyMotionVertical_step__ 2
#define __App_expectedFps__ 30
#define __AppEditorLineLen_max__ (1024 * 8)
// AppEditorTheme
#define __AppEditorTheme_disabled__ \
__ConsoleAnsiOutputFontStyle_start__ \
__ConsoleAnsiOutputFontStyle_bg_color_zero__ \
__ConsoleAnsiOutputFontStyle_sep__ \
__ConsoleAnsiOutputFontStyle_color_magenta__ \
__ConsoleAnsiOutputFontStyle_end__
#define __AppEditorTheme_commentary__ \
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
#define __TerminalInput_size__ 32
// __App__
#define __AppEditorViewsEasyMotionHorizontal_step__ 3
#define __AppEditorViewsEasyMotionVertical_step__ 2
#define __App_expectedFps__ 30
#define __AppEditorLineLen_max__ (1024 * 8)
// AppEditorTheme
#define __AppEditorTheme_headers__ \
__ConsoleAnsiOutputFontStyle_start__ \
__ConsoleAnsiOutputFontStyle_bold__ \
__ConsoleAnsiOutputFontStyle_sep__ \
__ConsoleAnsiOutputFontStyle_bg_color_zero__ \
__ConsoleAnsiOutputFontStyle_sep__ \
__ConsoleAnsiOutputFontStyle_color_cyan__ \
__ConsoleAnsiOutputFontStyle_end__
#define __AppEditorTheme_easy_motion__ \
__ConsoleAnsiOutputFontStyle_start__ \
__ConsoleAnsiOutputFontStyle_bg_color_zero__ \
__ConsoleAnsiOutputFontStyle_sep__ \
__ConsoleAnsiOutputFontStyle_color_yellow__ \
__ConsoleAnsiOutputFontStyle_end__
#define __AppEditorTheme_disabled__ \
__ConsoleAnsiOutputFontStyle_start__ \
__ConsoleAnsiOutputFontStyle_bg_color_zero__ \
__ConsoleAnsiOutputFontStyle_sep__ \
__ConsoleAnsiOutputFontStyle_color_magenta__ \
__ConsoleAnsiOutputFontStyle_end__
#define __AppEditorTheme_commentary__ \
|
| ︙ | | | ︙ | |
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#define __NetworkTcpHttpWebSocketHeaderLong__ 2
#define __NetworkTcpHttpWebSocketHeaderLong_flag__ 127
#define __NetworkTcpHttpWebSocketHeaderLongExtendedPayloadLen_size__ 8
#define __NetworkTcpHttpWebSocketHeaderLong_size__ (__NetworkTcpHttpWebSocketHeaderSmall_size__ + __NetworkTcpHttpWebSocketHeaderLongExtendedPayloadLen_size__)
// time
#define __Time_msPerS__ 1000
// formats
#define __TextCharsDecFromU64_lenMax__ __CText_len__("18446744073709551615")
// OsNetwork
#define __OsNetworkTcp_maxAllowedListenPort__ 49151
// auto unwrap CText to text and len
// # __OsConsoleOutput_writeCText__
#define __OsConsoleOutput_writeCText__( ctx, text) \
__OsConsoleOutput_write__( ctx, text, __CText_len__(text))
|
>
|
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
#define __NetworkTcpHttpWebSocketHeaderLong__ 2
#define __NetworkTcpHttpWebSocketHeaderLong_flag__ 127
#define __NetworkTcpHttpWebSocketHeaderLongExtendedPayloadLen_size__ 8
#define __NetworkTcpHttpWebSocketHeaderLong_size__ (__NetworkTcpHttpWebSocketHeaderSmall_size__ + __NetworkTcpHttpWebSocketHeaderLongExtendedPayloadLen_size__)
// time
#define __Time_msPerS__ 1000
// formats
#define __u64FromTextDec_maxLen__ __CText_len__("18446744073709551615")
#define __TextCharsDecFromU64_lenMax__ __CText_len__("18446744073709551615")
// OsNetwork
#define __OsNetworkTcp_maxAllowedListenPort__ 49151
// auto unwrap CText to text and len
// # __OsConsoleOutput_writeCText__
#define __OsConsoleOutput_writeCText__( ctx, text) \
__OsConsoleOutput_write__( ctx, text, __CText_len__(text))
|
| ︙ | | | ︙ | |
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
void call_other_trace_example(
struct __Trace__* prev_trace // can be null
) {
struct __Trace__ trace; __Trace_init__(&trace, prev_trace, __LINE__);
trace.__Trace_line__ = __LINE__; print_trace_example(0, &trace);
}
// mem
// bits operations
uint8_t bits_rotl8(uint8_t value, unsigned int shift) {
shift %= 8;
return (value << shift) | (value >> (8 - shift));
}
uint16_t bits_rotl16(uint16_t value, unsigned int shift) {
shift %= 16;
return (value << shift) | (value >> (16 - shift));
}
uint32_t bits_rotl32(uint32_t value, unsigned int shift) {
shift %= 32;
return (value << shift) | (value >> (32 - shift));
}
uint64_t bits_rotl64(uint64_t value, unsigned int shift) {
shift %= 64;
return (value << shift) | (value >> (64 - shift));
}
// bytes operations
// # __Mem_fill__
void __Mem_fill__(
char *dest,
char symbol,
size_t len
) {
for (size_t pos = 0; pos < len; pos++) {
|
|
|
|
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
|
void call_other_trace_example(
struct __Trace__* prev_trace // can be null
) {
struct __Trace__ trace; __Trace_init__(&trace, prev_trace, __LINE__);
trace.__Trace_line__ = __LINE__; print_trace_example(0, &trace);
}
// mem
// bits
uint8_t bits_rotl8(uint8_t value, unsigned int shift) {
shift %= 8;
return (value << shift) | (value >> (8 - shift));
}
uint16_t bits_rotl16(uint16_t value, unsigned int shift) {
shift %= 16;
return (value << shift) | (value >> (16 - shift));
}
uint32_t bits_rotl32(uint32_t value, unsigned int shift) {
shift %= 32;
return (value << shift) | (value >> (32 - shift));
}
uint64_t bits_rotl64(uint64_t value, unsigned int shift) {
shift %= 64;
return (value << shift) | (value >> (64 - shift));
}
// bytes
// # __Mem_fill__
void __Mem_fill__(
char *dest,
char symbol,
size_t len
) {
for (size_t pos = 0; pos < len; pos++) {
|
| ︙ | | | ︙ | |
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
|
src = &src[len - 1];
size_t iter = 0;
for(; iter < len; iter++, dest++, src--) {
*dest = *src;
}
}
// # ____Mem_find__DiffOrLen__
size_t ____Mem_find__DiffOrLen__(const char a[], const char b[], size_t len) {
size_t pos = 0;
while(pos < len) {
if (a[pos] != b[pos]) break;
pos++;
}
return pos;
}
// # __Mem_isEql__
bool __Mem_isEql__(const char a[], const char b[], size_t len) {
return ____Mem_find__DiffOrLen__(a, b, len) == len;
}
// # __Mem_isEql_test_
bool __Mem_isEql_test_() {
return true
and (__Mem_isEql__("abcd", "abcd", 4))
and (__Mem_isEql__("abcd", "abce", 4) == false)
and (__Mem_isEql__("abcdasdasd", "asd", 3) == false)
|
|
|
|
|
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
|
src = &src[len - 1];
size_t iter = 0;
for(; iter < len; iter++, dest++, src--) {
*dest = *src;
}
}
// # __Mem_findDiffOrLen__
size_t __Mem_findDiffOrLen__(const char a[], const char b[], size_t len) {
size_t pos = 0;
while(pos < len) {
if (a[pos] != b[pos]) break;
pos++;
}
return pos;
}
// # __Mem_isEql__
bool __Mem_isEql__(const char a[], const char b[], size_t len) {
return __Mem_findDiffOrLen__(a, b, len) == len;
}
// # __Mem_isEql_test_
bool __Mem_isEql_test_() {
return true
and (__Mem_isEql__("abcd", "abcd", 4))
and (__Mem_isEql__("abcd", "abce", 4) == false)
and (__Mem_isEql__("abcdasdasd", "asd", 3) == false)
|
| ︙ | | | ︙ | |
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
|
size_t delta = indent - new_indent;
__Buffer_removeIndent__(buffer, buffer_used, delta);
}
}
return ok;
}
// Finder
void __Finder_init__(
struct Finder* t,
char* text,
size_t text_len,
char* desired,
size_t desired_len
) {
t->pos = 0;
t->text.ptr = text;
t->text.len = text_len;
t->desired.ptr = desired;
t->desired.len = desired_len;
}
bool __Finder_next__(struct Finder* t, size_t* out_pos) {
if (t->pos != t->text.len) {
struct Slice slice; // = t->text[->.pos..];
slice.ptr = &t->text.ptr[t->pos];
slice.len = t->text.len - t->pos;
size_t finded;
if (__Mem_find__(&slice, &t->desired, &finded)) {
size_t new_pos = t->pos + finded;
|
>
|
|
>
>
>
|
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
|
size_t delta = indent - new_indent;
__Buffer_removeIndent__(buffer, buffer_used, delta);
}
}
return ok;
}
// Finder
// # __Finder_init__
void __Finder_init__(
struct Finder* t,
char* text,
size_t text_len,
char* desired,
size_t desired_len
) {
t->pos = 0;
t->text.ptr = text;
t->text.len = text_len;
t->desired.ptr = desired;
t->desired.len = desired_len;
}
// # __Finder_next__
bool __Finder_next__(
struct Finder* t,
size_t* out_pos
) {
if (t->pos != t->text.len) {
struct Slice slice; // = t->text[->.pos..];
slice.ptr = &t->text.ptr[t->pos];
slice.len = t->text.len - t->pos;
size_t finded;
if (__Mem_find__(&slice, &t->desired, &finded)) {
size_t new_pos = t->pos + finded;
|
| ︙ | | | ︙ | |
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
|
size_t current_pos = __OsFile_getPos__(file);
ftruncate(file, current_pos);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
}
// time
uint64_t __os_getTick__() {
#if __target_os__ == __TargetOs_windows__
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
uint64_t ft64;
__Mem_copy__((char*) &ft64, (char*) &ft, 8);
return ft64 / 10000; // in ft is intervals * 100 ns. to ms need * 100 / 1000000
|
>
|
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
|
size_t current_pos = __OsFile_getPos__(file);
ftruncate(file, current_pos);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
}
// time
// # __os_getTick__
uint64_t __os_getTick__() {
#if __target_os__ == __TargetOs_windows__
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
uint64_t ft64;
__Mem_copy__((char*) &ft64, (char*) &ft, 8);
return ft64 / 10000; // in ft is intervals * 100 ns. to ms need * 100 / 1000000
|
| ︙ | | | ︙ | |
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
|
t->handle = input_hanle;
t->fileno = fileno(t->handle);
#else
#error "__target_os__ not implemented"
#endif
}
// Flags
uint32_t __BitsU32_enable__(uint32_t value, uint32_t mask) {
return value | mask;
}
uint32_t __BitsU32_disable__(uint32_t value, uint32_t mask) {
return value & (~mask);
}
uint32_t __BitsU32_toggle__(uint32_t value, uint32_t mask, bool new_state) {
if (new_state) {
return __BitsU32_enable__(value, mask);
} else {
return __BitsU32_disable__(value, mask);
}
}
bool __OsConsoleFlags_setNonCanonicalMode__(struct __OsConsoleFlags__ *t) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
uint32_t input = t->input_mode;
input = __BitsU32_toggle__(input, ENABLE_EXTENDED_FLAGS, true);
input = __BitsU32_toggle__(input, ENABLE_ECHO_INPUT, false); // print pressed keys
input = __BitsU32_toggle__(input, ENABLE_LINE_INPUT, false); // wait '\n' (buffering on enter)
|
>
|
|
|
|
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
|
t->handle = input_hanle;
t->fileno = fileno(t->handle);
#else
#error "__target_os__ not implemented"
#endif
}
// Flags
// # __BitsU32_enable__
uint32_t __BitsU32_enable__(uint32_t value, uint32_t mask) {
return value | mask;
}
// # __BitsU32_disable__
uint32_t __BitsU32_disable__(uint32_t value, uint32_t mask) {
return value & (~mask);
}
// # __BitsU32_toggle__
uint32_t __BitsU32_toggle__(uint32_t value, uint32_t mask, bool new_state) {
if (new_state) {
return __BitsU32_enable__(value, mask);
} else {
return __BitsU32_disable__(value, mask);
}
}
// # __OsConsoleFlags_setNonCanonicalMode__
bool __OsConsoleFlags_setNonCanonicalMode__(struct __OsConsoleFlags__ *t) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
uint32_t input = t->input_mode;
input = __BitsU32_toggle__(input, ENABLE_EXTENDED_FLAGS, true);
input = __BitsU32_toggle__(input, ENABLE_ECHO_INPUT, false); // print pressed keys
input = __BitsU32_toggle__(input, ENABLE_LINE_INPUT, false); // wait '\n' (buffering on enter)
|
| ︙ | | | ︙ | |
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
|
oflag = __BitsU32_toggle__(oflag, OFDEL, false); // use null for deleted symbol
t->termios.c_oflag = oflag;
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return ok;
}
bool __OsConsoleFlags_apply__(
struct __OsConsoleFlags__ *t,
struct __OsConsoleInput__ *input,
struct __OsConsoleOutput__ *output
) {
// expected inited
|
|
|
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
|
oflag = __BitsU32_toggle__(oflag, OFDEL, false); // use null for deleted symbol
t->termios.c_oflag = oflag;
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return ok;
}
// # __OsConsoleFlags_apply__
bool __OsConsoleFlags_apply__(
struct __OsConsoleFlags__ *t,
struct __OsConsoleInput__ *input,
struct __OsConsoleOutput__ *output
) {
// expected inited
|
| ︙ | | | ︙ | |
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
|
tcsetattr(input->fileno, TCSANOW, &t->termios);
tcsetattr(output->__OsConsoleOutput_handle__, TCSANOW, &t->termios);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return ok;
}
bool __OsConsoleFlags_init__(struct __OsConsoleFlags__ *t, struct __OsConsoleInput__* input, struct __OsConsoleOutput__ *output) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
bool result1 = GetConsoleMode(input->handle, &t->input_mode);
if (result1 != 0) {
bool result2 = GetConsoleMode(output->__OsConsoleOutput_handle__, &t->output_mode);
if (result2 != 0) {
|
|
|
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
|
tcsetattr(input->fileno, TCSANOW, &t->termios);
tcsetattr(output->__OsConsoleOutput_handle__, TCSANOW, &t->termios);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return ok;
}
// # __OsConsoleFlags_init__
bool __OsConsoleFlags_init__(struct __OsConsoleFlags__ *t, struct __OsConsoleInput__* input, struct __OsConsoleOutput__ *output) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
bool result1 = GetConsoleMode(input->handle, &t->input_mode);
if (result1 != 0) {
bool result2 = GetConsoleMode(output->__OsConsoleOutput_handle__, &t->output_mode);
if (result2 != 0) {
|
| ︙ | | | ︙ | |
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
|
__OsConsoleOutput_write__ (NULL, data, len);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return len;
}
// Helper
bool __OsHelper_init__(struct __OsHelper__* t) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
HMODULE instance = GetModuleHandleA(NULL);
if (instance != NULL) {
t->instance = instance;
|
>
|
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
|
__OsConsoleOutput_write__ (NULL, data, len);
#else
#error "not implemented current __target_os__ or not defined. (expected __TargetOs_ value)"
#endif
return len;
}
// Helper
// # __OsHelper_init__
bool __OsHelper_init__(struct __OsHelper__* t) {
bool ok = true;
#if __target_os__ == __TargetOs_windows__
HMODULE instance = GetModuleHandleA(NULL);
if (instance != NULL) {
t->instance = instance;
|
| ︙ | | | ︙ | |
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
|
t->console_input_handle = stdin;
t->console_output_handle = 1; // standart output
#else
#error "__target_os__ not implemented"
#endif
return ok;
}
void __OsHelper_deinit__(struct __OsHelper__*t) {
#if __target_os__ == __TargetOs_windows__
#if __with_network__
WSACleanup();
#endif
FreeLibrary(t->instance);
#elif __target_os__ == __TargetOs_linux__
// nothing to do
#else
#error "__target_os__ not implemented"
#endif
}
void __OsHelper_process__(struct __OsHelper__* t) {
#if __target_os__ == __TargetOs_windows__
MSG msg;
if (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
#elif __target_os__ == __TargetOs_linux__
// nothing to do
#else
#error "__target_os__ not implemented"
#endif
}
// text
// Splitter
void __TextSplitter_init__(struct __Text_Splitter__* t, char* text, size_t text_len, char* delim, size_t delim_len) {
t->stop = false;
t->last_pos = 0;
__Finder_init__(&t->finder, text, text_len, delim, delim_len);
}
bool __TextSplitter_next__(struct __Text_Splitter__* t, struct Slice* out_slice) {
if (t->stop == false) {
size_t finded;
if (__Finder_next__(&t->finder, &finded)) {
out_slice->ptr = &t->finder.text.ptr[t->last_pos]; // slice = t->finder.text[t->last_pos..finded];
out_slice->len = finded - t->last_pos;
t->last_pos = t->finder.pos;
|
|
|
>
|
|
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
|
t->console_input_handle = stdin;
t->console_output_handle = 1; // standart output
#else
#error "__target_os__ not implemented"
#endif
return ok;
}
// # __OsHelper_deinit__
void __OsHelper_deinit__(struct __OsHelper__*t) {
#if __target_os__ == __TargetOs_windows__
#if __with_network__
WSACleanup();
#endif
FreeLibrary(t->instance);
#elif __target_os__ == __TargetOs_linux__
// nothing to do
#else
#error "__target_os__ not implemented"
#endif
}
// # __OsHelper_process__
void __OsHelper_process__(struct __OsHelper__* t) {
#if __target_os__ == __TargetOs_windows__
MSG msg;
if (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
#elif __target_os__ == __TargetOs_linux__
// nothing to do
#else
#error "__target_os__ not implemented"
#endif
}
// text
// Splitter
// # __TextSplitter_init__
void __TextSplitter_init__(struct __Text_Splitter__* t, char* text, size_t text_len, char* delim, size_t delim_len) {
t->stop = false;
t->last_pos = 0;
__Finder_init__(&t->finder, text, text_len, delim, delim_len);
}
// # __TextSplitter_next__
bool __TextSplitter_next__(struct __Text_Splitter__* t, struct Slice* out_slice) {
if (t->stop == false) {
size_t finded;
if (__Finder_next__(&t->finder, &finded)) {
out_slice->ptr = &t->finder.text.ptr[t->last_pos]; // slice = t->finder.text[t->last_pos..finded];
out_slice->len = finded - t->last_pos;
t->last_pos = t->finder.pos;
|
| ︙ | | | ︙ | |
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
|
ok = false;
}
__OsFile_close__(file);
}
return ok;
}
// formats
// __TextFormatU64__ (utoa utos)
// # __TextFormatU64_do__
void __TextFormatU64_do__(
struct __TextFormatU64__ *t,
uint64_t number,
size_t min_width
) {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
|
ok = false;
}
__OsFile_close__(file);
}
return ok;
}
// formats
// # __u64FromTextDec__ atoi atou
uint64_t
__u64FromTextDec__(
char *bytes, // expected bytes only with numbers from '0' to '9'
size_t len // expected > 0; expected <= __u64FromTextDec_maxLen__
) {
uint64_t result = 0;
size_t numerical_place = 1;
size_t pos = len - 1; // last symbol
while (true) {
char byte = bytes[pos];
uint64_t value = (uint64_t) ((uint8_t)byte - (uint8_t)'0');
result += value * numerical_place;
if (pos == 0) break;
numerical_place *= 10;
pos -= 1;
}
return result;
}
// __TextFormatU64__ (utoa utos)
// # __TextFormatU64_do__
void __TextFormatU64_do__(
struct __TextFormatU64__ *t,
uint64_t number,
size_t min_width
) {
|
| ︙ | | | ︙ | |
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
|
size_t __Text_formatU64__(__WriteFnPtrVoid__ fn_write, void* context, uint64_t number, size_t min_width) {
struct __TextFormatU64__ format;
__TextFormatU64_do__(&format, number, min_width);
fn_write(context, format.ptr, format.len);
return format.len;
}
// Base64
uint32_t Base64_getEncodeSize(uint32_t src_len) {
int blocks = ceilDiv32(src_len, 3);
return blocks * 4;
}
void Base64_encode3Bytes(unsigned char in[3], char out[4]) {
out[0] = __Base64StandardAlphabet_chars__[(in[0] & 0b11111100) >> 2];
out[1] = __Base64StandardAlphabet_chars__[((in[0] & 0b00000011) << 4) | ((in[1] & 0b11110000) >> 4)];
out[2] = __Base64StandardAlphabet_chars__[((in[1] & 0b00001111) << 2) | ((in[2] & 0b11000000) >> 6)];
out[3] = __Base64StandardAlphabet_chars__[(in[2] & 0b00111111)];
}
bool test_rot() {
char a = 0b11111100 >> 2;
if (a == 0b00111111) {
return true;
} else {
printf("%d: failed test_rot.", __LINE__);
}
return false;
}
void Base64_encode(char* src, size_t src_len, char* out) {
// EXAMPLE
// Source ASCII text: "Ma"
// Character M a
// Octets 77 (0x4d) 97 (0x61)
// Bits 0 1 0 0 1 1 0 1|0 1 1 0 0 0 0 1|0 0 _ _ _ _ _ _|
// Base64 0 1 0 0 1 1|0 1 0 1 1 0|0 0 0 1 0 0|_ _ _ _ _ _|
|
>
|
|
|
|
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
|
size_t __Text_formatU64__(__WriteFnPtrVoid__ fn_write, void* context, uint64_t number, size_t min_width) {
struct __TextFormatU64__ format;
__TextFormatU64_do__(&format, number, min_width);
fn_write(context, format.ptr, format.len);
return format.len;
}
// Base64
// # Base64_getEncodeSize
uint32_t Base64_getEncodeSize(uint32_t src_len) {
int blocks = ceilDiv32(src_len, 3);
return blocks * 4;
}
// # Base64_encode3Bytes
void Base64_encode3Bytes(unsigned char in[3], char out[4]) {
out[0] = __Base64StandardAlphabet_chars__[(in[0] & 0b11111100) >> 2];
out[1] = __Base64StandardAlphabet_chars__[((in[0] & 0b00000011) << 4) | ((in[1] & 0b11110000) >> 4)];
out[2] = __Base64StandardAlphabet_chars__[((in[1] & 0b00001111) << 2) | ((in[2] & 0b11000000) >> 6)];
out[3] = __Base64StandardAlphabet_chars__[(in[2] & 0b00111111)];
}
// # test_rot
bool test_rot() {
char a = 0b11111100 >> 2;
if (a == 0b00111111) {
return true;
} else {
printf("%d: failed test_rot.", __LINE__);
}
return false;
}
// # Base64_encode
void Base64_encode(char* src, size_t src_len, char* out) {
// EXAMPLE
// Source ASCII text: "Ma"
// Character M a
// Octets 77 (0x4d) 97 (0x61)
// Bits 0 1 0 0 1 1 0 1|0 1 1 0 0 0 0 1|0 0 _ _ _ _ _ _|
// Base64 0 1 0 0 1 1|0 1 0 1 1 0|0 0 0 1 0 0|_ _ _ _ _ _|
|
| ︙ | | | ︙ | |
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
|
size_t pads = 3 - extra_bytes;
for (char* pad = out - pads ;pad < out; pad++) {*pad = '=';}
}
}
// HEX
#define bytesLenToHexLen(len) (len * 2)
#define hexLenToBytesLen(len) (len >> 1)
void printHex(char* buffer_ptr, uintptr_t buffer_len) {
for (int i = 0; i < buffer_len; i++) {
printf(" %02X ", buffer_ptr[i]);
}
}
// parse
|
|
|
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
|
size_t pads = 3 - extra_bytes;
for (char* pad = out - pads ;pad < out; pad++) {*pad = '=';}
}
}
// HEX
#define bytesLenToHexLen(len) (len * 2)
#define hexLenToBytesLen(len) (len >> 1)
// # printHex
void printHex(char* buffer_ptr, uintptr_t buffer_len) {
for (int i = 0; i < buffer_len; i++) {
printf(" %02X ", buffer_ptr[i]);
}
}
// parse
|
| ︙ | | | ︙ | |
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
|
// expected hex.len is bytesLenToHexLen(len)
for(size_t pos = 0; pos < data_len; pos++) {
Hex_packByte_H(data[pos], &hex[pos * 2]);
}
}
// AP
// pack
size_t __FormatsAp_packNibble__(char* buffer, char data) {
if (data <= 16) {
*buffer = 'A' + data;
}
return 1;
}
size_t __FormatsAp_packByte__(
char* buffer,
char byte
) {
size_t writed = 0;
writed += __FormatsAp_packNibble__(&buffer[writed], (byte & 0xF0) >> 4);
writed += __FormatsAp_packNibble__(&buffer[writed], byte & 0x0F);
return writed;
}
size_t __FormatsAp_packBytes_calculateLen__(size_t len) {
return len * 2;
}
size_t __FormatsAp_packBytes__(
char* buffer,
char* data,
size_t len
) {
size_t writed = 0;
for(size_t pos = 0; pos < len; pos++) {
writed += __FormatsAp_packByte__(&buffer[writed], data[pos]);
}
return writed;
}
size_t __FormatsAp_writeToBuffer__(char buffer[], const char data[], size_t len) {
size_t writed = 0;
for(size_t pos = 0; pos < len; pos++) {
writed += __FormatsAp_packByte__(&buffer[writed], data[pos]);
}
return writed;
}
// parse
size_t formats_AP_parseNibble(char* buffer, char data) {
if (data >= 'A' && data <= 'P') {
*buffer = data - 'A';
} else if (data >= 'a' && data <= 'p') {
*buffer = data - 'a';
} else {
*buffer = 0;
}
return 1;
}
size_t formats_AP_parseByteToBuffer(char out_buffer[], const char* data) {
char buffer[2];
size_t writed = 0;
writed += formats_AP_parseNibble(&buffer[writed], data[0]);
writed += formats_AP_parseNibble(&buffer[writed], data[1]);
*out_buffer = (buffer[0] << 4) | buffer[1];
return 1;
}
size_t formats_AP_parseToBuffer(char buffer[], const char *ap, size_t ap_len) {
size_t writed = 0;
size_t readed = 0;
while(readed < ap_len) {
writed += formats_AP_parseByteToBuffer(&buffer[writed], &ap[readed]);
readed += 2;
}
|
>
|
|
|
|
>
|
|
|
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
|
// expected hex.len is bytesLenToHexLen(len)
for(size_t pos = 0; pos < data_len; pos++) {
Hex_packByte_H(data[pos], &hex[pos * 2]);
}
}
// AP
// pack
// # __FormatsAp_packNibble__
size_t __FormatsAp_packNibble__(char* buffer, char data) {
if (data <= 16) {
*buffer = 'A' + data;
}
return 1;
}
// # __FormatsAp_packByte__
size_t __FormatsAp_packByte__(
char* buffer,
char byte
) {
size_t writed = 0;
writed += __FormatsAp_packNibble__(&buffer[writed], (byte & 0xF0) >> 4);
writed += __FormatsAp_packNibble__(&buffer[writed], byte & 0x0F);
return writed;
}
// # __FormatsAp_packBytes_calculateLen__
size_t __FormatsAp_packBytes_calculateLen__(size_t len) {
return len * 2;
}
// # __FormatsAp_packBytes__
size_t __FormatsAp_packBytes__(
char* buffer,
char* data,
size_t len
) {
size_t writed = 0;
for(size_t pos = 0; pos < len; pos++) {
writed += __FormatsAp_packByte__(&buffer[writed], data[pos]);
}
return writed;
}
// # __FormatsAp_writeToBuffer__
size_t __FormatsAp_writeToBuffer__(char buffer[], const char data[], size_t len) {
size_t writed = 0;
for(size_t pos = 0; pos < len; pos++) {
writed += __FormatsAp_packByte__(&buffer[writed], data[pos]);
}
return writed;
}
// parse
// # formats_AP_parseNibble
size_t formats_AP_parseNibble(char* buffer, char data) {
if (data >= 'A' && data <= 'P') {
*buffer = data - 'A';
} else if (data >= 'a' && data <= 'p') {
*buffer = data - 'a';
} else {
*buffer = 0;
}
return 1;
}
// # formats_AP_parseByteToBuffer
size_t formats_AP_parseByteToBuffer(char out_buffer[], const char* data) {
char buffer[2];
size_t writed = 0;
writed += formats_AP_parseNibble(&buffer[writed], data[0]);
writed += formats_AP_parseNibble(&buffer[writed], data[1]);
*out_buffer = (buffer[0] << 4) | buffer[1];
return 1;
}
// # formats_AP_parseToBuffer
size_t formats_AP_parseToBuffer(char buffer[], const char *ap, size_t ap_len) {
size_t writed = 0;
size_t readed = 0;
while(readed < ap_len) {
writed += formats_AP_parseByteToBuffer(&buffer[writed], &ap[readed]);
readed += 2;
}
|
| ︙ | | | ︙ | |
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
|
for (size_t pos = 0; pos < dest_len; pos++) {
dest[pos] = a[pos] ^ b[ci];
ci += 1;
if (ci == b_len) ci = 0;
}
}
// __Logger__
void __Logger_init__ (struct __Logger__* t) {
for (int i = 0; i < __arrayLen__(t->writers); i++) {
struct __Writer__* w = &t->writers[i];
w->context = 0;
w->write = 0;
}
}
void __Logger_writeToSelf__(struct __Logger__* t, const char bytes[], size_t len) {
for (size_t i = 0; i < __arrayLen__(t->writers); i++) {
struct __Writer__* w = &t->writers[i];
if (w->write) {
size_t writed = w->write(w->context, bytes, len);
}
}
|
>
|
|
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
|
for (size_t pos = 0; pos < dest_len; pos++) {
dest[pos] = a[pos] ^ b[ci];
ci += 1;
if (ci == b_len) ci = 0;
}
}
// __Logger__
// # __Logger_init__
void __Logger_init__ (struct __Logger__* t) {
for (int i = 0; i < __arrayLen__(t->writers); i++) {
struct __Writer__* w = &t->writers[i];
w->context = 0;
w->write = 0;
}
}
// # __Logger_writeToSelf__
void __Logger_writeToSelf__(struct __Logger__* t, const char bytes[], size_t len) {
for (size_t i = 0; i < __arrayLen__(t->writers); i++) {
struct __Writer__* w = &t->writers[i];
if (w->write) {
size_t writed = w->write(w->context, bytes, len);
}
}
|
| ︙ | | | ︙ | |
2956
2957
2958
2959
2960
2961
2962
|
#ifndef __COMPILED_FROM_ZIG_FILE__
int main(int args_len, char** args_ptr) {
trace_example();
if (real_main(args_len, args_ptr)) return 0;
return 1;
}
#endif
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
|
#ifndef __COMPILED_FROM_ZIG_FILE__
int main(int args_len, char** args_ptr) {
trace_example();
if (real_main(args_len, args_ptr)) return 0;
return 1;
}
#endif
// TODO port this zig code to C
/*
const Utf8 = struct {
const BytesIterator = struct {
const Parsed = struct {
num: u64 = 0,
bytes: []const u8 = &.{},
fn is_connector(t: *Parsed) bool {
if (t.num == 0x200D) return true;
return false;
}
};
const Iterator = struct {
bytes: []const u8,
pos: usize = 0,
fn next(t: *Iterator) ?Parsed {
// Masks:
//| 1 byte | 2 byte | 3 byte | 4 byte
//
// 0xxxxxxx
// 110xxxxx 10xxxxxx
// 1110xxxx 10xxxxxx 10xxxxxx
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// example "?"
// 208 -- 110 10000
// 159 -- 10 011111
// is
// 1055 -- 000 10000011111
parse: {
var parsed: Parsed = .{};
// 1 byte
if (t.bytes.len <= t.pos) break :parse;
const b1 = t.bytes[t.pos];
const c1: u64 = @intCast(b1);
if (checkMask(b1, 0, 0b10000000)) { // 0xxxxxxx
parsed.num = b1 & 0b01111111;
parsed.bytes = t.bytes[t.pos .. t.pos + 1];
t.pos += 1;
return parsed;
}
// 2 byte
if (t.bytes.len <= t.pos + 1) break :parse;
const b2 = t.bytes[t.pos + 1];
const c2: u64 = @intCast(b2);
if (checkMask(b1, 0b11000000, 0b11100000)) { // 110xxxxx
parsed.num = ((c1 & 31) << 6) + (c2 & 63);
parsed.bytes = t.bytes[t.pos .. t.pos + 2];
t.pos += 2;
return parsed;
}
// 3 byte
if (t.bytes.len <= t.pos + 2) break :parse;
const b3 = t.bytes[t.pos + 2];
const c3: u64 = @intCast(b3);
if (checkMask(b1, 0b11100000, 0b11110000)) { // 1110xxxx
parsed.num = ((c1 & 15) << 12) + ((c2 & 63) << 6) + (c3 & 63);
parsed.bytes = t.bytes[t.pos .. t.pos + 3];
t.pos += 3;
return parsed;
}
// 4 byte
if (t.bytes.len <= t.pos + 3) break :parse;
const b4 = t.bytes[t.pos + 3];
const c4: u64 = @intCast(b4);
if (checkMask(b1, 0b11110000, 0b11111000)) { // 11110xxx
parsed.num = ((c1 & 7) << 21) + ((c2 & 63) << 12) + ((c3 & 63) << 6) + (c4 & 63);
parsed.bytes = t.bytes[t.pos .. t.pos + 4];
t.pos += 4;
return parsed;
}
}
t.pos += 1;
return null;
}
};
fn checkMask(num: u8, mask: u8, used_bits: u8) bool {
if (num & used_bits == mask) {
return true;
} else {
return false;
}
}
fn fromDo(bytes: []const u8) Iterator {
return Iterator{ .bytes = bytes };
}
};
};
*/
|