forked from mrihtar/GeoCoordinateConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deelx.h
4669 lines (3789 loc) · 106 KB
/
deelx.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
// deelx.h
//
// DEELX Regular Expression Engine (v1.2)
//
// Copyright 2006 ~ 2012 (c) RegExLab.com
// All Rights Reserved.
//
// http://www.regexlab.com/deelx/
//
// Author: Ê·ÊÙΰ (sswater shi)
//
// $Revision: 864 $
//
#ifndef __DEELX_REGEXP__H__
#define __DEELX_REGEXP__H__
#include <memory.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
extern "C" {
typedef int (*POSIX_FUNC)(int);
int isblunk(int c);
}
//
// Data Reference
//
template <class ELT> class CBufferRefT
{
public:
CBufferRefT(const ELT * pcsz, int length);
CBufferRefT(const ELT * pcsz);
public:
int nCompare (const ELT * pcsz) const;
int nCompareNoCase(const ELT * pcsz) const;
int Compare (const ELT * pcsz) const;
int CompareNoCase(const ELT * pcsz) const;
int Compare (const CBufferRefT <ELT> &) const;
int CompareNoCase(const CBufferRefT <ELT> &) const;
ELT At (int nIndex, ELT def = 0) const;
ELT operator [] (int nIndex) const;
const ELT * GetBuffer() const;
int GetSize() const;
public:
virtual ~CBufferRefT();
// Content
protected:
ELT * m_pBuffer;
int m_nSize;
};
//
// Implemenation
//
template <class ELT> CBufferRefT <ELT> :: CBufferRefT(const ELT * pcsz, int length)
{
m_pBuffer = (ELT *)pcsz;
m_nSize = length;
}
template <class ELT> CBufferRefT <ELT> :: CBufferRefT(const ELT * pcsz)
{
m_pBuffer = (ELT *)pcsz;
m_nSize = 0;
if(pcsz != 0) while(m_pBuffer[m_nSize] != 0) m_nSize ++;
}
template <class ELT> int CBufferRefT <ELT> :: nCompare(const ELT * pcsz) const
{
for(int i=0; i<m_nSize; i++)
{
if(m_pBuffer[i] != pcsz[i])
return m_pBuffer[i] - pcsz[i];
}
return 0;
}
template <class ELT> int CBufferRefT <ELT> :: nCompareNoCase(const ELT * pcsz) const
{
for(int i=0; i<m_nSize; i++)
{
if(m_pBuffer[i] != pcsz[i])
{
if(toupper((int)m_pBuffer[i]) != toupper((int)pcsz[i]))
return m_pBuffer[i] - pcsz[i];
}
}
return 0;
}
template <class ELT> inline int CBufferRefT <ELT> :: Compare(const ELT * pcsz) const
{
return nCompare(pcsz) ? 1 : (int)pcsz[m_nSize];
}
template <class ELT> inline int CBufferRefT <ELT> :: CompareNoCase(const ELT * pcsz) const
{
return nCompareNoCase(pcsz) ? 1 : (int)pcsz[m_nSize];
}
template <class ELT> inline int CBufferRefT <ELT> :: Compare(const CBufferRefT <ELT> & cref) const
{
return m_nSize == cref.m_nSize ? nCompare(cref.GetBuffer()) : 1;
}
template <class ELT> inline int CBufferRefT <ELT> :: CompareNoCase(const CBufferRefT <ELT> & cref) const
{
return m_nSize == cref.m_nSize ? nCompareNoCase(cref.GetBuffer()) : 1;
}
template <class ELT> inline ELT CBufferRefT <ELT> :: At(int nIndex, ELT def) const
{
return nIndex >= m_nSize ? def : m_pBuffer[nIndex];
}
template <class ELT> inline ELT CBufferRefT <ELT> :: operator [] (int nIndex) const
{
return nIndex >= m_nSize ? 0 : m_pBuffer[nIndex];
}
template <class ELT> const ELT * CBufferRefT <ELT> :: GetBuffer() const
{
static const ELT _def[] = {0}; return m_pBuffer ? m_pBuffer : _def;
}
template <class ELT> inline int CBufferRefT <ELT> :: GetSize() const
{
return m_nSize;
}
template <class ELT> CBufferRefT <ELT> :: ~CBufferRefT()
{
}
//
// Data Buffer
//
template <class ELT> class CBufferT : public CBufferRefT <ELT>
{
public:
CBufferT(const ELT * pcsz, int length);
CBufferT(const ELT * pcsz);
CBufferT();
public:
ELT & operator [] (int nIndex);
const ELT & operator [] (int nIndex) const;
void Append(const ELT * pcsz, int length, int eol = 0);
void Append(ELT el, int eol = 0);
public:
void Push(ELT el);
void Push(const CBufferRefT<ELT> & buf);
int Pop (ELT & el);
int Pop (CBufferT<ELT> & buf);
int Peek(ELT & el) const;
public:
const ELT * GetBuffer() const;
ELT * GetBuffer();
ELT * Detach();
void Release();
void Prepare(int index, int fill = 0);
void Restore(int size);
ELT * PrepareInsert(int nPos, int nSize)
{
int nOldSize = CBufferRefT<ELT>::m_nSize;
Restore(nPos > CBufferRefT<ELT>::m_nSize ? nPos : CBufferRefT<ELT>::m_nSize + nSize);
if( nPos < nOldSize )
{
ELT * from = CBufferRefT<ELT>::m_pBuffer + nPos, * to = CBufferRefT<ELT>::m_pBuffer + nPos + nSize;
memmove(to, from, sizeof(ELT) * (nOldSize - nPos));
}
return CBufferRefT<ELT>::m_pBuffer + nPos;
}
void Insert(int nIndex, const ELT & rT)
{
Insert(nIndex, &rT, 1);
}
void Insert(int nIndex, const ELT * pT, int nSize)
{
memcpy(PrepareInsert(nIndex, nSize), pT, sizeof(ELT) * nSize);
}
void Remove(int nIndex)
{
Remove(nIndex, 1);
}
void Remove(int nIndex, int nSize)
{
if( nIndex < CBufferRefT <ELT> :: m_nSize )
{
if( nIndex + nSize >= CBufferRefT <ELT> :: m_nSize )
{
Restore(nIndex);
}
else
{
memmove(CBufferRefT <ELT> :: m_pBuffer + nIndex, CBufferRefT <ELT> :: m_pBuffer + nIndex + nSize, sizeof(ELT) * (CBufferRefT <ELT> :: m_nSize - nIndex - nSize));
Restore(CBufferRefT <ELT> :: m_nSize - nSize);
}
}
}
void SetMaxLength(int nSize)
{
if( nSize > m_nMaxLength )
{
if( m_nMaxLength < 8 )
m_nMaxLength = 8;
if( nSize > m_nMaxLength )
m_nMaxLength *= 2;
if( nSize > m_nMaxLength )
{
m_nMaxLength = nSize + 11;
m_nMaxLength -= m_nMaxLength & 0x07;
}
CBufferRefT <ELT> :: m_pBuffer = (ELT *) realloc(CBufferRefT <ELT> :: m_pBuffer, sizeof(ELT) * m_nMaxLength);
}
}
public:
virtual ~CBufferT();
// Content
protected:
int m_nMaxLength;
};
//
// Implemenation
//
template <class ELT> CBufferT <ELT> :: CBufferT(const ELT * pcsz, int length) : CBufferRefT <ELT> (0, length)
{
m_nMaxLength = CBufferRefT <ELT> :: m_nSize + 1;
CBufferRefT <ELT> :: m_pBuffer = (ELT *) malloc(sizeof(ELT) * m_nMaxLength);
memcpy(CBufferRefT<ELT>::m_pBuffer, pcsz, sizeof(ELT) * CBufferRefT <ELT> :: m_nSize);
CBufferRefT<ELT>::m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> CBufferT <ELT> :: CBufferT(const ELT * pcsz) : CBufferRefT <ELT> (pcsz)
{
m_nMaxLength = CBufferRefT <ELT> :: m_nSize + 1;
CBufferRefT <ELT> :: m_pBuffer = (ELT *) malloc(sizeof(ELT) * m_nMaxLength);
memcpy(CBufferRefT<ELT>::m_pBuffer, pcsz, sizeof(ELT) * CBufferRefT <ELT> :: m_nSize);
CBufferRefT<ELT>::m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> CBufferT <ELT> :: CBufferT() : CBufferRefT <ELT> (0, 0)
{
m_nMaxLength = 0;
CBufferRefT<ELT>::m_pBuffer = 0;
}
template <class ELT> inline ELT & CBufferT <ELT> :: operator [] (int nIndex)
{
return CBufferRefT<ELT>::m_pBuffer[nIndex];
}
template <class ELT> inline const ELT & CBufferT <ELT> :: operator [] (int nIndex) const
{
return CBufferRefT<ELT>::m_pBuffer[nIndex];
}
template <class ELT> void CBufferT <ELT> :: Append(const ELT * pcsz, int length, int eol)
{
int nNewLength = m_nMaxLength;
// Check length
if(nNewLength < 8)
nNewLength = 8;
if(CBufferRefT <ELT> :: m_nSize + length + eol > nNewLength)
nNewLength *= 2;
if(CBufferRefT <ELT> :: m_nSize + length + eol > nNewLength)
{
nNewLength = CBufferRefT <ELT> :: m_nSize + length + eol + 11;
nNewLength -= nNewLength % 8;
}
// Realloc
if(nNewLength > m_nMaxLength)
{
CBufferRefT <ELT> :: m_pBuffer = (ELT *) realloc(CBufferRefT<ELT>::m_pBuffer, sizeof(ELT) * nNewLength);
m_nMaxLength = nNewLength;
}
// Append
memcpy(CBufferRefT<ELT>::m_pBuffer + CBufferRefT <ELT> :: m_nSize, pcsz, sizeof(ELT) * length);
CBufferRefT <ELT> :: m_nSize += length;
if(eol > 0) CBufferRefT<ELT>::m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> inline void CBufferT <ELT> :: Append(ELT el, int eol)
{
Append(&el, 1, eol);
}
template <class ELT> void CBufferT <ELT> :: Push(ELT el)
{
// Realloc
if(CBufferRefT <ELT> :: m_nSize >= m_nMaxLength)
{
int nNewLength = m_nMaxLength * 2;
if( nNewLength < 8 ) nNewLength = 8;
CBufferRefT <ELT> :: m_pBuffer = (ELT *) realloc(CBufferRefT<ELT>::m_pBuffer, sizeof(ELT) * nNewLength);
m_nMaxLength = nNewLength;
}
// Append
CBufferRefT<ELT>::m_pBuffer[CBufferRefT <ELT> :: m_nSize++] = el;
}
template <class ELT> void CBufferT <ELT> :: Push(const CBufferRefT<ELT> & buf)
{
for(int i=0; i<buf.GetSize(); i++)
{
Push(buf[i]);
}
Push((ELT)buf.GetSize());
}
template <class ELT> inline int CBufferT <ELT> :: Pop(ELT & el)
{
if(CBufferRefT <ELT> :: m_nSize > 0)
{
el = CBufferRefT<ELT>::m_pBuffer[--CBufferRefT <ELT> :: m_nSize];
return 1;
}
else
{
return 0;
}
}
template <class ELT> int CBufferT <ELT> :: Pop (CBufferT<ELT> & buf)
{
int size, res = 1;
res = res && Pop(*(ELT*)&size);
buf.Restore(size);
for(int i=size-1; i>=0; i--)
{
res = res && Pop(buf[i]);
}
return res;
}
template <class ELT> inline int CBufferT <ELT> :: Peek(ELT & el) const
{
if(CBufferRefT <ELT> :: m_nSize > 0)
{
el = CBufferRefT<ELT>::m_pBuffer[CBufferRefT <ELT> :: m_nSize - 1];
return 1;
}
else
{
return 0;
}
}
template <class ELT> const ELT * CBufferT <ELT> :: GetBuffer() const
{
static const ELT _def[] = {0}; return CBufferRefT<ELT>::m_pBuffer ? CBufferRefT<ELT>::m_pBuffer : _def;
}
template <class ELT> ELT * CBufferT <ELT> :: GetBuffer()
{
static const ELT _def[] = {0}; return CBufferRefT<ELT>::m_pBuffer ? CBufferRefT<ELT>::m_pBuffer : (ELT *)_def;
}
template <class ELT> ELT * CBufferT <ELT> :: Detach()
{
ELT * pBuffer = CBufferRefT<ELT>::m_pBuffer;
CBufferRefT <ELT> :: m_pBuffer = 0;
CBufferRefT <ELT> :: m_nSize = m_nMaxLength = 0;
return pBuffer;
}
template <class ELT> void CBufferT <ELT> :: Release()
{
ELT * pBuffer = Detach();
if(pBuffer != 0) free(pBuffer);
}
template <class ELT> void CBufferT <ELT> :: Prepare(int index, int fill)
{
int nNewSize = index + 1;
// Realloc
if(nNewSize > m_nMaxLength)
{
int nNewLength = m_nMaxLength;
if( nNewLength < 8 )
nNewLength = 8;
if( nNewSize > nNewLength )
nNewLength *= 2;
if( nNewSize > nNewLength )
{
nNewLength = nNewSize + 11;
nNewLength -= nNewLength % 8;
}
CBufferRefT <ELT> :: m_pBuffer = (ELT *) realloc(CBufferRefT<ELT>::m_pBuffer, sizeof(ELT) * nNewLength);
m_nMaxLength = nNewLength;
}
// size
if( CBufferRefT <ELT> :: m_nSize < nNewSize )
{
memset(CBufferRefT<ELT>::m_pBuffer + CBufferRefT <ELT> :: m_nSize, fill, sizeof(ELT) * (nNewSize - CBufferRefT <ELT> :: m_nSize));
CBufferRefT <ELT> :: m_nSize = nNewSize;
}
}
template <class ELT> inline void CBufferT <ELT> :: Restore(int size)
{
SetMaxLength(size);
CBufferRefT <ELT> :: m_nSize = size;
}
template <class ELT> CBufferT <ELT> :: ~CBufferT()
{
if(CBufferRefT<ELT>::m_pBuffer != 0) free(CBufferRefT<ELT>::m_pBuffer);
}
template <class T> class CSortedBufferT : public CBufferT <T>
{
public:
CSortedBufferT(int reverse = 0);
CSortedBufferT(int(*)(const void *, const void *));
public:
void Add(const T & rT);
void Add(const T * pT, int nSize);
int Remove(const T & rT);
void RemoveAll();
void SortFreeze() { m_bSortFreezed = 1; }
void SortUnFreeze();
public:
int Find(const T & rT, int(* compare)(const void *, const void *) = 0) { return FindAs(*(T*)&rT, compare); }
int FindAs(const T & rT, int(*)(const void *, const void *) = 0);
int GetSize() const { return CBufferRefT<T>::m_nSize; }
T & operator [] (int nIndex) { return CBufferT <T> :: operator [] (nIndex); }
protected:
int (* m_fncompare)(const void *, const void *);
static int compareT(const void *, const void *);
static int compareReverseT(const void *, const void *);
int m_bSortFreezed;
};
template <class T> CSortedBufferT <T> :: CSortedBufferT(int reverse)
{
m_fncompare = reverse ? compareReverseT : compareT;
m_bSortFreezed = 0;
}
template <class T> CSortedBufferT <T> :: CSortedBufferT(int (* compare)(const void *, const void *))
{
m_fncompare = compare;
m_bSortFreezed = 0;
}
template <class T> void CSortedBufferT <T> :: Add(const T & rT)
{
if(m_bSortFreezed != 0)
{
this->Append(rT);
return;
}
int a = 0, b = CBufferRefT<T>::m_nSize - 1, c = CBufferRefT<T>::m_nSize / 2;
while(a <= b)
{
int r = m_fncompare(&rT, &CBufferRefT<T>::m_pBuffer[c]);
if ( r < 0 ) b = c - 1;
else if( r > 0 ) a = c + 1;
else break;
c = (a + b + 1) / 2;
}
this->Insert(c, rT);
}
template <class T> void CSortedBufferT <T> :: Add(const T * pT, int nSize)
{
Append(pT, nSize);
if(m_bSortFreezed == 0)
{
qsort(CBufferRefT<T>::m_pBuffer, CBufferRefT<T>::m_nSize, sizeof(T), m_fncompare);
}
}
template <class T> int CSortedBufferT <T> :: FindAs(const T & rT, int(* compare)(const void *, const void *))
{
const T * pT = (const T *)bsearch(&rT, CBufferRefT<T>::m_pBuffer, CBufferRefT<T>::m_nSize, sizeof(T), compare == 0 ? m_fncompare : compare);
if( pT != NULL )
return pT - CBufferRefT<T>::m_pBuffer;
else
return -1;
}
template <class T> int CSortedBufferT <T> :: Remove(const T & rT)
{
int pos = Find(rT);
if( pos >= 0 ) CBufferT <T> :: Remove(pos);
return pos;
}
template <class T> inline void CSortedBufferT <T> :: RemoveAll()
{
CBufferT<T>::Restore(0);
}
template <class T> void CSortedBufferT <T> :: SortUnFreeze()
{
if(m_bSortFreezed != 0)
{
m_bSortFreezed = 0;
qsort(CBufferRefT<T>::m_pBuffer, CBufferRefT<T>::m_nSize, sizeof(T), m_fncompare);
}
}
template <class T> int CSortedBufferT <T> :: compareT(const void * elem1, const void * elem2)
{
if( *(const T *)elem1 == *(const T *)elem2 )
return 0;
else if( *(const T *)elem1 < *(const T *)elem2 )
return -1;
else
return 1;
}
template <class T> int CSortedBufferT <T> :: compareReverseT(const void * elem1, const void * elem2)
{
if( *(const T *)elem1 == *(const T *)elem2 )
return 0;
else if( *(const T *)elem1 > *(const T *)elem2 )
return -1;
else
return 1;
}
//
// Context
//
class CContext
{
public:
CBufferT <int> m_stack;
CBufferT <int> m_capturestack, m_captureindex;
public:
int m_nCurrentPos;
int m_nBeginPos;
int m_nLastBeginPos;
int m_nParenZindex;
int m_nCursiveLimit;
void * m_pMatchString;
int m_pMatchStringLength;
};
class CContextShot
{
public:
CContextShot(CContext * pContext)
{
m_nCurrentPos = pContext->m_nCurrentPos;
nsize = pContext->m_stack.GetSize();
ncsize = pContext->m_capturestack.GetSize();
}
void Restore(CContext * pContext)
{
pContext->m_stack.Restore(nsize);
pContext->m_capturestack.Restore(ncsize);
pContext->m_nCurrentPos = m_nCurrentPos;
}
public:
int m_nCurrentPos;
int nsize ;
int ncsize;
};
//
// Interface
//
class ElxInterface
{
public:
virtual int Match (CContext * pContext) const = 0;
virtual int MatchNext(CContext * pContext) const = 0;
public:
virtual ~ElxInterface() {};
};
//
// Alternative
//
template <int x> class CAlternativeElxT : public ElxInterface
{
public:
int Match (CContext * pContext) const;
int MatchNext(CContext * pContext) const;
public:
CAlternativeElxT();
public:
CBufferT <ElxInterface *> m_elxlist;
};
typedef CAlternativeElxT <0> CAlternativeElx;
//
// Assert
//
template <int x> class CAssertElxT : public ElxInterface
{
public:
int Match (CContext * pContext) const;
int MatchNext(CContext * pContext) const;
public:
CAssertElxT(ElxInterface * pelx, int byes = 1);
public:
ElxInterface * m_pelx;
int m_byes;
};
typedef CAssertElxT <0> CAssertElx;
//
// Back reference elx
//
template <class CHART> class CBackrefElxT : public ElxInterface
{
public:
int Match (CContext * pContext) const;
int MatchNext(CContext * pContext) const;
public:
CBackrefElxT(int nnumber, int brightleft, int bignorecase);
public:
int m_nnumber;
int m_brightleft;
int m_bignorecase;
CBufferT <CHART> m_szNamed;
};
//
// Implementation
//
template <class CHART> CBackrefElxT <CHART> :: CBackrefElxT(int nnumber, int brightleft, int bignorecase)
{
m_nnumber = nnumber;
m_brightleft = brightleft;
m_bignorecase = bignorecase;
}
template <class CHART> int CBackrefElxT <CHART> :: Match(CContext * pContext) const
{
// check number, for named
if( m_nnumber < 0 || m_nnumber >= pContext->m_captureindex.GetSize() ) return 0;
int index = pContext->m_captureindex[m_nnumber];
if( index < 0 ) return 0;
// check enclosed
int pos1 = pContext->m_capturestack[index + 1];
int pos2 = pContext->m_capturestack[index + 2];
if( pos2 < 0 ) pos2 = pContext->m_nCurrentPos;
// info
int lpos = pos1 < pos2 ? pos1 : pos2;
int rpos = pos1 < pos2 ? pos2 : pos1;
int slen = rpos - lpos;
const CHART * pcsz = (const CHART *)pContext->m_pMatchString;
int npos = pContext->m_nCurrentPos;
int tlen = pContext->m_pMatchStringLength;
// compare
int bsucc;
CBufferRefT <CHART> refstr(pcsz + lpos, slen);
if( m_brightleft )
{
if(npos < slen)
return 0;
if(m_bignorecase)
bsucc = ! refstr.nCompareNoCase(pcsz + (npos - slen));
else
bsucc = ! refstr.nCompare (pcsz + (npos - slen));
if( bsucc )
{
pContext->m_stack.Push(npos);
pContext->m_nCurrentPos -= slen;
}
}
else
{
if(npos + slen > tlen)
return 0;
if(m_bignorecase)
bsucc = ! refstr.nCompareNoCase(pcsz + npos);
else
bsucc = ! refstr.nCompare (pcsz + npos);
if( bsucc )
{
pContext->m_stack.Push(npos);
pContext->m_nCurrentPos += slen;
}
}
return bsucc;
}
template <class CHART> int CBackrefElxT <CHART> :: MatchNext(CContext * pContext) const
{
int npos = 0;
pContext->m_stack.Pop(npos);
pContext->m_nCurrentPos = npos;
return 0;
}
// RCHART
#ifndef RCHART
#define RCHART(ch) ((CHART)ch)
#endif
// BOUNDARY_TYPE
enum BOUNDARY_TYPE
{
BOUNDARY_FILE_BEGIN, // begin of whole text
BOUNDARY_FILE_END , // end of whole text
BOUNDARY_FILE_END_N, // end of whole text, or before newline at the end
BOUNDARY_LINE_BEGIN, // begin of line
BOUNDARY_LINE_END , // end of line
BOUNDARY_WORD_BEGIN, // begin of word
BOUNDARY_WORD_END , // end of word
BOUNDARY_WORD_EDGE
};
//
// Boundary Elx
//
template <class CHART> class CBoundaryElxT : public ElxInterface
{
public:
int Match (CContext * pContext) const;
int MatchNext(CContext * pContext) const;
public:
CBoundaryElxT(int ntype, int byes = 1);
protected:
static int IsWordChar(CHART ch);
public:
int m_ntype;
int m_byes;
};
//
// Implementation
//
template <class CHART> CBoundaryElxT <CHART> :: CBoundaryElxT(int ntype, int byes)
{
m_ntype = ntype;
m_byes = byes;
}
template <class CHART> int CBoundaryElxT <CHART> :: Match(CContext * pContext) const
{
const CHART * pcsz = (const CHART *)pContext->m_pMatchString;
int npos = pContext->m_nCurrentPos;
int tlen = pContext->m_pMatchStringLength;
CHART chL = npos > 0 ? pcsz[npos - 1] : 0;
CHART chR = npos < tlen ? pcsz[npos ] : 0;
int bsucc = 0;
switch(m_ntype)
{
case BOUNDARY_FILE_BEGIN:
bsucc = (npos <= 0);
break;
case BOUNDARY_FILE_END:
bsucc = (npos >= tlen);
break;
case BOUNDARY_FILE_END_N:
bsucc = (npos >= tlen) || (pcsz[tlen-1] == RCHART('\n') && (npos == tlen-1 || (pcsz[tlen-2] == RCHART('\r') && npos == tlen-2)));
break;
case BOUNDARY_LINE_BEGIN:
bsucc = (npos <= 0 ) || (chL == RCHART('\n')) || ((chL == RCHART('\r')) && (chR != RCHART('\n')));
break;
case BOUNDARY_LINE_END:
bsucc = (npos >= tlen) || (chR == RCHART('\r')) || ((chR == RCHART('\n')) && (chL != RCHART('\r')));
break;
case BOUNDARY_WORD_BEGIN:
bsucc = ! IsWordChar(chL) && IsWordChar(chR);
break;
case BOUNDARY_WORD_END:
bsucc = IsWordChar(chL) && ! IsWordChar(chR);
break;
case BOUNDARY_WORD_EDGE:
bsucc = IsWordChar(chL) ? ! IsWordChar(chR) : IsWordChar(chR);
break;
}
return m_byes ? bsucc : ! bsucc;
}
template <class CHART> int CBoundaryElxT <CHART> :: MatchNext(CContext *) const
{
return 0;
}
template <class CHART> inline int CBoundaryElxT <CHART> :: IsWordChar(CHART ch)
{
return (ch >= RCHART('A') && ch <= RCHART('Z')) || (ch >= RCHART('a') && ch <= RCHART('z')) || (ch >= RCHART('0') && ch <= RCHART('9')) || (ch == RCHART('_'));
}
//
// Bracket
//
template <class CHART> class CBracketElxT : public ElxInterface
{
public:
int Match (CContext * pContext) const;
int MatchNext(CContext * pContext) const;
public:
CBracketElxT(int nnumber, int bright);
static int CheckCaptureIndex(int & index, CContext * pContext, int number);
public:
int m_nnumber;
int m_bright;
CBufferT <CHART> m_szNamed;
};
template <class CHART> CBracketElxT <CHART> :: CBracketElxT(int nnumber, int bright)
{
m_nnumber = nnumber;
m_bright = bright;
}
template <class CHART> inline int CBracketElxT <CHART> :: CheckCaptureIndex(int & index, CContext * pContext, int number)
{
if( index >= pContext->m_capturestack.GetSize() )
index = pContext->m_capturestack.GetSize() - 4;
while(index >= 0)
{
if(pContext->m_capturestack[index] == number)
{
return 1;
}
index -= 4;
}
return 0;
}
//
// capturestack[index+0] => Group number
// capturestack[index+1] => Capture start pos
// capturestack[index+2] => Capture end pos
// capturestack[index+3] => Capture enclose z-index, zindex<0 means inner group with same name
//
template <class CHART> int CBracketElxT <CHART> :: Match(CContext * pContext) const
{
// check, for named
if(m_nnumber < 0) return 0;
if( ! m_bright )
{
pContext->m_captureindex.Prepare(m_nnumber, -1);
int index = pContext->m_captureindex[m_nnumber];
// check
if(CheckCaptureIndex(index, pContext, m_nnumber) && pContext->m_capturestack[index+2] < 0)
{
pContext->m_capturestack[index+3] --;
return 1;
}
// save
pContext->m_captureindex[m_nnumber] = pContext->m_capturestack.GetSize();
pContext->m_capturestack.Push(m_nnumber);
pContext->m_capturestack.Push(pContext->m_nCurrentPos);
pContext->m_capturestack.Push(-1);
pContext->m_capturestack.Push( 0); // z-index
}
else
{
// check
int index = pContext->m_captureindex[m_nnumber];
if(CheckCaptureIndex(index, pContext, m_nnumber))
{
if(pContext->m_capturestack[index + 3] < 0) // check inner group with same name
{
pContext->m_capturestack[index + 3] ++;
return 1;
}
// save
pContext->m_capturestack[index + 2] = pContext->m_nCurrentPos;
pContext->m_capturestack[index + 3] = pContext->m_nParenZindex ++;
}
}
return 1;
}
template <class CHART> int CBracketElxT <CHART> :: MatchNext(CContext * pContext) const
{
int index = pContext->m_captureindex[m_nnumber];
if( ! CheckCaptureIndex(index, pContext, m_nnumber) )
{
return 0;
}
if( ! m_bright )
{
if(pContext->m_capturestack[index + 3] < 0)
{
pContext->m_capturestack[index + 3] ++;