-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamgustring.cxx
executable file
·4390 lines (3605 loc) · 148 KB
/
tamgustring.cxx
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
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamgustring.cxx
Date : 2017/09/01
Purpose :
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#include "codeparse.h"
#include "tamgu.h"
#include "tamgutaskell.h"
#include "tamgustring.h"
#include "constobjects.h"
#include "tamguivector.h"
#include "tamgufvector.h"
#include "tamgubvector.h"
#include "tamgusvector.h"
#include "tamguvector.h"
#include "tamgulvector.h"
#include "tamguuvector.h"
#include "tamgumapsi.h"
#include "tamguconstants.h"
#include "tamgubyte.h"
#include "tamguustring.h"
#include "fractalhmap.h"
#include "instructions.h"
#include "tamgumap.h"
#include "compilecode.h"
#include "tamguautomaton.h"
#include "tamgufile.h"
#include "x_tokenize.h"
//We need to declare once again our local definitions.
Exporting basebin_hash<stringMethod> Tamgustring::methods;
static ThreadLock classlock;
#ifdef DOSOUTPUT
static bool dosoutput = true;
static void setdosoutput(bool d) { dosoutput = d; }
#define conversion2Dos(x) dosoutput?s_utf8_to_dos(USTR(x)):x
#endif
Exporting long GetBlankSize();
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
void Tamgustring::AddMethod(TamguGlobal* global, string name, stringMethod func, unsigned long arity, string infos, short returntype) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(a_string) != global->infomethods.end() &&
global->infomethods[a_string].find(name) != global->infomethods[a_string].end())
return;
global->infomethods[a_string][name] = infos;
global->RecordArity(a_string, idname, arity);
global->RecordArity(a_stringthrough, idname, arity);
global->RecordArity(a_sloop, idname, arity);
if (returntype != a_null)
global->returntypes[idname] = returntype;
}
void Tamgustring::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamgustring::methods.isEmpty())
Tamgustring::InitialisationModule(global,"");
}
bool Tamgustring::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
Tamgustring::AddMethod(global, "succ", &Tamgustring::MethodSucc, P_NONE, "succ(): Return the successor of a character.", a_none);
Tamgustring::AddMethod(global, "pred", &Tamgustring::MethodPred, P_NONE, "pred(): Return the predecessor of a byte.", a_none);
Tamgustring::AddMethod(global, "doublemetaphone", &Tamgustring::MethodDoubleMetaphone, P_NONE, "doublemetaphone(): Return the string double metaphone conversion into a svector", a_svector);
Tamgustring::AddMethod(global, "hash", &Tamgustring::MethodHash, P_NONE, "hash(): Return the hash value of a string.", a_int);
Tamgustring::AddMethod(global, "utf16", &Tamgustring::MethodUTF16, P_NONE, "utf16(): Return the character unicode as UTF16 encoding.", a_vector);
Tamgustring::AddMethod(global, "ord", &Tamgustring::MethodOrd, P_NONE, "ord(): return the ASCII code of the first character, or a list of all ASCII code if the recipient is a vector", a_none);
Tamgustring::AddMethod(global, "bytes", &Tamgustring::MethodBytes, P_NONE, "bytes(): Return the string as a vector of bytes", a_ivector);
Tamgustring::AddMethod(global, "base", &Tamgustring::MethodBase, P_ONE | P_TWO, "base(int b, bool toconvert=true): Return the value corresponding to the string in base b", a_none);
Tamgustring::AddMethod(global, "parse", &Tamgustring::MethodParse, P_NONE | P_TWO, "parse(): Parse a string as a piece of code and returns the evaluation as a vector.", a_none);
Tamgustring::AddMethod(global, "sizeb", &Tamgustring::MethodSizeb, P_NONE, "sizeb(): Return the size in bytes of the string", a_int);
Tamgustring::AddMethod(global, "parenthetics", &Tamgustring::MethodParenthetic, P_NONE | P_TWO | P_THREE | P_FOUR | P_FIVE | P_SIX, "parenthetics(): parenthetics(string o,string c,bool comma,bool keep_carriage): Parse a string as a parenthetic expressions, o is '(' and c is ')' by default. If 'comma' is true, then the decimal character is ',' otherwise it is '.'. If 'separator' is true then '1,000' is accepted as a number.", a_none);
Tamgustring::AddMethod(global, "tags", &Tamgustring::MethodTags, P_TWO | P_THREE | P_FOUR | P_FIVE | P_SIX, "tags(string o,string c,bool comma,bool separator,bool keep_carriage, svector rules): Parse a string as a parenthetic expressions, where o and c are string (not characters). If 'comma' is true, then the decimal character is ',' otherwise it is '.'. If 'separator' is true then '1,000' is accepted as a number.", a_none);
Tamgustring::AddMethod(global, "scan", &Tamgustring::MethodScan, P_ONE | P_TWO | P_THREE | P_FOUR, "scan(sub, string sep, bool immediate,string remaining): Find the substrings matching sub, with TRE. 'sep' is a separator between strings. 'immediate' always combines with separator, it means that the matching should start at the first character of the string, default is false. 'remaining' also combines with 'separator', it returns the rest of the string after the section that matched.", a_none);
Tamgustring::AddMethod(global, "getstd", &Tamgustring::MethodGetstd, P_ONE, "getstd(bool): catch or release the standard output", a_none);
Tamgustring::AddMethod(global, "geterr", &Tamgustring::MethodGeterr, P_ONE, "geterr(bool): catch or release the error output", a_none);
Tamgustring::AddMethod(global, "levenshtein", &Tamgustring::MethodEditdistance, P_ONE | P_TWO, "levenshtein(string s,bool byte): Return the edit distance with 's' according to Levenshtein algorithm. If byte is true, force a byte level comparison. byte is optionnal.", a_int);
Tamgustring::AddMethod(global, "editdistance", &Tamgustring::MethodEditdistance, P_ONE | P_TWO | P_THREE, "editdistance(string s,bool byte): Return the edit distance with 's'. If byte is true, force a byte level comparison. byte is optionnal.", a_int);
Tamgustring::AddMethod(global, "tokenize", &Tamgustring::MethodTokenize, P_NONE | P_ONE | P_TWO | P_THREE, "tokenize(bool comma,bool separator, svector rules): Segment a string into words and punctuations. If 'comma' is true, then the decimal character is ',' otherwise it is '.'. If 'separator' is true then '1,000' is accepted as a number. rules is a set of tokenization rules that can be first initialized then modified with _getdefaulttokenizerules", a_none);
Tamgustring::AddMethod(global, "stokenize", &Tamgustring::MethodStokenize, P_NONE | P_ONE, "stokenize(map keeps): Segment a string into words and punctuations, with a keep.", a_none);
Tamgustring::AddMethod(global, "split", &Tamgustring::MethodSplit, P_ONE | P_NONE, "split(string splitter): split a string along splitter and store the results in a vector. If splitter=='', then the string is split into a vector of characters", a_svector);
Tamgustring::AddMethod(global, "multisplit", &Tamgustring::MethodMultiSplit, P_ATLEASTONE, "multisplit(string splitter1,string splitter2..): split a string along different splitters. Return a svector.", a_svector);
Tamgustring::AddMethod(global, "splite", &Tamgustring::MethodSplite, P_ONE | P_NONE, "splite(string splitter): split a string along splitter and store the results in a vector. If splitter=='', then the string is split into a vector of characters. Empty strings are kept in the result.", a_svector);
Tamgustring::AddMethod(global, "slice", &Tamgustring::MethodSlice, P_ONE, "slice(int sz): Return a vector of all slices of size sz", a_svector);
Tamgustring::AddMethod(global, "ngrams", &Tamgustring::MethodNgrams, P_ONE, "ngrams(int r): Return a vector of all ngrams of rank r", a_none);
Tamgustring::AddMethod(global, "extract", &Tamgustring::MethodExtract, P_ATLEASTTHREE, "extract(int pos,string from,string up1,string up2...): extract substrings between 'from' and 'up1'...'upn' (the shortest string is used). Return a vector of strings", a_svector);
Tamgustring::AddMethod(global, "find", &Tamgustring::MethodFind, P_TWO | P_ONE, "find(string sub,int pos): Return the position of substring sub starting at position pos", a_none);
Tamgustring::AddMethod(global, "rfind", &Tamgustring::MethodRfind, P_TWO | P_ONE, "rfind(string sub,int pos): Return the position of substring sub backward starting at position pos", a_int);
Tamgustring::AddMethod(global, "count", &Tamgustring::MethodCount, P_TWO | P_ONE, "count(string sub,int pos): Count the number of substrings starting at position pos", a_int);
Tamgustring::AddMethod(global, "countbaseline", &Tamgustring::MethodCountBaseLine, P_TWO | P_ONE, "countbaseline(string sub,int pos): Count the number of substrings starting at position pos with base line method", a_int);
Tamgustring::AddMethod(global, "byteposition", &Tamgustring::MethodByteposition, P_ONE, "byteposition(int pos): Convert a character position into a byte position", a_int);
Tamgustring::AddMethod(global, "charposition", &Tamgustring::MethodCharposition, P_ONE, "charposition(int pos): Convert a byte position into a character position", a_int);
Tamgustring::AddMethod(global, "isalpha", &Tamgustring::MethodIsalpha, P_NONE, "isalpha(): Test if a string only contains only alphabetical characters", a_boolean);
Tamgustring::AddMethod(global, "isconsonant", &Tamgustring::MethodIsconsonant, P_NONE, "isconsonant(): Test if a string only contains consonants", a_boolean);
Tamgustring::AddMethod(global, "isvowel", &Tamgustring::MethodIsvowel, P_NONE, "isvowel(): Test if a string only contains only vowels", a_boolean);
Tamgustring::AddMethod(global, "ispunctuation", &Tamgustring::MethodIspunctuation, P_NONE, "ispunctuation(): Test if the character is a punctuation", a_boolean);
Tamgustring::AddMethod(global, "isdigit", &Tamgustring::MethodIsdigit, P_NONE, "isdigit(): Test if a string only contains digits", a_boolean);
Tamgustring::AddMethod(global, "isupper", &Tamgustring::MethodIsupper, P_NONE, "isupper(): Test if a string only contains uppercase characters", a_boolean);
Tamgustring::AddMethod(global, "islower", &Tamgustring::MethodIslower, P_NONE, "islower(): Test if a string only contains lowercase characters", a_boolean);
Tamgustring::AddMethod(global, "isemoji", &Tamgustring::MethodIsemoji, P_NONE, "isemoji(): Test if a string only contains emoji characters", a_none);
Tamgustring::AddMethod(global, "isutf8", &Tamgustring::MethodIsutf8, P_NONE, "isutf8(): Return true is the string is encoded in UTF8", a_boolean);
Tamgustring::AddMethod(global, "reverse", &Tamgustring::MethodReverse, P_NONE, "reverse(): reverse the string", a_none);
Tamgustring::AddMethod(global, "format", &Tamgustring::MethodFormat, P_ATLEASTONE, "format(p1,p2,p3): Create a new string from the current string in which each '%x' is associated to one of the parameters, 'x' being the position of that parameter in the argument list. 'x' starts at 1.", a_none);
Tamgustring::AddMethod(global, "fill", &Tamgustring::MethodFill, P_TWO, "fill(int nb,string c): create a string of nb characters c", a_string);
Tamgustring::AddMethod(global, "padding", &Tamgustring::MethodPadding, P_TWO | P_THREE, "padding(int nb,string c,bool paddattheend): add nb characters c to the current string. Last parameter is optional", a_string);
Tamgustring::AddMethod(global, "pop", &Tamgustring::MethodPop, P_NONE | P_ONE | P_TWO, "pop(): remove last character", a_none);
Tamgustring::AddMethod(global, "evaluate", &Tamgustring::MethodEvaluate, P_NONE, "evaluate(): evaluate the meta-characters within a string and return the evaluated string.", a_string);
Tamgustring::AddMethod(global, "html", &Tamgustring::MethodTohtml, P_NONE, "html(): Return the string into an HTML compatible string or as a vector of strings", a_string);
Tamgustring::AddMethod(global, "tohtml", &Tamgustring::MethodTohtml, P_NONE, "tohtml(): Return the string into an HTML compatible string or as a vector of strings", a_string);
Tamgustring::AddMethod(global, "toxml", &Tamgustring::MethodToxml, P_NONE, "toxml(): Return the string into an XML compatible string or as a vector of strings", a_string);
Tamgustring::AddMethod(global, "replace", &Tamgustring::MethodReplace, P_TWO, "replace(string sub,string str): Replace the substrings matching sub with str", a_string);
Tamgustring::AddMethod(global, "removefirst", &Tamgustring::MethodRemovefirst, P_ONE, "removefirst(int nb): remove the first nb characters of a string", a_string);
Tamgustring::AddMethod(global, "removelast", &Tamgustring::MethodRemovelast, P_ONE, "removelast(int nb): remove the last nb characters of a string", a_string);
Tamgustring::AddMethod(global, "utf8", &Tamgustring::MethodUtf8, P_NONE | P_ONE, "utf8(int table): convert a LATIN string into UTF8. table is optional, by default it is ISO/IEC 8859 part 1.", a_string);
Tamgustring::AddMethod(global, "latin", &Tamgustring::MethodLatin, P_NONE, "latin(): convert an UTF8 string in LATIN", a_string);
Tamgustring::AddMethod(global, "dos", &Tamgustring::MethodDos, P_NONE, "dos(): convert a string into DOS encoding", a_string);
Tamgustring::AddMethod(global, "dostoutf8", &Tamgustring::MethodDostoutf8, P_NONE, "dostoutf8(): convert a DOS string into UTF8", a_string);
Tamgustring::AddMethod(global, "left", &Tamgustring::MethodLeft, P_ONE, "left(int nb): return the first nb characters of a string", a_none);
Tamgustring::AddMethod(global, "right", &Tamgustring::MethodRight, P_ONE, "right(int nb): return the last nb characters of a string", a_none);
Tamgustring::AddMethod(global, "mid", &Tamgustring::MethodMid, P_TWO, "mid(int pos,int nb): return the nb characters starting at position pos of a string", a_string);
Tamgustring::AddMethod(global, "emoji", &Tamgustring::MethodEmoji, P_NONE, "emoji(): Return the textual description of an emoji", a_none);
Tamgustring::AddMethod(global, "upper", &Tamgustring::MethodUpper, P_NONE, "upper(): Return the string in upper characters", a_string);
Tamgustring::AddMethod(global, "deaccentuate", &Tamgustring::MethodDeaccentuate, P_NONE, "deaccentuate(): Remove the accents from accented characters", a_string);
Tamgustring::AddMethod(global, "indent", &Tamgustring::MethodIndent, P_NONE | P_ONE | P_TWO, "indent(int nbblanks, bool taskel): Format a piece of code.", a_string);
Tamgustring::AddMethod(global, "lower", &Tamgustring::MethodLower, P_NONE, "lower(): Return the string in lower characters", a_string);
Tamgustring::AddMethod(global, "startwith", &Tamgustring::MethodStartWith, P_ONE, "startwith(string): check if it starts with a given string", a_boolean);
Tamgustring::AddMethod(global, "endwith", &Tamgustring::MethodEndWith, P_ONE, "endwith(string): check if it ends with a given string", a_boolean);
Tamgustring::AddMethod(global, "trim", &Tamgustring::MethodTrim, P_NONE, "trim(): remove the trailing characters", a_string);
Tamgustring::AddMethod(global, "trimleft", &Tamgustring::MethodTrimleft, P_NONE, "trimleft(): remove the trailing characters on the left", a_string);
Tamgustring::AddMethod(global, "trimright", &Tamgustring::MethodTrimright, P_NONE, "trimright(): remove the trailing characters on the right", a_string);
Tamgustring::AddMethod(global, "last", &Tamgustring::MethodLast, P_NONE, "last(): return last character", a_none);
Tamgustring::AddMethod(global, "insert", &Tamgustring::MethodInsert, P_ONE | P_TWO, "insert(int i,string s): insert the string s at i. If i is -1, then insert s between each character in the input string", a_none);
Tamgustring::AddMethod(global, "clear", &Tamgustring::MethodClear, P_NONE, "clear(): Clean the content of a string.", a_none);
Tamgustring::AddMethod(global, "jamo", &Tamgustring::MethodJamo, P_NONE | P_ONE, "jamo(bool combine): if 'combine' is false split a Korean jamo into its main components, else combine contents into a jamo.", a_none);
Tamgustring::AddMethod(global, "isjamo", &Tamgustring::MethodIsJamo, P_NONE, "isjamo(): return true if it is a Hangul jamo.", a_none);
Tamgustring::AddMethod(global, "ishangul", &Tamgustring::MethodIsHangul, P_NONE, "ishangul(): return true if it is a Hangul character.", a_none);
Tamgustring::AddMethod(global, "normalizehangul", &Tamgustring::MethodNormalizeHangul, P_NONE, "normalizehangul(): Normalize Hangul characters.", a_string);
Tamgustring::AddMethod(global, "romanization", &Tamgustring::MethodTransliteration, P_NONE, "romanization(): romanization of Hangul characters.", a_none);
Tamgustring::AddMethod(global, "read", &Tamgustring::MethodRead, P_ONE, "read(string path): read the file content into the current variable.", a_none);
Tamgustring::AddMethod(global, "write", &Tamgustring::MethodWrite, P_ONE, "write(string path): write the string content into a file.", a_none);
if (version != "") {
global->newInstance[a_string] = new Tamgustring("", global);
global->newInstance[a_stringthrough] = global->newInstance[a_string];
global->RecordCompatibilities(a_string);
global->RecordCompatibilities(a_stringthrough);
global->RecordCompatibilities(a_sloop);
//Encoding table name...
global->CreateSystemVariable(aONE, "e_latin_we", a_short);
global->CreateSystemVariable(global->ProvideConstint(2), "e_latin_ce", a_short);
global->CreateSystemVariable(global->ProvideConstint(3), "e_latin_se", a_short);
global->CreateSystemVariable(global->ProvideConstint(4), "e_latin_ne", a_short);
global->CreateSystemVariable(global->ProvideConstint(5), "e_cyrillic", a_short);
global->CreateSystemVariable(global->ProvideConstint(6), "e_arabic", a_short);
global->CreateSystemVariable(global->ProvideConstint(7), "e_greek", a_short);
global->CreateSystemVariable(global->ProvideConstint(8), "e_hebrew", a_short);
global->CreateSystemVariable(global->ProvideConstint(9), "e_turkish", a_short);
global->CreateSystemVariable(global->ProvideConstint(10), "e_nordic", a_short);
global->CreateSystemVariable(global->ProvideConstint(11), "e_thai", a_short);
global->CreateSystemVariable(global->ProvideConstint(13), "e_baltic", a_short);
global->CreateSystemVariable(global->ProvideConstint(14), "e_celtic", a_short);
global->CreateSystemVariable(global->ProvideConstint(15), "e_latin_ffe", a_short);
global->CreateSystemVariable(global->ProvideConstint(16), "e_latin_see", a_short);
global->CreateSystemVariable(global->ProvideConstint(17), "e_windows", a_short);
global->CreateSystemVariable(global->ProvideConstint(17), "e_cp1252", a_short);
}
Tamgua_string::InitialisationModule(global, version);
return true;
}
Exporting TamguIteration* Tamgustring::Newiteration(bool direction) {
Locking _lock(this);
return new TamguIterationstring(value, direction);
}
Tamgu* Tamgustring::Loopin(TamguInstruction* ins, Tamgu* context, short idthread) {
agnostring val(String());
Tamgu* var = ins->instructions.vecteur[0]->Instruction(0);
var = var->Eval(context, aNULL, idthread);
Tamgustring v("");
Tamgu* a = aNULL;
bool testcond = false;
val.begin();
while (!val.end() && !testcond) {
a->Releasenonconst();
v.value = val.nextemoji();
var->Putvalue(&v, idthread);
a = ins->instructions.vecteur[1]->Eval(context, aNULL, idthread);
//Continue does not trigger needInvestigate
testcond = a->needInvestigate();
}
if (testcond) {
if (a == aBREAK)
return this;
return a;
}
a->Releasenonconst();
return this;
}
Tamgu* Tamgustring::Succ() {
Locking _lock(this);
if (value.empty())
return globalTamgu->Providestring("");
wstring v;
s_utf8_to_unicode(v, USTR(value), value.size());
v[v.size() - 1] = v.back() + 1;
return globalTamgu->Providewithustring(v);
}
Tamgu* Tamgustring::Pred() {
Locking _lock(this);
if (value.empty())
return globalTamgu->Providestring("");
wstring v;
s_utf8_to_unicode(v, USTR(value), value.size());
wchar_t c = v.back();
if (c <= 1)
return globalTamgu->Providewithustring(v);
v[v.size() - 1] = c - 1;
return globalTamgu->Providewithustring(v);
}
Tamgu* Tamgustring::MethodStartWith(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
string copen = callfunc->Evaluate(0, contextualpattern, idthread)->String();
Locking _lock(this);
string v = value.substr(0, copen.size());
if (v == copen)
return aTRUE;
return aFALSE;
}
Tamgu* Tamgustring::MethodEndWith(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
string copen = callfunc->Evaluate(0, contextualpattern, idthread)->String();
Locking _lock(this);
long sz = value.size();
long szo = copen.size();
if (sz >= szo) {
string v = value.substr(sz - szo, szo);
if (v == copen)
return aTRUE;
}
return aFALSE;
}
Exporting Tamgu* Tamgustring::in(Tamgu* context, Tamgu* a, short idthread) {
//Three cases along the container type...
//It is a Boolean, we expect false or true
//It is an integer, we expect a position in v
//It is a container, we are looking for all positions...
string val;
a->Setstring(val, idthread);
long r;
if (globalTamgu->threadMODE) {
locking();
if (context->isVectorContainer()) {
Tamguivector* v = (Tamguivector*)Selectaivector(context);
Locking* _vlock = _getlock(v);
s_findall(value, val, v->values);
v_convertbytetocharposition(USTR(value), v->values);
_cleanlock(_vlock);
unlocking();
return v;
}
r = s_find(value, val, 0);
unlocking();
}
else {
if (context->isVectorContainer()) {
Tamguivector* v = (Tamguivector*)Selectaivector(context);
s_findall(value, val, v->values);
v_convertbytetocharposition(USTR(value), v->values);
return v;
}
r = s_find(value, val, 0);
}
if (context->isJustString()) {
return (r == -1 ? aEMPTYSTRING : globalTamgu->Providewithstring(val));
}
if (context->isJustNumber())
return globalTamgu->ProvideConstint(r);
return (r == -1 ? aFALSE : aTRUE);
}
Tamgu* TamguConstString::in(Tamgu* context, Tamgu* a, short idthread) {
//Three cases along the container type...
//It is a Boolean, we expect false or true
//It is an integer, we expect a position in v
//It is a container, we are looking for all positions...
string val = a->String();
if (context->isVectorContainer()) {
Tamguivector* v = (Tamguivector*)Selectaivector(context);
Locking* _vlock = _getlock(v);
s_findall(value, val, v->values);
v_convertbytetocharposition(USTR(value), v->values);
_cleanlock(_vlock);
return v;
}
long r = s_find(value, val, 0);
if (context->isJustString()) {
if (r==-1)
return aEMPTYSTRING;
return globalTamgu->Providewithstring(val);
}
if (context->isJustNumber())
return globalTamgu->ProvideConstint(r);
if (r == -1)
return aFALSE;
return aTRUE;
}
Exporting unsigned long Tamgustring::EditDistance(Tamgu* e) {
locking();
string s2 = e->String();
unsigned long s1len, s2len, x, y, lastdiag, olddiag;
s1len = value.size();
s2len = s2.size();
size_t* column = new size_t[s1len + 1];
for (y = 1; y <= s1len; y++)
column[y] = y;
for (x = 1; x <= s2len; x++) {
column[0] = x;
for (y = 1, lastdiag = x - 1; y <= s1len; y++) {
olddiag = column[y];
column[y] = MIN3(column[y] + 1, column[y - 1] + 1, lastdiag + (value[y - 1] == s2[x - 1] ? 0 : 1));
lastdiag = olddiag;
}
}
s2len = column[s1len];
delete[] column;
unlocking();
return s2len;
}
Exporting Tamgu* Tamgustring::Put(Tamgu* idx, Tamgu* v, short idthread) {
if (!idx->isIndex()) {
if (v == this)
return aFALSE;
locking();
v->Setstring(value, idthread);
unlocking();
return aTRUE;
}
long ileft, iright;
locking();
char res = StringIndexes(value, idx, ileft, iright, idthread);
if (res == 0) {
unlocking();
if (globalTamgu->erroronkey)
return globalTamgu->Returnerror(e_wrong_key_in, idthread);
return aFALSE;
}
if (res == 1) {
iright = ileft;
iright = c_char_next(USTR(value), iright);
}
else
iright = iright - ileft;
value.erase(ileft, iright);
string s;
if (v != aNULL)
s = v->String();
if (s != "")
value.insert(ileft, s);
unlocking();
return aTRUE;
}
Exporting Tamgu* Tamgustring::Eval(Tamgu* context, Tamgu* idx, short idthread) {
if (!idx->isIndex() || context == idx)
return this;
long ileft, iright;
locking();
char res = StringIndexes(value, idx, ileft, iright, idthread);
if (res == 0) {
unlocking();
if (globalTamgu->erroronkey)
return globalTamgu->Returnerror(e_wrong_key_in, idthread);
return aNOELEMENT;
}
if (res == 1)
idx = globalTamgu->Providestring(c_char_get(USTR(value), ileft));
else
idx = globalTamgu->Providestring(value.substr(ileft, iright - ileft));
unlocking();
return idx;
}
//------------------------------------------------------------------------------------------------------------------
#ifdef WSTRING_IS_UTF16
Tamgu* Tamgustring::MethodUTF16(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
wstring s = UString();
uint32_t code;
uint32_t subcode;
Tamgu* kvect=SelectContainer(contextualpattern,idthread);
if (kvect==NULL)
kvect=new Tamgulvector;
Locking _lock((TamguObject*)kvect);
for (size_t i = 0; i < s.size(); i++) {
kvect->storevalue((BLONG)s[i]);
}
return kvect;
}
Tamgu* Tamgustring::MethodOrd(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
wstring s = UString();
uint32_t c;
if (s.size() >= 1) {
if (contextualpattern->isVectorContainer() || s.size()>1) {
Tamgu* kvect = SelectContainer(contextualpattern, idthread);
if (kvect == NULL)
kvect = new Tamgulvector;
Locking _lock((TamguObject*)kvect);
for (size_t i = 0; i < s.size(); i++) {
if (c_utf16_to_unicode(c, s[i], false))
c_utf16_to_unicode(c, s[++i], true);
kvect->storevalue((BLONG)c);
}
return kvect;
}
if (c_utf16_to_unicode(c, s[0], false))
c_utf16_to_unicode(c, s[1], true);
if (contextualpattern->isNumber()) {
contextualpattern->storevalue((BLONG)c);
return contextualpattern;
}
return globalTamgu->Providelong(c);
}
return aNULL;
}
#else
Tamgu* Tamgustring::MethodUTF16(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
wstring s = UString();
uint32_t code;
uint32_t subcode;
Tamgu* kvect=SelectContainer(contextualpattern,idthread);
if (kvect==NULL)
kvect=new Tamgulvector;
Locking _lock((TamguObject*)kvect);
for (size_t i = 0; i < s.size(); i++) {
c_unicode_to_utf16(code, s[i]);
if (!code)
kvect->storevalue((BLONG)code);
else {
subcode = code >> 16;
code &= 0xFFFF;
if (subcode)
kvect->storevalue((BLONG)subcode);
kvect->storevalue((BLONG)code);
}
}
return kvect;
}
Tamgu* Tamgustring::MethodOrd(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
wstring s = UString();
if (s.size() >= 1) {
if (contextualpattern->isVectorContainer() || s.size()>1) {
Tamgu* kvect = SelectContainer(contextualpattern, idthread);
if (kvect == NULL)
kvect = new Tamgulvector;
Locking _lock((TamguObject*)kvect);
for (size_t i = 0; i < s.size(); i++)
kvect->storevalue(s[i]);
return kvect;
}
if (contextualpattern->isNumber()) {
contextualpattern->storevalue((BLONG)s[0]);
return contextualpattern;
}
return globalTamgu->Providelong(s[0]);
}
return aNULL;
}
#endif
//------------------------------------------------------------------------------------------------------------------
Tamgu* Tamgustring::MethodIsutf8(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
contextualpattern = booleantamgu[s_is_utf8(USTR(value), value.size())];
unlocking();
return contextualpattern;
}
Tamgu* Tamgustring::MethodEvaluate(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgustring* thestr = globalTamgu->Providestring();
locking();
EvaluateMetaInString(thestr->value,value);
unlocking();
return thestr;
}
void XNBrowse(x_node* xn, Tamgu* kf);
Tamgu* Tamgustring::MethodParse(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
tokenizer_result<string> xr;
bnf_tamgu bnf;
locking();
string str = value;
unlocking();
x_node* xn;
globalTamgu->tamgu_tokenizer.tokenize<string>(str, xr);
if (xr.size() == 0)
return Selectavector(contextualpattern);
xn = bnf.x_parsing(&xr, FULL, false);
string message;
if (xn == NULL) {
if (bnf.errornumber != -1) {
string msg("EVL(100):");
msg += bnf.x_errormsg(bnf.errornumber);
char ch[20];
sprintf_s(ch, 20, " (line:%ld)", bnf.lineerror);
msg += ch;
return globalTamgu->Returnerror(msg, idthread);
}
return globalTamgu->Returnerror(e_unknown_expression, idthread);
}
Tamgu* kvect = Selectavector(contextualpattern);
XNBrowse(xn, kvect);
return kvect;
}
Tamgu* Tamgustring::MethodParenthetic(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
string str = value;
unlocking();
string copen("(");
string cclose(")");
long nbparams = callfunc->Size();
vector<string> rules;
if (nbparams == 0)
return globalTamgu->EvaluateParenthetic(str, copen, cclose, false, false, false, rules, idthread);
copen = callfunc->Evaluate(0, contextualpattern, idthread)->String();
cclose = callfunc->Evaluate(1, contextualpattern, idthread)->String();
if (copen.size() != 1 || cclose.size() != 1)
return globalTamgu->Returnerror("TAMGUI(831): Wrong opening or closing character in LISP", idthread);
bool comma = false;
bool separator = false;
bool keeprc = false;
if (nbparams >= 3) {
comma = callfunc->Evaluate(2, contextualpattern, idthread)->Boolean();
if (nbparams >= 4) {
separator = callfunc->Evaluate(3, contextualpattern, idthread)->Boolean();
if (nbparams >= 5) {
keeprc = callfunc->Evaluate(4, contextualpattern, idthread)->Boolean();
if (nbparams == 6) {
Tamgu* vect = callfunc->Evaluate(5, contextualpattern, idthread);
for (long i = 0; i< vect->Size(); i++)
rules.push_back(vect->getstring(i));
}
}
}
}
return globalTamgu->EvaluateParenthetic(str, copen, cclose, comma, separator, keeprc, rules, idthread);
}
Tamgu* Tamgustring::MethodTags(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
string str = value;
unlocking();
string copen("(");
string cclose(")");
long nbparams = callfunc->Size();
vector<string> rules;
copen = callfunc->Evaluate(0, contextualpattern, idthread)->String();
cclose = callfunc->Evaluate(1, contextualpattern, idthread)->String();
//if (copen.size() != 1 || cclose.size() != 1)
// return globalTamgu->Returnerror("TAMGUI(832): Wrong opening or closing character in TAGS", idthread);
bool comma = false;
bool separator = false;
bool keeprc = false;
if (nbparams >= 3) {
comma = callfunc->Evaluate(2, contextualpattern, idthread)->Boolean();
if (nbparams >= 4) {
separator = callfunc->Evaluate(3, contextualpattern, idthread)->Boolean();
if (nbparams >= 5) {
keeprc = callfunc->Evaluate(4, contextualpattern, idthread)->Boolean();
if (nbparams == 6) {
Tamgu* vect = callfunc->Evaluate(5, contextualpattern, idthread);
for (long i = 0; i< vect->Size(); i++)
rules.push_back(vect->getstring(i));
}
}
}
}
return globalTamgu->EvaluateTags(str, copen, cclose, comma, separator, keeprc, rules, idthread);
}
Tamgu* Tamgustring::MethodScan(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* rgx = callfunc->Evaluate(0, contextualpattern, idthread);
Au_automate* a = getautomate(rgx);
if (a == NULL)
return globalTamgu->Returnerror(e_wrong_grammar_definition);
wstring reg;
locking();
sc_utf8_to_unicode(reg, USTR(value), value.size());
unlocking();
bool fsep = false;
wstring sep = L"";
bool immediate=false;
if (callfunc->Size() > 1) {
sep = callfunc->Evaluate(1, contextualpattern, idthread)->UString();
fsep = true;
if (callfunc->Size() >= 3)
immediate = callfunc->Evaluate(2, contextualpattern, idthread)->Boolean();
}
if (fsep || contextualpattern->isVectorContainer()) {
Tamgu* vect = Selectavector(contextualpattern);
vector<long> values;
if (fsep) {
wstring wlocal=reg;
values.push_back(0);
long sz = reg.size();
for (long i =0; i< sz; i++) {
values[0] = i;
a->find(reg,sep, values);
if (values.size()>1) {
if (i) {
for (long dx =1; dx < values.size();dx++)
values[dx] += i;
}
break;
}
else
if (immediate)
return vect;
reg=reg.c_str()+1;
}
if (values.size() <= 1)
return vect;
Locking _vlock((TamguObject*)vect);
wstring sub;
for (long i=0; i< values.size() -1; i++) {
sub = wlocal.substr(values[i], values[i+1]-values[i]-1);
if (sub != L"")
vect->storevalue(sub);
}
if (callfunc->Size() == 4) {
Tamgu* s = callfunc->Evaluate(3,contextualpattern, idthread);
s->storevalue(wlocal.substr(values.back(), wlocal.size()-values.back()));
}
return vect;
}
a->searchall(reg,values);
Locking _vlock((TamguObject*)vect);
if (contextualpattern->isNumber()) {
//This a number vector, we return the positions...
for (long i = 0; i< values.size(); i++)
vect->storevalue(values[i]);
return vect;
}
wstring sub;
for (long i=0; i< values.size(); i+=2) {
sub = reg.substr(values[i], values[i+1]-values[i]);
if (sub != L"")
vect->storevalue(sub);
}
return vect;
}
if (contextualpattern->isNumber() && contextualpattern->isAtom()) {
long res,end;
a->search(reg,res,end);
return globalTamgu->ProvideConstint(res);
}
if (contextualpattern->isString()) {
long res,end;
if (a->search(reg,res,end)) {
reg = reg.substr(res, end-res);
return globalTamgu->Providewithustring(reg);
}
else
return aEMPTYSTRING;
}
return booleantamgu[a->match(reg)];
}
Tamgu* Tamgustring::MethodCount(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
long i = 0;
Tamgu* substr = callfunc->Evaluate(0, contextualpattern, idthread);
if (callfunc->Size() == 2) {
i = callfunc->Evaluate(1, contextualpattern, idthread)->Integer();
}
if (substr->isRegular()) {
wstring w = UString();
if (i)
w = w.substr(i, w.size() - i);
vector<long> values;
substr->searchall(w,values);
return globalTamgu->ProvideConstint(values.size()>>1);
}
string sub = substr->String();
if (globalTamgu->threadMODE) {
locking();
if (value.empty())
i = 0;
else
i = s_count(value, sub, i);
unlocking();
}
else {
if (value.empty())
i = 0;
else
i = s_count(value, sub, i);
}
return globalTamgu->ProvideConstint(i);
}
static long s_countbaseline(string& str, string& sub, long i) {
long nb = 0;
i = str.find(sub, i);
while (i != -1) {
nb++;
i = str.find(sub, ++i);
}
return nb;
}
Tamgu* Tamgustring::MethodCountBaseLine(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
long i = 0;
Tamgu* substr = callfunc->Evaluate(0, contextualpattern, idthread);
if (callfunc->Size() == 2) {
i = callfunc->Evaluate(1, contextualpattern, idthread)->Integer();
}
if (substr->isRegular()) {
wstring w = UString();
if (i)
w = w.substr(i, w.size() - i);
vector<long> values;
substr->searchall(w,values);
return globalTamgu->ProvideConstint(values.size()>>1);
}
string sub = substr->String();
if (globalTamgu->threadMODE) {
locking();
if (value.empty())
i = 0;
else
i = s_countbaseline(value, sub, i);
unlocking();
}
else {
if (value.empty())
i = 0;
else
i = s_countbaseline(value, sub, i);
}
return globalTamgu->ProvideConstint(i);
}
Tamgu* Tamgustring::MethodFind(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* ksub = callfunc->Evaluate(0, contextualpattern, idthread);
if (!Size()) {
if (contextualpattern->isVectorContainer())
return Selectavector(contextualpattern);
return aMINUSONE;
}
//we search for a substring starting at position idx
long i = 0;
if (callfunc->Size() == 2)
i = callfunc->Evaluate(1, contextualpattern, idthread)->Integer();
if (contextualpattern->isVectorContainer()) {
Tamgu* kvect = Selectavector(contextualpattern);
long j;
wstring str = UString();
vector<long> v;
if (ksub->isRegular()) {
ksub->searchall(str,v,i);
}
else {
wstring sub = ksub->UString();
s_findall(str, sub, v, i);
}
if (v.size()) {
Locking _lock((TamguObject*)kvect);
switch (kvect->Type()) {
case a_bvector:
for (j = 0; j < v.size(); j++) {
if (v[j] >= i)
((Tamgubvector*)kvect)->values.push_back((uchar)v[j]);
}
return kvect;
case a_ivector:
for (j = 0; j < v.size(); j++) {
if (v[j] >= i)
((Tamguivector*)kvect)->values.push_back((uchar)v[j]);
}
return kvect;
case a_fvector:
for (j = 0; j < v.size(); j++) {
if (v[j] >= i)
((Tamgufvector*)kvect)->values.push_back((double)v[j]);
}
return kvect;
}
TamguConstInt loc(0);
for (j = 0; j < v.size(); j++) {
if (v[j] >= i) {
loc.value = v[j];
kvect->Push(&loc);
}
}
}
return kvect;
}
if (ksub->isRegular()) {
wstring str = UString();
long e;
if (!ksub->search(str,i,e,i))
return aMINUSONE;
return globalTamgu->ProvideConstint(i);
}
string sub = ksub->String();
locking();
i = s_find(value, sub, i);
unlocking();
if (i == -1)
return aMINUSONE;
return globalTamgu->ProvideConstint(i);
}
Tamgu* Tamgustring::MethodRfind(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
if (globalTamgu->threadMODE) {
if (!Size()) {
if (contextualpattern->isVectorContainer())
return Selectavector(contextualpattern);
return aMINUSONE;
}
}
else {
if (!value.size()) {
if (contextualpattern->isVectorContainer())
return Selectavector(contextualpattern);
return aMINUSONE;
}
}
Tamgu* ksub = callfunc->Evaluate(0, contextualpattern, idthread);
//we search for a substring starting at position idx
long i = -1;
if (callfunc->Size() == 2)