-
Notifications
You must be signed in to change notification settings - Fork 57
/
eel_strings.h
1666 lines (1505 loc) · 49.8 KB
/
eel_strings.h
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __EEL__STRINGS_H__
#define __EEL__STRINGS_H__
#include "ns-eel-int.h"
#include "../wdlcstring.h"
#include "../wdlstring.h"
// required for context
// #define EEL_STRING_GET_CONTEXT_POINTER(opaque) (((sInst *)opaque)->m_eel_string_state)
/*
// writeable user-strings are 0..1023 (EEL_STRING_MAX_USER_STRINGS-1), and can be up to about EEL_STRING_MAXUSERSTRING_LENGTH_HINT bytes long
printf("string %d blah"); -- output to log, allows %d %u %f etc, if host implements formats [1]
sprintf(str,"string %d blah"); -- output to str [1]
strlen(str); -- returns string length
match("*test*", "this is a test") -- search for first parameter regex-style in second parameter
matchi("*test*", "this is a test") -- search for first parameter regex-style in second parameter (case insensitive)
// %s means 1 or more chars
// %0s means 0 or more chars
// %5s means exactly 5 chars
// %5-s means 5 or more chars
// %-10s means 1-10 chars
// %3-5s means 3-5 chars.
// %0-5s means 0-5 chars.
// %S (uppercase) indicates lazy match (%D, %F, %X, etc)
strcpy(str, srcstr); -- replaces str with srcstr
strcat(str, srcstr); -- appends srcstr to str
strcmp(str, str2) -- compares strings
stricmp(str, str2) -- compares strings (ignoring case)
strncmp(str, str2, maxlen) -- compares strings up to maxlen bytes
strnicmp(str, str2, maxlen) -- compares strings (ignoring case) up to maxlen bytes
strncpy(str, srcstr, maxlen); -- replaces str with srcstr, up to maxlen (-1 for unlimited)
strncat(str, srcstr, maxlen); -- appends up to maxlen of srcstr to str (-1 for unlimited)
strcpy_from(str,srcstr, offset); -- copies srcstr to str, but starts reading srcstr at offset offset
strcpy_substr(str, srcstr, offs, ml) -- php-style (start at offs, offs<0 means from end, ml for maxlen, ml<0 = reduce length by this amt)
str_getchar(str, offset[, type]); -- returns value at offset offset, type can be omitted or 0 or 'c', 's', 'S', 'i', 'I', 'f', 'F', 'd', 'D', 'uc', 'us', 'US', 'ui', 'UI'
-- negative offset is offset from end of string
str_setchar(str, offset, val[, type]); - sets value at offset offset, type optional. offset must be [0,length], if length it can lengthen string, if >length then call fails
-- negative offset is offset from end of string
str_setlen(str, len); -- sets length of string (if increasing, will be space-padded)
str_delsub(str, pos, len); -- deletes len chars at pos
str_insert(str, srcstr, pos); -- inserts srcstr at pos
[1]: note: printf/sprintf are NOT binary safe when using %s with modifiers (such as %100s etc) -- the source string being formatted
will terminate at the first NULL character
);
*/
// define this to allow modifying literal strings via code, i.e. foobar = strcat("foo","bar");
// disabled by default, so literals can be pooled/reused/etc
// #define EEL_STRINGS_MUTABLE_LITERALS
#ifndef EEL_STRING_MAXUSERSTRING_LENGTH_HINT
#define EEL_STRING_MAXUSERSTRING_LENGTH_HINT 16384
#endif
#ifndef EEL_STRING_MAX_USER_STRINGS
// strings 0...x-1
#define EEL_STRING_MAX_USER_STRINGS 1024
#endif
#ifndef EEL_STRING_LITERAL_BASE
// strings defined by "xyz"
#define EEL_STRING_LITERAL_BASE 10000
#endif
// base for named mutable strings (#xyz)
#ifndef EEL_STRING_NAMED_BASE
#define EEL_STRING_NAMED_BASE 90000
#endif
// base for unnamed mutable strings (#)
#ifndef EEL_STRING_UNNAMED_BASE
#define EEL_STRING_UNNAMED_BASE 190000
#endif
// define EEL_STRING_MUTEXLOCK_SCOPE for custom, otherwise EEL_STRING_WANT_MUTEX for builtin locking
#ifndef EEL_STRING_MUTEXLOCK_SCOPE
#ifdef EEL_STRING_WANT_MUTEX
#include "../mutex.h"
#define EEL_STRING_MUTEXLOCK_SCOPE WDL_MutexLock __lock(&(EEL_STRING_GET_CONTEXT_POINTER(opaque)->m_mutex));
#else
#define EEL_STRING_MUTEXLOCK_SCOPE
#endif
#endif
// allow overriding behavior
#ifndef EEL_STRING_GET_FOR_INDEX
#define EEL_STRING_GET_FOR_INDEX(x, wr) (EEL_STRING_GET_CONTEXT_POINTER(opaque)->GetStringForIndex(x, wr, false))
#endif
#ifndef EEL_STRING_GET_FOR_WRITE
#define EEL_STRING_GET_FOR_WRITE(x, wr) (EEL_STRING_GET_CONTEXT_POINTER(opaque)->GetStringForIndex(x, wr, true))
#endif
#ifndef EEL_STRING_GETFMTVAR
#define EEL_STRING_GETFMTVAR(x) (EEL_STRING_GET_CONTEXT_POINTER(opaque)->GetVarForFormat(x))
#endif
#ifndef EEL_STRING_GETNAMEDVAR
#define EEL_STRING_GETNAMEDVAR(x,createOK,altOut) (EEL_STRING_GET_CONTEXT_POINTER(opaque)->GetNamedVar(x,createOK,altOut))
#endif
#ifndef EEL_STRING_STORAGECLASS
#define EEL_STRING_STORAGECLASS WDL_FastString
#endif
class eel_string_context_state
{
public:
eel_string_context_state() : m_named_strings_names(false), m_varname_cache(false, NULL, false)
{
m_vm=0;
memset(m_user_strings,0,sizeof(m_user_strings));
}
~eel_string_context_state()
{
clear_state(true);
}
void clear_state(bool full)
{
if (full)
{
int x;
for (x=0;x<EEL_STRING_MAX_USER_STRINGS;x++)
{
delete m_user_strings[x];
m_user_strings[x]=0;
}
m_named_strings_names.DeleteAll();
m_named_strings.Empty(true);
}
if (full) m_literal_strings.Empty(true);
m_varname_cache.DeleteAll();
m_unnamed_strings.Empty(true);
}
void update_named_vars(NSEEL_VMCTX vm) // call after compiling any code, or freeing code, etc
{
m_vm = vm;
m_varname_cache.DeleteAll();
if (vm) NSEEL_VM_enumallvars(vm,varEnumProc, this);
m_varname_cache.Resort();
}
EEL_F *GetVarForFormat(int formatidx)
{
return NULL; // must use %{xyz}s syntax -- override to change defaults
}
// if named variables are used, must call update_named_vars(vm) to set context/generate list
EEL_F *GetNamedVar(const char *s, bool createIfNotExists, EEL_F *altOut)
{
if (!s || !*s) return NULL;
if (s[0] == '#')
{
if (!s[1] || !altOut) return NULL;
int idx = m_named_strings_names.Get(s+1);
if (!idx && createIfNotExists)
{
idx = m_named_strings.GetSize() + EEL_STRING_NAMED_BASE;
m_named_strings.Add(new EEL_STRING_STORAGECLASS);
m_named_strings_names.Insert(s+1,idx);
}
if (!idx) return NULL;
*altOut = (EEL_F) idx;
return altOut;
}
EEL_F *r = m_varname_cache.Get(s);
if (r || !createIfNotExists || !m_vm) return r;
const char *p=NULL;
r=nseel_int_register_var((compileContext*)m_vm,s,0,&p);
if (r&&p) m_varname_cache.Insert(p,r);
return r;
}
const char *GetStringForIndex(EEL_F val, EEL_STRING_STORAGECLASS **stringContainerOut=NULL, bool is_for_write=false)
{
int idx = (int) (val+0.5);
if (idx>=0 && idx < EEL_STRING_MAX_USER_STRINGS)
{
if (stringContainerOut)
{
if (!m_user_strings[idx]) m_user_strings[idx] = new EEL_STRING_STORAGECLASS;
*stringContainerOut = m_user_strings[idx];
}
return m_user_strings[idx]?m_user_strings[idx]->Get():"";
}
EEL_STRING_STORAGECLASS *s;
s = m_unnamed_strings.Get(idx - EEL_STRING_UNNAMED_BASE);
if (!s) s= m_named_strings.Get(idx - EEL_STRING_NAMED_BASE);
if (s)
{
// mutable string
if (stringContainerOut) *stringContainerOut=s;
}
else
{
s = m_literal_strings.Get(idx - EEL_STRING_LITERAL_BASE);
#ifdef EEL_STRINGS_MUTABLE_LITERALS
if (stringContainerOut) *stringContainerOut=s;
#else
if (stringContainerOut) *stringContainerOut=is_for_write ? NULL : s;
#endif
}
return s ? s->Get() : NULL;
}
WDL_PtrList<EEL_STRING_STORAGECLASS> m_literal_strings; // "this kind", normally immutable
WDL_PtrList<EEL_STRING_STORAGECLASS> m_unnamed_strings; // #
WDL_PtrList<EEL_STRING_STORAGECLASS> m_named_strings; // #xyz by index, but stringkeyed below for names
WDL_StringKeyedArray<int> m_named_strings_names; // #xyz->index
EEL_STRING_STORAGECLASS *m_user_strings[EEL_STRING_MAX_USER_STRINGS]; // indices 0-1023 (etc)
WDL_StringKeyedArray<EEL_F_PTR> m_varname_cache; // cached pointers when using %{xyz}s, %{#xyz}s bypasses
NSEEL_VMCTX m_vm;
#ifdef EEL_STRING_WANT_MUTEX
WDL_Mutex m_mutex;
#endif
static EEL_F addNamedStringCallback(void *opaque, const char *name)
{
if (!opaque) return -1.0;
eel_string_context_state *_this = EEL_STRING_GET_CONTEXT_POINTER(opaque);
if (!_this) return -1.0;
#ifdef EEL_STRING_NAMEDSTRINGCALLBACK_HOOK
EEL_STRING_NAMEDSTRINGCALLBACK_HOOK
#endif
EEL_STRING_MUTEXLOCK_SCOPE
if (!name || !name[0])
{
_this->m_unnamed_strings.Add(new EEL_STRING_STORAGECLASS);
return (EEL_F) (_this->m_unnamed_strings.GetSize()-1 + EEL_STRING_UNNAMED_BASE);
}
int a = _this->m_named_strings_names.Get(name);
if (a) return (EEL_F)a;
a = _this->m_named_strings.GetSize() + EEL_STRING_NAMED_BASE;
_this->m_named_strings.Add(new EEL_STRING_STORAGECLASS);
_this->m_named_strings_names.Insert(name,a);
return (EEL_F)a;
}
static EEL_F addStringCallback(void *opaque, struct eelStringSegmentRec *list)
{
if (!opaque) return -1.0;
eel_string_context_state *_this = EEL_STRING_GET_CONTEXT_POINTER(opaque);
if (!_this) return -1.0;
EEL_STRING_STORAGECLASS *ns = new EEL_STRING_STORAGECLASS;
// could probably do a faster implementation using AddRaw() etc but this should also be OK
int sz=nseel_stringsegments_tobuf(NULL,0,list);
ns->SetLen(sz+32);
sz=nseel_stringsegments_tobuf((char *)ns->Get(),sz,list);
ns->SetLen(sz);
EEL_STRING_MUTEXLOCK_SCOPE
return (EEL_F)_this->AddString(ns);
}
int AddString(EEL_STRING_STORAGECLASS *ns)
{
#ifdef EEL_STRINGS_MUTABLE_LITERALS
m_literal_strings.Add(ns);
return m_literal_strings.GetSize()-1+EEL_STRING_LITERAL_BASE;
#else
const int l = ns->GetLength();
const int sz=m_literal_strings.GetSize();
int x;
for (x=0;x<sz;x++)
{
EEL_STRING_STORAGECLASS *s = m_literal_strings.Get(x);
if (ns->GetLength() == l && !strcmp(s->Get(),ns->Get())) break;
}
if (x<sz) delete ns;
else m_literal_strings.Add(ns);
return x+EEL_STRING_LITERAL_BASE;
#endif
}
static int varEnumProc(const char *name, EEL_F *val, void *ctx)
{
((eel_string_context_state *)ctx)->m_varname_cache.AddUnsorted(name,val);
return 1;
}
};
static int eel_validate_format_specifier(const char *fmt_in, char *typeOut,
char *fmtOut, int fmtOut_sz,
char *varOut, int varOut_sz,
int *varOut_used
)
{
const char *fmt = fmt_in+1;
int state=0;
if (fmt_in[0] != '%') return 0; // ugh passed a non-specifier
*varOut_used = 0;
*varOut = 0;
if (fmtOut_sz-- < 2) return 0;
*fmtOut++ = '%';
while (*fmt)
{
const char c = *fmt++;
if (fmtOut_sz < 2) return 0;
if (c == 'f'|| c=='e' || c=='E' || c=='g' || c=='G' || c == 'd' || c == 'u' ||
c == 'x' || c == 'X' || c == 'c' || c == 'C' || c =='s' || c=='S' || c=='i')
{
*typeOut = c;
fmtOut[0] = c;
fmtOut[1] = 0;
return (int) (fmt - fmt_in);
}
else if (c == '.')
{
*fmtOut++ = c; fmtOut_sz--;
if (state&(2)) break;
state |= 2;
}
else if (c == '+')
{
*fmtOut++ = c; fmtOut_sz--;
if (state&(32|16|8|4)) break;
state |= 8;
}
else if (c == '-' || c == ' ')
{
*fmtOut++ = c; fmtOut_sz--;
if (state&(32|16|8|4)) break;
state |= 16;
}
else if (c >= '0' && c <= '9')
{
*fmtOut++ = c; fmtOut_sz--;
state|=4;
}
else if (c == '{')
{
if (state & 64) break;
state|=64;
if (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) return 0; // symbol name can't start with 0-9 or .
while (*fmt != '}')
{
if ((*fmt >= 'a' && *fmt <= 'z') ||
(*fmt >= 'A' && *fmt <= 'Z') ||
(*fmt >= '0' && *fmt <= '9') ||
*fmt == '_' || *fmt == '.' || *fmt == '#')
{
if (varOut_sz < 2) return 0;
*varOut++ = *fmt++;
varOut_sz -- ;
}
else
{
return 0; // bad character in variable name
}
}
fmt++;
*varOut = 0;
*varOut_used=1;
}
else
{
break;
}
}
return 0;
}
int eel_format_strings(void *opaque, const char *fmt, const char *fmt_end, char *buf, int buf_sz, int num_fmt_parms, EEL_F **fmt_parms)
{
int fmt_parmpos = 0;
char *op = buf;
while ((fmt_end ? fmt < fmt_end : *fmt) && op < buf+buf_sz-128)
{
if (fmt[0] == '%' && fmt[1] == '%')
{
*op++ = '%';
fmt+=2;
}
else if (fmt[0] == '%')
{
char ct=0;
char fs[128];
char varname[128];
int varname_used=0;
const int l=eel_validate_format_specifier(fmt,&ct,fs,sizeof(fs),varname,sizeof(varname),&varname_used);
if (!l || !ct)
{
*op=0;
return -1;
}
EEL_F vv=0.0;
const EEL_F *varptr = NULL;
if (varname_used)
{
#ifdef EEL_STRING_GETNAMEDVAR
if (varname[0]) varptr=EEL_STRING_GETNAMEDVAR(varname,0,&vv);
#endif
}
else
{
if (fmt_parmpos < num_fmt_parms) varptr = fmt_parms[fmt_parmpos];
#ifdef EEL_STRING_GETFMTVAR
if (!varptr) varptr = EEL_STRING_GETFMTVAR(fmt_parmpos);
#endif
fmt_parmpos++;
}
double v = varptr ? (double)*varptr : 0.0;
if (ct == 's' || ct=='S')
{
EEL_STRING_STORAGECLASS *wr=NULL;
const char *str = EEL_STRING_GET_FOR_INDEX(v,&wr);
const int maxl=(int) (buf+buf_sz - 2 - op);
if (wr && !fs[2]) // %s or %S -- todo: implement padding modes for binary compat too?
{
int wl = wr->GetLength();
if (wl > maxl) wl=maxl;
memcpy(op,wr->Get(),wl);
op += wl;
*op=0;
}
else
{
snprintf(op,maxl,fs,str ? str : "");
}
}
else
{
if (varptr == &vv) // passed %{#str}d etc, convert to float
{
const char *str = EEL_STRING_GET_FOR_INDEX(v,NULL);
v = str ? atof(str) : 0.0;
}
if (ct == 'x' || ct == 'X' || ct == 'd' || ct == 'u' || ct=='i')
{
snprintf(op,64,fs,(int) (v));
}
else if (ct == 'c')
{
*op++=(char) (int)v;
*op=0;
}
else if (ct == 'C')
{
const unsigned int iv = (unsigned int) v;
int bs = 0;
if (iv & 0xff000000) bs=24;
else if (iv & 0x00ff0000) bs=16;
else if (iv & 0x0000ff00) bs=8;
while (bs>=0)
{
const char c=(char) (iv>>bs);
*op++=c?c:' ';
bs-=8;
}
*op=0;
}
else
{
snprintf(op,64,fs,v);
}
}
while (*op) op++;
fmt += l;
}
else
{
*op++ = *fmt++;
}
}
*op=0;
return (int) (op - buf);
}
static int eel_string_match(void *opaque, const char *fmt, const char *msg, int match_fmt_pos, int ignorecase, const char *fmt_endptr, const char *msg_endptr, int num_fmt_parms, EEL_F **fmt_parms)
{
// check for match, updating EEL_STRING_GETFMTVAR(*) as necessary
// %d=12345
// %f=12345[.678]
// %c=any nonzero char, ascii value
// %x=12354ab
// %*, %?, %+, %% literals
// * ? + match minimal groups of 0+,1, or 1+ chars
for (;;)
{
if (fmt>=fmt_endptr)
{
if (msg>=msg_endptr) return 1;
return 0; // format ends before matching string
}
// if string ends and format is not on a wildcard, early-out to 0
if (msg>=msg_endptr && *fmt != '*' && *fmt != '%') return 0;
switch (*fmt)
{
case '*':
case '+':
// if last char of search pattern, we're done!
if (fmt+1>=fmt_endptr || (fmt[1] == '?' && fmt+2>=fmt_endptr)) return *fmt == '*' || msg<msg_endptr;
if (fmt[0] == '+') msg++; // skip a character for + . Note that in this case msg[1] is valid, because of the !*msg && *fmt != '*' check above
fmt++;
if (*fmt == '?')
{
// *? or +? are lazy matches
fmt++;
while (msg<msg_endptr && !eel_string_match(opaque,fmt, msg,match_fmt_pos,ignorecase,fmt_endptr, msg_endptr,num_fmt_parms,fmt_parms)) msg++;
return msg<msg_endptr;
}
else
{
// greedy match
int len = (int) (msg_endptr-msg);
while (len >= 0 && !eel_string_match(opaque,fmt, msg+len,match_fmt_pos,ignorecase,fmt_endptr, msg_endptr,num_fmt_parms,fmt_parms)) len--;
return len >= 0;
}
break;
case '?':
fmt++;
msg++;
break;
case '%':
{
fmt++;
unsigned short fmt_minlen = 1, fmt_maxlen = 0;
if (*fmt >= '0' && *fmt <= '9')
{
fmt_minlen = *fmt++ - '0';
while (*fmt >= '0' && *fmt <= '9') fmt_minlen = fmt_minlen * 10 + (*fmt++ - '0');
fmt_maxlen = fmt_minlen;
}
if (*fmt == '-')
{
fmt++;
fmt_maxlen = 0;
while (*fmt >= '0' && *fmt <= '9') fmt_maxlen = fmt_maxlen * 10 + (*fmt++ - '0');
}
const char *dest_varname=NULL;
if (*fmt == '{')
{
dest_varname=++fmt;
while (*fmt && fmt < fmt_endptr && *fmt != '}') fmt++;
if (fmt >= fmt_endptr-1 || *fmt != '}') return 0; // malformed %{var}s
fmt++; // skip '}'
}
char fmt_char = *fmt++;
if (!fmt_char) return 0; // malformed
if (fmt_char == '*' ||
fmt_char == '?' ||
fmt_char == '+' ||
fmt_char == '%')
{
if (*msg++ != fmt_char) return 0;
}
else if (fmt_char == 'c')
{
EEL_F *varOut = NULL;
EEL_F vv=0.0;
if (!dest_varname)
{
if (match_fmt_pos < num_fmt_parms) varOut = fmt_parms[match_fmt_pos];
#ifdef EEL_STRING_GETFMTVAR
if (!varOut) varOut = EEL_STRING_GETFMTVAR(match_fmt_pos);
#endif
match_fmt_pos++;
}
else
{
#ifdef EEL_STRING_GETNAMEDVAR
char tmp[128];
int idx=0;
while (dest_varname < fmt_endptr && *dest_varname && *dest_varname != '}' && idx<(int)sizeof(tmp)-1) tmp[idx++] = *dest_varname++;
tmp[idx]=0;
if (idx>0) varOut = EEL_STRING_GETNAMEDVAR(tmp,1,&vv);
#endif
}
if (msg >= msg_endptr) return 0; // out of chars
if (varOut)
{
if (varOut == &vv) // %{#foo}c
{
EEL_STRING_STORAGECLASS *wr=NULL;
EEL_STRING_GET_FOR_WRITE(vv, &wr);
if (wr) wr->Set(msg,1);
}
else
{
*varOut = (EEL_F)*(unsigned char *)msg;
}
}
msg++;
}
else
{
int len=0;
int lazy=0;
if (fmt_char>='A'&&fmt_char<='Z') { lazy=1; fmt_char += 'a' - 'A'; }
if (fmt_char == 's')
{
len = (int) (msg_endptr-msg);
}
else if (fmt_char == 'x')
{
while ((msg[len] >= '0' && msg[len] <= '9') ||
(msg[len] >= 'A' && msg[len] <= 'F') ||
(msg[len] >= 'a' && msg[len] <= 'f')) len++;
}
else if (fmt_char == 'f')
{
if (msg[len] == '-') len++;
while (msg[len] >= '0' && msg[len] <= '9') len++;
if (msg[len] == '.')
{
len++;
while (msg[len] >= '0' && msg[len] <= '9') len++;
}
}
else if (fmt_char == 'd' || fmt_char == 'u' || fmt_char == 'i')
{
if (fmt_char != 'u' && msg[len] == '-') len++;
while (msg[len] >= '0' && msg[len] <= '9') len++;
}
else
{
// bad format
return 0;
}
if (fmt_maxlen>0 && len > fmt_maxlen) len = fmt_maxlen;
if (!dest_varname) match_fmt_pos++;
if (lazy)
{
if (fmt_maxlen<1 || fmt_maxlen>len) fmt_maxlen=len;
len=fmt_minlen;
while (len <= fmt_maxlen && !eel_string_match(opaque,fmt, msg+len,match_fmt_pos,ignorecase,fmt_endptr, msg_endptr,num_fmt_parms,fmt_parms)) len++;
if (len > fmt_maxlen) return 0;
}
else
{
while (len >= fmt_minlen && !eel_string_match(opaque,fmt, msg+len,match_fmt_pos,ignorecase,fmt_endptr, msg_endptr,num_fmt_parms,fmt_parms)) len--;
if (len < fmt_minlen) return 0;
}
EEL_F vv=0.0;
EEL_F *varOut = NULL;
if (!dest_varname)
{
if (match_fmt_pos>0 && match_fmt_pos-1 < num_fmt_parms) varOut = fmt_parms[match_fmt_pos-1];
#ifdef EEL_STRING_GETFMTVAR
if (!varOut) varOut = EEL_STRING_GETFMTVAR(match_fmt_pos-1);
#endif
}
else
{
#ifdef EEL_STRING_GETNAMEDVAR
char tmp[128];
int idx=0;
while (dest_varname < fmt_endptr && *dest_varname && *dest_varname != '}' && idx<(int)sizeof(tmp)-1) tmp[idx++] = *dest_varname++;
tmp[idx]=0;
if (idx>0) varOut = EEL_STRING_GETNAMEDVAR(tmp,1,&vv);
#endif
}
if (varOut)
{
if (fmt_char == 's')
{
EEL_STRING_STORAGECLASS *wr=NULL;
EEL_STRING_GET_FOR_WRITE(*varOut, &wr);
if (wr)
{
if (msg_endptr >= wr->Get() && msg_endptr <= wr->Get() + wr->GetLength())
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("match: destination specifier passed is also haystack, will not update");
#endif
}
else if (fmt_endptr >= wr->Get() && fmt_endptr <= wr->Get() + wr->GetLength())
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("match: destination specifier passed is also format, will not update");
#endif
}
else
{
wr->SetRaw(msg,len);
}
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("match: bad destination specifier passed as %d: %f",match_fmt_pos,*varOut);
#endif
}
}
else
{
char tmp[128];
lstrcpyn_safe(tmp,msg,wdl_min(len+1,(int)sizeof(tmp)));
if (varOut == &vv)
{
EEL_STRING_STORAGECLASS *wr=NULL;
EEL_STRING_GET_FOR_WRITE(vv, &wr);
if (wr) wr->Set(tmp);
}
else
{
char *bl=(char*)msg;
if (fmt_char == 'u')
*varOut = (EEL_F)strtoul(tmp,&bl,10);
else if (fmt_char == 'x')
*varOut = (EEL_F)strtoul(msg,&bl,16);
else
*varOut = (EEL_F)atof(tmp);
}
}
}
return 1;
}
}
break;
default:
if (ignorecase ? (toupper_safe(*fmt) != toupper_safe(*msg)) : (*fmt!= *msg)) return 0;
fmt++;
msg++;
break;
}
}
}
static EEL_F NSEEL_CGEN_CALL _eel_sprintf(void *opaque, INT_PTR num_param, EEL_F **parms)
{
if (num_param<2) return 0.0;
if (opaque)
{
EEL_STRING_MUTEXLOCK_SCOPE
EEL_STRING_STORAGECLASS *wr=NULL;
EEL_STRING_GET_FOR_WRITE(*(parms[0]), &wr);
if (!wr)
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("sprintf: bad destination specifier passed %f",*(parms[0]));
#endif
}
else
{
EEL_STRING_STORAGECLASS *wr_src=NULL;
const char *fmt = EEL_STRING_GET_FOR_INDEX(*(parms[1]),&wr_src);
if (fmt)
{
char buf[16384];
const int fmt_len = eel_format_strings(opaque,fmt,wr_src?(fmt+wr_src->GetLength()):NULL,buf,(int)sizeof(buf), (int)num_param-2, parms+2);
if (fmt_len>=0)
{
wr->SetRaw(buf,fmt_len);
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("sprintf: bad format string %s",fmt);
#endif
}
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("sprintf: bad format specifier passed %f",*(parms[1]));
#endif
}
}
}
return *(parms[0]);
}
static EEL_F NSEEL_CGEN_CALL _eel_strncat(void *opaque, EEL_F *strOut, EEL_F *fmt_index, EEL_F *maxlen)
{
if (opaque)
{
EEL_STRING_MUTEXLOCK_SCOPE
EEL_STRING_STORAGECLASS *wr=NULL, *wr_src=NULL;
EEL_STRING_GET_FOR_WRITE(*strOut, &wr);
if (!wr)
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("str%scat: bad destination specifier passed %f",maxlen ? "n":"",*strOut);
#endif
}
else
{
const char *fmt = EEL_STRING_GET_FOR_INDEX(*fmt_index,&wr_src);
if (fmt||wr_src)
{
if (wr->GetLength() > EEL_STRING_MAXUSERSTRING_LENGTH_HINT)
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("str%scat: will not grow string since it is already %d bytes",maxlen ? "n":"",wr->GetLength());
#endif
}
else
{
int ml=0;
if (maxlen && *maxlen > 0) ml = (int)*maxlen;
if (wr_src)
{
EEL_STRING_STORAGECLASS tmp;
if (wr_src == wr) *(wr_src=&tmp) = *wr;
wr->AppendRaw(wr_src->Get(), ml > 0 && ml < wr_src->GetLength() ? ml : wr_src->GetLength());
}
else
wr->Append(fmt, ml);
}
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("str%scat: bad format specifier passed %f",maxlen ? "n":"",*fmt_index);
#endif
}
}
}
return *strOut;
}
static EEL_F NSEEL_CGEN_CALL _eel_strcpysubstr(void *opaque, INT_PTR nparm, EEL_F **parms) //EEL_F *strOut, EEL_F *fmt_index, EEL_F *offs
{
if (opaque && nparm>=3)
{
EEL_STRING_MUTEXLOCK_SCOPE
EEL_STRING_STORAGECLASS *wr=NULL, *wr_src=NULL;
EEL_STRING_GET_FOR_WRITE(parms[0][0], &wr);
if (!wr)
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("strcpy_substr: bad destination specifier passed %f",parms[0][0]);
#endif
}
else
{
const char *fmt = EEL_STRING_GET_FOR_INDEX(parms[1][0],&wr_src);
if (fmt)
{
const int fmt_len = wr_src ? wr_src->GetLength() : (int) strlen(fmt);
int maxlen, o = (int) parms[2][0];
if (o < 0)
{
o = fmt_len + o;
if (o < 0) o=0;
}
maxlen = fmt_len - o;
if (nparm >= 4)
{
const int a = (int) parms[3][0];
if (a<0) maxlen += a;
else if (a<maxlen) maxlen=a;
}
if (maxlen < 1 || o >= fmt_len)
{
wr->Set("");
}
else
{
if (wr_src==wr)
{
wr->DeleteSub(0,o);
if (wr->GetLength() > maxlen) wr->SetLen(maxlen);
}
else
{
wr->SetRaw(fmt+o,maxlen);
}
}
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("strcpy_substr: bad format specifier passed %f",parms[1][0]);
#endif
}
}
return parms[0][0];
}
return 0.0;
}
static EEL_F NSEEL_CGEN_CALL _eel_strncpy(void *opaque, EEL_F *strOut, EEL_F *fmt_index, EEL_F *maxlen)
{
if (opaque)
{
EEL_STRING_MUTEXLOCK_SCOPE
EEL_STRING_STORAGECLASS *wr=NULL, *wr_src=NULL;
EEL_STRING_GET_FOR_WRITE(*strOut, &wr);
if (!wr)
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("str%scpy: bad destination specifier passed %f",maxlen ? "n":"",*strOut);
#endif
}
else
{
const char *fmt = EEL_STRING_GET_FOR_INDEX(*fmt_index,&wr_src);
if (fmt)
{
int ml=-1;
if (maxlen && *maxlen >= 0) ml = (int)*maxlen;
if (wr_src == wr)
{
if (ml>=0 && ml < wr->GetLength()) wr->SetLen(ml); // shorten string if strncpy(x,x,len) and len >=0
return *strOut;
}
if (wr_src) wr->SetRaw(fmt, ml>0 && ml < wr_src->GetLength() ? ml : wr_src->GetLength());
else wr->Set(fmt,ml);
}
else
{
#ifdef EEL_STRING_DEBUGOUT
EEL_STRING_DEBUGOUT("str%scpy: bad format specifier passed %f",maxlen ? "n":"",*fmt_index);
#endif
}
}
}
return *strOut;
}
static EEL_F _eel_strcmp_int(const char *a, int a_len, const char *b, int b_len, int ml, bool ignorecase)
{
// binary-safe comparison (at least if a_len>=0 etc)
int pos = 0;
for (;;)
{
if (ml > 0 && pos == ml) return 0.0;
const bool a_end = a_len >= 0 ? pos == a_len : !a[pos];
const bool b_end = b_len >= 0 ? pos == b_len : !b[pos];
if (a_end || b_end)
{
if (!b_end) return -1.0; // b[pos] is nonzero, a[pos] is zero
if (!a_end) return 1.0;
return 0.0;
}
char av = a[pos];
char bv = b[pos];
if (ignorecase)
{
av=toupper_safe(av);
bv=toupper_safe(bv);