Repository navigation
Expand file tree
/
Copy pathLObjectC.cs
More file actions
655 lines (618 loc) · 23.3 KB
/
Copy pathLObjectC.cs
File metadata and controls
655 lines (618 loc) · 23.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Port of lobject.c - some generic functions over Lua objects. The type
// definitions of lobject.h live in LObject.cs. See LICENSE_LUA.txt.
//
// The variadic functions of the C sources ('luaO_pushfstring' and friends)
// take a 'params ReadOnlySpan<LuaArg>' here: a stack-allocated argument list
// with implicit conversions, so call sites read like the C ones.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace LinkBridge.Lua;
// One argument of a Lua format string. The format specifier decides how the
// value is read, exactly as 'va_arg' does in C.
[StructLayout(LayoutKind.Auto)]
public unsafe struct LuaArg
{
public long i;
public double n;
public void* p;
public static implicit operator LuaArg(int v) => new LuaArg { i = v };
public static implicit operator LuaArg(uint v) => new LuaArg { i = v };
public static implicit operator LuaArg(long v) => new LuaArg { i = v };
public static implicit operator LuaArg(ulong v) => new LuaArg { i = (long)v };
public static implicit operator LuaArg(nuint v) => new LuaArg { i = (long)v };
public static implicit operator LuaArg(nint v) => new LuaArg { i = v };
public static implicit operator LuaArg(double v) => new LuaArg { n = v };
public static implicit operator LuaArg(byte* v) => new LuaArg { p = v };
public static implicit operator LuaArg(void* v) => new LuaArg { p = v };
}
public static unsafe partial class LuaApi
{
/*
** Computes ceil(log2(x)), which is the smallest integer n such that
** x <= (1 << n).
*/
public static lu_byte luaO_ceillog2(uint x)
{
// log_2[i - 1] = ceil(log2(i))
ReadOnlySpan<byte> log_2 = new byte[]
{
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
int l = 0;
x--;
while (x >= 256) { l += 8; x >>= 8; }
return (lu_byte)(l + log_2[(int)x]);
}
/*
** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx).
*/
public static lu_byte luaO_codeparam(uint p)
{
if (p >= ((lu_mem)0x1F << (0xF - 7 - 1)) * 100u) // overflow?
return 0xFF; // return maximum value
else
{
p = (p * 128 + 99) / 100; // round up the division
if (p < 0x10)
{ // subnormal number?
// exponent bits are already zero; nothing else to do
return (lu_byte)p;
}
else
{ // p >= 0x10 implies ceil(log2(p + 1)) >= 5
// preserve 5 bits in 'p'
uint log = luaO_ceillog2(p + 1) - 5u;
return (lu_byte)(((p >> (int)log) - 0x10) | ((log + 1) << 4));
}
}
}
/*
** Computes 'p' times 'x', where 'p' is a floating-point byte.
*/
public static l_mem luaO_applyparam(lu_byte p, l_mem x)
{
int m = p & 0xF; // mantissa
int e = p >> 4; // exponent
if (e > 0)
{ // normalized?
e--; // correct exponent
m += 0x10; // correct mantissa; maximum value is 0x1F
}
e -= 7; // correct excess-7
if (e >= 0)
{
if (x < (MAX_LMEM / 0x1F) >> e) // no overflow?
return (x * m) << e; // order doesn't matter here
else // real overflow
return MAX_LMEM;
}
else
{ // negative exponent
e = -e;
if (x < MAX_LMEM / 0x1F) // multiplication cannot overflow?
return (x * m) >> e; // multiplying first gives more precision
else if ((x >> e) < MAX_LMEM / 0x1F) // cannot overflow after shift?
return (x >> e) * m;
else // real overflow
return MAX_LMEM;
}
}
private static lua_Integer intarith(lua_State* L, int op, lua_Integer v1, lua_Integer v2)
{
switch (op)
{
case LUA_OPADD: return intop_add(v1, v2);
case LUA_OPSUB: return intop_sub(v1, v2);
case LUA_OPMUL: return intop_mul(v1, v2);
case LUA_OPMOD: return luaV_mod(L, v1, v2);
case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
case LUA_OPBAND: return l_castU2S(l_castS2U(v1) & l_castS2U(v2));
case LUA_OPBOR: return l_castU2S(l_castS2U(v1) | l_castS2U(v2));
case LUA_OPBXOR: return l_castU2S(l_castS2U(v1) ^ l_castS2U(v2));
case LUA_OPSHL: return luaV_shiftl(v1, v2);
case LUA_OPSHR: return luaV_shiftr(v1, v2);
case LUA_OPUNM: return intop_sub(0, v1);
case LUA_OPBNOT: return l_castU2S(~l_castS2U(0) ^ l_castS2U(v1));
default: lua_assert(false); return 0;
}
}
private static lua_Number numarith(lua_State* L, int op, lua_Number v1, lua_Number v2)
{
switch (op)
{
case LUA_OPADD: return luai_numadd(L, v1, v2);
case LUA_OPSUB: return luai_numsub(L, v1, v2);
case LUA_OPMUL: return luai_nummul(L, v1, v2);
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
case LUA_OPPOW: return luai_numpow(L, v1, v2);
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
case LUA_OPUNM: return luai_numunm(L, v1);
case LUA_OPMOD: return luaV_modf(L, v1, v2);
default: lua_assert(false); return 0;
}
}
public static int luaO_rawarith(lua_State* L, int op, TValue* p1, TValue* p2, TValue* res)
{
switch (op)
{
case LUA_OPBAND:
case LUA_OPBOR:
case LUA_OPBXOR:
case LUA_OPSHL:
case LUA_OPSHR:
case LUA_OPBNOT:
{ // operate only on integers
lua_Integer i1;
lua_Integer i2;
if (tointegerns(p1, &i1) && tointegerns(p2, &i2))
{
setivalue(res, intarith(L, op, i1, i2));
return 1;
}
else return 0; // fail
}
case LUA_OPDIV:
case LUA_OPPOW:
{ // operate only on floats
lua_Number n1;
lua_Number n2;
if (tonumberns(p1, out n1) && tonumberns(p2, out n2))
{
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; // fail
}
default:
{ // other operations
lua_Number n1;
lua_Number n2;
if (ttisinteger(p1) && ttisinteger(p2))
{
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
return 1;
}
else if (tonumberns(p1, out n1) && tonumberns(p2, out n2))
{
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; // fail
}
}
}
public static void luaO_arith(lua_State* L, int op, TValue* p1, TValue* p2, StkId res)
{
if (luaO_rawarith(L, op, p1, p2, s2v(res)) == 0)
{
// could not perform raw operation; try metamethod
luaT_trybinTM(L, p1, p2, res, (op - LUA_OPADD) + TM_ADD);
}
}
public static lu_byte luaO_hexavalue(int c)
{
lua_assert(lisxdigit(c));
if (lisdigit(c)) return (lu_byte)(c - '0');
else return (lu_byte)((ltolower(c) - 'a') + 10);
}
private static int isneg(byte** s)
{
if (**s == '-') { (*s)++; return 1; }
else if (**s == '+') (*s)++;
return 0;
}
// maximum length of a numeral to be converted to a number
private const int L_MAXLENNUM = 200;
/*
** Convert string 's' to a Lua number (put in 'result'). Return NULL on
** fail or the address of the ending '\0' on success. ('mode' == 'x')
** means a hexadecimal numeral.
*/
private static byte* l_str2dloc(byte* s, lua_Number* result, int mode)
{
byte* endptr;
// 'strtod' of this port reads both decimal and hexadecimal numerals
*result = strtod(s, &endptr);
if (endptr == s) return null; // nothing recognized?
while (lisspace(*endptr)) endptr++; // skip trailing spaces
return *endptr == 0 ? endptr : null; // OK iff no trailing chars
}
/*
** Convert string 's' to a Lua number (put in 'result'). The port has no
** locale support, so the radix mark is always a dot; the check for the
** locale radix mark of the C sources is not needed here.
** The variable 'mode' checks for special characters in the string:
** - 'n' means 'inf' or 'nan' (which should be rejected)
** - 'x' means a hexadecimal numeral
*/
private static byte* l_str2d(byte* s, lua_Number* result)
{
byte* pmode = strpbrk(s, CSTR(".xXnN"u8)); // look for special chars
int mode = pmode != null ? ltolower(*pmode) : 0;
if (mode == 'n') // reject 'inf' and 'nan'
return null;
return l_str2dloc(s, result, mode); // try to convert
}
private static readonly lua_Unsigned MAXBY10 = (lua_Unsigned)(LUA_MAXINTEGER / 10);
private const int MAXLASTD = (int)(LUA_MAXINTEGER % 10);
private static byte* l_str2int(byte* s, lua_Integer* result)
{
lua_Unsigned a = 0;
int empty = 1;
int neg;
while (lisspace(*s)) s++; // skip initial spaces
neg = isneg(&s);
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
{ // hex?
s += 2; // skip '0x'
for (; lisxdigit(*s); s++)
{
a = a * 16 + luaO_hexavalue(*s);
empty = 0;
}
}
else
{ // decimal
for (; lisdigit(*s); s++)
{
int d = *s - '0';
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) // overflow?
return null; // do not accept it (as integer)
a = a * 10 + (uint)d;
empty = 0;
}
}
while (lisspace(*s)) s++; // skip trailing spaces
if (empty != 0 || *s != 0) return null; // something wrong in the numeral
else
{
*result = l_castU2S(neg != 0 ? 0u - a : a);
return s;
}
}
public static nuint luaO_str2num(byte* s, TValue* o)
{
lua_Integer i;
lua_Number n;
byte* e;
if ((e = l_str2int(s, &i)) != null)
{ // try as an integer
setivalue(o, i);
}
else if ((e = l_str2d(s, &n)) != null)
{ // else try as a float
setfltvalue(o, n);
}
else
return 0; // conversion failed
return ct_diff2sz((nint)(e - s)) + 1; // success; return string size
}
public static int luaO_utf8esc(byte* buff, l_uint32 x)
{
int n = 1; // number of bytes put in buffer (backwards)
lua_assert(x <= 0x7FFFFFFFu);
if (x < 0x80) // ASCII?
buff[UTF8BUFFSZ - 1] = (byte)x;
else
{ // need continuation bytes
uint mfb = 0x3f; // maximum that fits in first byte
do
{ // add continuation bytes
buff[UTF8BUFFSZ - (n++)] = (byte)(0x80 | (x & 0x3f));
x >>= 6; // remove added bits
mfb >>= 1; // now there is one less bit available in first byte
} while (x > mfb); // still needs continuation byte?
buff[UTF8BUFFSZ - n] = (byte)((~mfb << 1) | x); // add first byte
}
return n;
}
/*
** Convert a float to a string, adding it to a buffer.
*/
private static int tostringbuffFloat(lua_Number n, byte* buff)
{
// first conversion
int len = fmt_g(buff, LUA_N2SBUFFSZ, n, 15, false);
lua_Number check = strtod(buff, null); // read it back
if (check != n)
{ // not enough precision?
// convert again with more precision
len = fmt_g(buff, LUA_N2SBUFFSZ, n, 17, false);
}
// looks like an integer?
if (buff[strspn(buff, CSTR("-0123456789"u8))] == 0)
{
buff[len++] = (byte)'.';
buff[len++] = (byte)'0'; // adds '.0' to result
}
return len;
}
/*
** Convert a number object to a string, adding it to a buffer.
*/
public static uint luaO_tostringbuff(TValue* obj, byte* buff)
{
int len;
lua_assert(ttisnumber(obj));
if (ttisinteger(obj))
len = fmt_d(buff, LUA_N2SBUFFSZ, ivalue(obj));
else
len = tostringbuffFloat(fltvalue(obj), buff);
lua_assert(len < LUA_N2SBUFFSZ);
return (uint)len;
}
/*
** Convert a number object to a Lua string, replacing the value at 'obj'
*/
public static void luaO_tostring(lua_State* L, TValue* obj)
{
byte* buff = stackalloc byte[LUA_N2SBUFFSZ];
uint len = luaO_tostringbuff(obj, buff);
setsvalue(L, obj, luaS_newlstr(L, buff, len));
}
/*
** {==================================================================
** 'luaO_pushvfstring'
** ===================================================================
*/
// Size for buffer space used by 'luaO_pushvfstring'.
private const int BUFVFS = LUA_IDSIZE + LUA_N2SBUFFSZ + 95;
/*
** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while
** building result (memory error [1] or buffer overflow [2]).
*/
private struct BuffFS
{
public lua_State* L;
public byte* b;
public nuint buffsize;
public nuint blen; // length of string in 'buff'
public int err;
public fixed byte space[BUFVFS]; // initial buffer
}
private static void initbuff(lua_State* L, BuffFS* buff)
{
buff->L = L;
buff->b = buff->space;
buff->buffsize = BUFVFS;
buff->blen = 0;
buff->err = 0;
}
/*
** Push final result from 'luaO_pushvfstring'.
*/
private static void pushbuff(lua_State* L, void* ud)
{
BuffFS* buff = (BuffFS*)ud;
switch (buff->err)
{
case 1: // memory error
luaD_throw(L, LUA_ERRMEM);
break;
case 2: // length overflow: Add "..." at the end of result
if (buff->buffsize - buff->blen < 3)
strcpy(buff->b + buff->blen - 3, CSTR("..."u8)); // 'blen' must be > 3
else
{ // there is enough space left for the "..."
strcpy(buff->b + buff->blen, CSTR("..."u8));
buff->blen += 3;
}
goto default; // FALLTHROUGH
default:
{ // no errors, but it can raise one creating the new string
TString* ts = luaS_newlstr(L, buff->b, buff->blen);
setsvalue2s(L, L->top.p, ts);
L->top.p++;
break;
}
}
}
private static byte* clearbuff(BuffFS* buff)
{
lua_State* L = buff->L;
byte* res;
if (luaD_rawrunprotected(L, &pushbuff, buff) != LUA_OK) // errors?
res = null; // error message is on the top of the stack
else
res = getstr(tsvalue(s2v(L->top.p - 1)));
if (buff->b != buff->space) // using dynamic buffer?
luaM_freearray(L, buff->b, buff->buffsize); // free it
return res;
}
private static void addstr2buff(BuffFS* buff, byte* str, nuint slen)
{
nuint left = buff->buffsize - buff->blen; // space left in the buffer
if (buff->err != 0) // do nothing else after an error
return;
if (slen > left)
{ // new string doesn't fit into current buffer?
if (slen > (MAX_SIZE / 2) - buff->blen)
{ // overflow?
memcpy(buff->b + buff->blen, str, left); // copy what it can
buff->blen = buff->buffsize;
buff->err = 2; // doesn't add anything else
return;
}
else
{
nuint newsize = buff->buffsize + slen; // limited to MAX_SIZE/2
byte* newb =
buff->b == buff->space // still using static space?
? (byte*)luaM_realloc_(buff->L, null, 0, newsize)
: (byte*)luaM_realloc_(buff->L, buff->b, buff->buffsize, newsize);
if (newb == null)
{ // allocation error?
buff->err = 1; // signal a memory error
return;
}
if (buff->b == buff->space) // new buffer (not reallocated)?
memcpy(newb, buff->b, buff->blen); // copy previous content
buff->b = newb; // set new (larger) buffer...
buff->buffsize = newsize; // ...and its new size
}
}
memcpy(buff->b + buff->blen, str, slen); // copy new content
buff->blen += slen;
}
/*
** Add a numeral to the buffer.
*/
private static void addnum2buff(BuffFS* buff, TValue* num)
{
byte* numbuff = stackalloc byte[LUA_N2SBUFFSZ];
uint len = luaO_tostringbuff(num, numbuff);
addstr2buff(buff, numbuff, len);
}
/*
** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
** conventional formats, plus Lua-specific '%I' and '%U'
*/
public static byte* luaO_pushvfstring(lua_State* L, byte* fmt, ReadOnlySpan<LuaArg> argp)
{
BuffFS buff; // holds last part of the result
byte* e; // points to next '%'
int arg = 0;
initbuff(L, &buff);
while ((e = strchr(fmt, '%')) != null)
{
addstr2buff(&buff, fmt, ct_diff2sz((nint)(e - fmt))); // add 'fmt' up to '%'
switch (*(e + 1))
{ // conversion specifier
case (byte)'s':
{ // zero-terminated string
byte* s = (byte*)argp[arg++].p;
if (s == null) s = CSTR("(null)"u8);
addstr2buff(&buff, s, strlen(s));
break;
}
case (byte)'c':
{ // an 'int' as a character
byte c = (byte)argp[arg++].i;
addstr2buff(&buff, &c, 1);
break;
}
case (byte)'d':
{ // an 'int'
TValue num;
setivalue(&num, (int)argp[arg++].i);
addnum2buff(&buff, &num);
break;
}
case (byte)'I':
{ // a 'lua_Integer'
TValue num;
setivalue(&num, argp[arg++].i);
addnum2buff(&buff, &num);
break;
}
case (byte)'f':
{ // a 'lua_Number'
TValue num;
setfltvalue(&num, argp[arg++].n);
addnum2buff(&buff, &num);
break;
}
case (byte)'p':
{ // a pointer
byte* bf = stackalloc byte[LUA_N2SBUFFSZ]; // enough space for '%p'
void* p = argp[arg++].p;
int len = fmt_p(bf, LUA_N2SBUFFSZ, p);
addstr2buff(&buff, bf, (uint)len);
break;
}
case (byte)'U':
{ // an 'unsigned long' as a UTF-8 sequence
byte* bf = stackalloc byte[UTF8BUFFSZ];
ulong a = (ulong)argp[arg++].i;
int len = luaO_utf8esc(bf, (l_uint32)a);
addstr2buff(&buff, bf + UTF8BUFFSZ - len, (uint)len);
break;
}
case (byte)'%':
{
addstr2buff(&buff, CSTR("%"u8), 1);
break;
}
default:
{
addstr2buff(&buff, e, 2); // keep unknown format in the result
break;
}
}
fmt = e + 2; // skip '%' and the specifier
}
addstr2buff(&buff, fmt, strlen(fmt)); // rest of 'fmt'
return clearbuff(&buff); // empty buffer into a new string
}
public static byte* luaO_pushfstring(lua_State* L, byte* fmt, params ReadOnlySpan<LuaArg> argp)
{
byte* msg = luaO_pushvfstring(L, fmt, argp);
if (msg == null) // error?
luaD_throw(L, LUA_ERRMEM);
return msg;
}
/*
** }==================================================================
*/
private static ReadOnlySpan<byte> RETS => "..."u8;
private static ReadOnlySpan<byte> PRE => "[string \""u8;
private static ReadOnlySpan<byte> POS => "\"]"u8;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void addstr(ref byte* a, byte* b, nuint l)
{
memcpy(a, b, l);
a += l;
}
public static void luaO_chunkid(byte* @out, byte* source, nuint srclen)
{
nuint bufflen = LUA_IDSIZE; // free space in buffer
if (*source == '=')
{ // 'literal' source
if (srclen <= bufflen) // small enough?
memcpy(@out, source + 1, srclen);
else
{ // truncate it
addstr(ref @out, source + 1, bufflen - 1);
*@out = 0;
}
}
else if (*source == '@')
{ // file name
if (srclen <= bufflen) // small enough?
memcpy(@out, source + 1, srclen);
else
{ // add '...' before rest of name
addstr(ref @out, CSTR(RETS), (nuint)RETS.Length);
bufflen -= (nuint)RETS.Length;
memcpy(@out, source + 1 + srclen - bufflen, bufflen);
}
}
else
{ // string; format as [string "source"]
byte* nl = strchr(source, '\n'); // find first new line (if any)
addstr(ref @out, CSTR(PRE), (nuint)PRE.Length); // add prefix
// save space for prefix+suffix+'\0'
bufflen -= (nuint)(PRE.Length + RETS.Length + POS.Length) + 1;
if (srclen < bufflen && nl == null)
{ // small one-line source?
addstr(ref @out, source, srclen); // keep it
}
else
{
if (nl != null)
srclen = ct_diff2sz((nint)(nl - source)); // stop at first newline
if (srclen > bufflen) srclen = bufflen;
addstr(ref @out, source, srclen);
addstr(ref @out, CSTR(RETS), (nuint)RETS.Length);
}
memcpy(@out, CSTR(POS), (nuint)POS.Length + 1);
}
}
}