-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextUtilities.cs
1368 lines (1084 loc) · 42.8 KB
/
TextUtilities.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Xml;
namespace Microsoft.SharePointIntegration
{
/// <summary>
/// Static class with general utilities for manipulating strings.
/// </summary>
public static partial class TextUtilities
{
public static byte[] GetBytesFromString(String str)
{
return Encoding.UTF8.GetBytes(str);
}
public static char CharAt(this String str, int index)
{
return str[index];
}
public static String GetSimplifiedString(String name)
{
String result = String.Empty;
foreach (char c in name)
{
if (Char.IsDigit(c) || Char.IsLetter(c))
{
result += c;
}
}
return result;
}
public static String Normalize(String name)
{
String normalizedName= String.Empty;
foreach (Char c in name)
{
if (Char.IsLetterOrDigit(c))
{
normalizedName += c;
}
}
return normalizedName;
}
public static IEnumerable<int> GetWordInstances(String source, String word, StringComparison comparisonType)
{
List<int> instances = null;
int i = source.IndexOf(word, comparisonType);
while (i >= 0)
{
bool isWord = true;
if (i > 0)
{
isWord = TextUtilities.IsBreakerChar(source[i - 1]);
}
if (isWord && i + word.Length < source.Length - 1)
{
isWord = TextUtilities.IsBreakerChar(source[i + word.Length]);
}
if (isWord)
{
if (instances == null)
{
instances = new List<int>();
}
instances.Add(i);
}
i = source.IndexOf(word, i + 1, comparisonType);
}
return instances;
}
public static bool ContainsWord(String source, String word, StringComparison comparisonType)
{
int i = source.IndexOf(word, comparisonType);
while (i >= 0)
{
bool isWord = true;
if (i > 0)
{
isWord = TextUtilities.IsBreakerChar(source[i - 1]);
}
if (isWord && i + word.Length < source.Length - 1)
{
isWord = TextUtilities.IsBreakerChar(source[i + word.Length]);
}
if (isWord)
{
return true;
}
i = source.IndexOf(word, i + 1, comparisonType);
}
return false;
}
public static bool IsInteger(String text)
{
int result = 0;
return Int32.TryParse(text, out result);
}
public static bool IsBreakerChar(char cand)
{
return Char.IsControl(cand) || Char.IsPunctuation(cand) || Char.IsSeparator(cand);
}
public static String RemoveTextToLeftOfNewLine(String text, int index)
{
if (index > text.Length)
{
index = text.Length;
}
int left = -1;
int nextChar = text.LastIndexOf("\r", index);
if (nextChar > left)
{
left = nextChar;
}
nextChar = text.LastIndexOf("\n", index);
if (nextChar > left)
{
left = nextChar;
}
nextChar = text.LastIndexOf("\t", index);
if (nextChar > left)
{
left = nextChar;
}
if (left > 0)
{
text = text.Substring(left +1, text.Length - (left + 1));
}
return text;
}
public static String RemoveTextToRightOfNewLine(String text, int index)
{
if (index < 0)
{
index = 0;
}
int right = text.Length + 1;
int nextChar = text.IndexOf("\r", index);
if (nextChar < right && nextChar >= 0)
{
right = nextChar;
}
nextChar = text.IndexOf("\n", index);
if (nextChar < right && nextChar >= 0)
{
right = nextChar;
}
nextChar = text.IndexOf("\t", index);
if (nextChar < right && nextChar >= 0)
{
right = nextChar;
}
if (right < text.Length)
{
text = text.Substring(0, right);
}
return text;
}
public static void ConvertAllTagsToSingleton(ref String source, String tagName)
{
Regex regexSpecificTagStart = new Regex("<" + tagName + "(\\s|>)");
Match matchSpecificTagStart = regexSpecificTagStart.Match(source);
while (matchSpecificTagStart.Success)
{
int i = matchSpecificTagStart.Index;
int nextTagStart= source.IndexOf("<", i + 1);
int nextStartTagEnd= source.IndexOf(">", i + 1);
int nextSingletonTagEnd = source.IndexOf("/>", i + 1);
if ( nextSingletonTagEnd < 0 || // make sure this tag is not a singleton
(nextTagStart > i && nextSingletonTagEnd > nextTagStart) )
{
source = source.Substring(0, nextStartTagEnd) + "/" + source.Substring(nextStartTagEnd, source.Length - nextStartTagEnd);
}
else
matchSpecificTagStart = regexSpecificTagStart.Match(source, nextSingletonTagEnd);
}
source = source.Replace("</" + tagName + ">", "");
}
public static void EnsureExplicitEndTag(ref String source, String tagName, String[] pseudoEndTags)
{
Regex regexSpecificTagStart = new Regex("<" + tagName + "(\\s|>)");
Match matchSpecificTagStart = regexSpecificTagStart.Match(source);
while (matchSpecificTagStart.Success)
{
int i = matchSpecificTagStart.Index;
int iNextTagStart= source.IndexOf("<", i + 1);
int iNextStartTagEnd= source.IndexOf(">", i + 1);
int iNextSingletonTagEnd = source.IndexOf("/>", i + 1);
if ( iNextSingletonTagEnd < 0 || // make sure this tag is not a singleton
(iNextTagStart > i && iNextSingletonTagEnd > iNextTagStart) )
{
int iEndTag = source.IndexOf("</" + tagName + ">", i + 1);
int iNextPseudoEnd = -1;
foreach (String sPseudoEnd in pseudoEndTags)
{
int iPseudoEnd = source.IndexOf("<" + sPseudoEnd, i + 1);
if (iPseudoEnd > 0 && (iNextPseudoEnd < 0 || iPseudoEnd < iNextPseudoEnd))
{
iNextPseudoEnd = iPseudoEnd;
}
}
if ( (iEndTag < 0 && iNextPseudoEnd > 0) ||
(iEndTag > 0 && iNextPseudoEnd > 0 && iEndTag > iNextPseudoEnd) )
{
source = source.Substring(0, iNextPseudoEnd) + "</" + tagName + ">" + source.Substring(iNextPseudoEnd, source.Length - iNextPseudoEnd);
}
matchSpecificTagStart = regexSpecificTagStart.Match(source, i + 1);
}
else
matchSpecificTagStart = regexSpecificTagStart.Match(source, iNextSingletonTagEnd);
}
}
public static void MakeTagNameUnderneathTagFirst(ref String source, String underneathTagName, String tagName)
{
Regex regexSpecificTagStart = new Regex("<" + underneathTagName + "(\\s|>)");
Match matchSpecificTagStart = regexSpecificTagStart.Match(source);
while (matchSpecificTagStart.Success)
{
int i = matchSpecificTagStart.Index;
int iNextTagStart= source.IndexOf("<", i + 1);
int iNextStartTagEnd= source.IndexOf(">", i + 1);
int iNextSingletonTagEnd = source.IndexOf("/>", i + 1);
int iNextComplexTagEnd = GetEndOfTag(source, underneathTagName, i);
if ( iNextSingletonTagEnd < 0 || // make sure this tag is not a singleton
(iNextTagStart > i && iNextSingletonTagEnd > iNextTagStart) )
{
iNextStartTagEnd++;
int iLast = source.LastIndexOf("</", iNextComplexTagEnd);
String sTag = source.Substring(iNextStartTagEnd, iLast - iNextStartTagEnd );
String sDiscard= StripTagsOfTypeAndTheirContents(ref sTag, tagName, true);
MakeTagNameUnderneathTagFirst(ref sTag, underneathTagName, tagName);
sTag = sDiscard + sTag;
source = source.Substring(0, iNextStartTagEnd) + sTag + source.Substring(iLast, source.Length - iLast);
matchSpecificTagStart = regexSpecificTagStart.Match(source, iNextComplexTagEnd);
}
else
matchSpecificTagStart = regexSpecificTagStart.Match(source, iNextSingletonTagEnd);
}
}
public static int GetMatchingEndCharacter(String source, String starterTags, String endTags, int startIndex)
{
Regex regexEnd = new Regex(endTags);
Regex regexStart = new Regex(starterTags);
// really find the start of the tag (should match startIndex)
Match matchStart = regexStart.Match(source, startIndex);
Match matchEnd = regexEnd.Match(source, startIndex);
int iStartTags = 1;
while (iStartTags > 0 && matchEnd.Success)
{
if ( matchEnd.Index < matchStart.Index ||
!matchStart.Success)
{
iStartTags--;
if (iStartTags > 0)
matchEnd = regexEnd.Match(source, matchEnd.Index + 1);
}
// is the next thing a tag start?
else if ( matchStart.Success &&
matchStart.Index < matchEnd.Index)
{
iStartTags++;
matchStart = regexStart.Match(source, matchStart.Index + 2);
}
else
{
Debug.Assert(false, "Warning: malformed tags detected in source '" + source + "' (mismatched tree)");
return -1;
}
}
if (matchEnd.Success)
{
return matchEnd.Index + matchEnd.Length;
}
// Console.WriteLine("Warning: malformed tags detected in source '" + source + "' (no end tag)");
return -1;
}
public static String GetTagInnerXml(String source, String tagName)
{
return GetTagInnerXml(source, tagName, 0);
}
public static String GetTagInnerXml(String source, String tagName, int startIndex)
{
Regex regexSpecificTagStart = new Regex("<" + tagName + "(\\s|/|>)");
// really find the start of the tag (should match startIndex)
Match matchStart = regexSpecificTagStart.Match(source, startIndex);
if (!matchStart.Success)
{
//Console.WriteLine("Warning: could not find start tag '" + tagName + "'.");
return null;
}
int endOfStartTag = source.IndexOf(">", matchStart.Index) + 1;
if (endOfStartTag <= 1)
{
//Console.WriteLine("Warning: could not find start tag '" + tagName + "'.");
return null;
}
int endOfEndTag = GetEndOfTag(source, tagName, matchStart.Index);
if (endOfStartTag == endOfEndTag)
{
return String.Empty;
}
int startOfEndTag = source.LastIndexOf("<", endOfEndTag - 1);
return source.Substring(endOfStartTag, startOfEndTag - endOfStartTag);
}
public static int GetStartOfTag(String source, String tagName, int startIndex)
{
tagName = tagName.Replace("?", "\\?");
Regex regexSpecificTagStart = new Regex("<" + tagName + "(\\s|/|>)");
Match matchNextTagStart = regexSpecificTagStart.Match(source, startIndex);
if (matchNextTagStart.Success)
{
return matchNextTagStart.Index;
}
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="source">
/// </param>
/// <param name="tagName">
/// </param>
/// <param name="startIndex">
/// </param>
/// <returns>
/// The index of the first character after the end of this tag, or -1 if this is a
/// malformed source.
/// </returns>
public static int GetEndOfTag(String source, String tagName, int startIndex)
{
Regex regexSpecificTagStart;
Regex regexSingleTagEnd = new Regex("/>");
Regex regexComplexTagEnd;
Regex regexTagStart = new Regex("<[\\w|?]");
bool findComplexEndAndLeave = false;
if (tagName == "?xml")
{
regexSpecificTagStart = new Regex("<\\?xml(\\s|/|>)");
regexComplexTagEnd = new Regex(">");
findComplexEndAndLeave = true;
}
else if (tagName == "![CDATA[")
{
regexSpecificTagStart = new Regex("<!\\[CDATA\\[");
regexComplexTagEnd = new Regex("]]>");
findComplexEndAndLeave = true;
}
else if (tagName.Length > 8 && tagName.Substring(0, 8) == "![CDATA[")
{
regexSpecificTagStart = new Regex("<!\\[CDATA\\[");
regexComplexTagEnd = new Regex("]]>");
findComplexEndAndLeave = true;
}
else
{
regexSpecificTagStart = new Regex("<" + tagName + "(\\s|/|>)");
if (tagName == "!--")
{
regexComplexTagEnd = new Regex("-->");
findComplexEndAndLeave = true;
}
else
{
regexComplexTagEnd = new Regex("</" + tagName + ">");
}
}
// really find the start of the tag (should match startIndex)
Match matchStart = regexSpecificTagStart.Match(source, startIndex);
if (!matchStart.Success)
{
throw new MalformedXmlException("Warning: Could not find and end to the tag '" + tagName + "'.", source);
}
// find the end of the start tag
int iEndOfStartTagInitial = matchStart.Index + matchStart.Length - 1;
// find next </Foo>
Match matchNextComplexCloser = regexComplexTagEnd.Match(source, iEndOfStartTagInitial);
while (!findComplexEndAndLeave && matchNextComplexCloser.Success && IsInsideOfIgnoredArea(ref source, matchNextComplexCloser.Index))
{
matchNextComplexCloser = regexComplexTagEnd.Match(source, matchNextComplexCloser.Index + 1);
}
if (findComplexEndAndLeave && matchNextComplexCloser.Success)
{
return matchNextComplexCloser.Index + matchNextComplexCloser.Length;
}
Debug.Assert(!findComplexEndAndLeave);
// find next <Foo
Match matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, iEndOfStartTagInitial);
while (matchNextSpecificSingleStart.Success && IsInsideOfIgnoredArea(ref source, matchNextSpecificSingleStart.Index))
{
matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, matchNextSpecificSingleStart.Index + 1);
}
// find next />
Match matchNextSingleCloser = regexSingleTagEnd.Match(source, iEndOfStartTagInitial);
while (matchNextSingleCloser.Success && IsInsideOfIgnoredArea(ref source, matchNextSingleCloser.Index))
{
matchNextSingleCloser = regexSingleTagEnd.Match(source, matchNextSingleCloser.Index + 1);
}
// find next <
Match matchNextSingleStart = regexTagStart.Match(source, iEndOfStartTagInitial);
while (matchNextSingleStart.Success && IsInsideOfIgnoredArea(ref source, matchNextSingleStart.Index))
{
matchNextSingleStart = regexTagStart.Match(source, matchNextSingleStart.Index + 1);
}
// are we dealing with a singleton (<Foo/>) tag? if so, just kill it.
if ( matchNextSingleCloser.Success &&
( !matchNextSingleStart.Success ||
matchNextSingleCloser.Index < matchNextSingleStart.Index) &&
( !matchNextComplexCloser.Success ||
matchNextSingleCloser.Index < matchNextComplexCloser.Index)
)
{
return matchNextSingleCloser.Index + 2;
}
// ok, it's a complex tag. We need to maintain a count of the number of start tags
// we have seen, and subtract out the number of closer tags we have seen
// when starttags == 0, we know we are done.
int iStartTags = 1;
while (iStartTags > 0 && matchNextComplexCloser.Success)
{
if ( matchNextComplexCloser.Index < matchNextSpecificSingleStart.Index ||
!matchNextSpecificSingleStart.Success)
{
iStartTags--;
if (iStartTags > 0)
{
matchNextComplexCloser = regexComplexTagEnd.Match(source, matchNextComplexCloser.Index + 1);
while (matchNextComplexCloser.Success && IsInsideOfIgnoredArea(ref source, matchNextComplexCloser.Index))
{
matchNextComplexCloser = regexComplexTagEnd.Match(source, matchNextComplexCloser.Index + 1);
}
}
}
// is the next thing a tag start?
else if ( matchNextSpecificSingleStart.Success &&
matchNextSpecificSingleStart.Index < matchNextComplexCloser.Index)
{
iStartTags++;
// see if this is a singleton tag... if so, subtract the count back out.
matchNextSingleCloser = regexSingleTagEnd.Match(source, matchNextSpecificSingleStart.Index + 1);
matchNextSingleStart = regexTagStart.Match(source, matchNextSpecificSingleStart.Index + 1);
if ( matchNextSingleCloser.Success && // we've found a />
matchNextSingleCloser.Index < matchNextComplexCloser.Index && // it comes before the next </Foo>
( matchNextSingleCloser.Index < matchNextSingleStart.Index || // it comes before the next <Bar> or <Foo> or whatever.
!matchNextSingleStart.Success))
{
iStartTags--;
}
matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, matchNextSpecificSingleStart.Index + 2);
while (matchNextSpecificSingleStart.Success && IsInsideOfIgnoredArea(ref source, matchNextSpecificSingleStart.Index))
{
matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, matchNextSpecificSingleStart.Index + 1);
}
}
else
{
throw new MalformedXmlException("Warning: malformed tags detected in source (mismatched tree)", source);
}
}
if (matchNextComplexCloser.Success)
{
int iComplexEnd = source.IndexOf(">", matchNextComplexCloser.Index);
// for any complex tag ender </, there should be a > that follows it. if not, bail.
if (iComplexEnd >= 0)
{
iComplexEnd++;
return iComplexEnd;
}
}
throw new MalformedXmlException("Warning: malformed tags detected in source (no end tag)", source);
}
/// <summary>
/// This derivation of GetEndOfTag accepts markup more tolerant than strict XML rules, and will accept things like <div><img src="fa"></div>
/// </summary>
/// <param name="source"></param>
/// <param name="tagName"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int GetEndOfTagTolerant(String source, String tagName, int startIndex)
{
Regex regexSpecificTagStart = new Regex("<" + tagName + "(\\s|>)");
Regex regexSingleTagEnd = new Regex("/>");
Regex regexComplexTagEnd = new Regex("</" + tagName + ">");
Regex regexTagStart = new Regex("<[\\w|?]");
// really find the start of the tag (should match startIndex)
Match matchStart = regexSpecificTagStart.Match(source, startIndex);
if (!matchStart.Success)
{
// Console.WriteLine("Warning: could not refind start tag '" + tagName + "'.");
return -1;
}
// find the end of the start tag
int iEndOfStartTagInitial = matchStart.Index + matchStart.Length;
// find next </Foo>
Match matchNextComplexCloser = regexComplexTagEnd.Match(source, iEndOfStartTagInitial);
// find next <Foo
Match matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, iEndOfStartTagInitial);
// find next />
Match matchNextSingleCloser = regexSingleTagEnd.Match(source, iEndOfStartTagInitial);
// find next <
Match matchNextSingleStart = regexTagStart.Match(source, iEndOfStartTagInitial);
// are we dealing with a singleton (<Foo/>) tag? if so, just kill it.
if (matchNextSingleCloser.Success &&
(!matchNextSingleStart.Success ||
matchNextSingleCloser.Index < matchNextSingleStart.Index) &&
(!matchNextComplexCloser.Success ||
matchNextSingleCloser.Index < matchNextComplexCloser.Index)
)
{
return matchNextSingleCloser.Index + 2;
}
// ok, it's a complex tag. We need to maintain a count of the number of start tags
// we have seen, and subtract out the number of closer tags we have seen
// when starttags == 0, we know we are done.
int iStartTags = 1;
while (iStartTags > 0 && matchNextComplexCloser.Success)
{
if (matchNextComplexCloser.Index < matchNextSpecificSingleStart.Index ||
!matchNextSpecificSingleStart.Success)
{
iStartTags--;
if (iStartTags > 0)
matchNextComplexCloser = regexComplexTagEnd.Match(source, matchNextComplexCloser.Index + 1);
}
// is the next thing a tag start?
else if (matchNextSpecificSingleStart.Success &&
matchNextSpecificSingleStart.Index < matchNextComplexCloser.Index)
{
iStartTags++;
// see if this is a singleton tag... if so, subtract the count back out.
matchNextSingleCloser = regexSingleTagEnd.Match(source, matchNextSpecificSingleStart.Index + 1);
matchNextSingleStart = regexTagStart.Match(source, matchNextSpecificSingleStart.Index + 1);
if (matchNextSingleCloser.Success && // we've found a />
matchNextSingleCloser.Index < matchNextComplexCloser.Index && // it comes before the next </Foo>
(matchNextSingleCloser.Index < matchNextSingleStart.Index || // it comes before the next <Bar> or <Foo> or whatever.
!matchNextSingleStart.Success))
{
iStartTags--;
}
matchNextSpecificSingleStart = regexSpecificTagStart.Match(source, matchNextSpecificSingleStart.Index + 2);
}
else
{
// Console.WriteLine("Warning: malformed tags detected in source '" + source + "' (mismatched tree)");
return -1;
}
}
if (matchNextComplexCloser.Success)
{
int iComplexEnd = source.IndexOf(">", matchNextComplexCloser.Index);
// for any complex tag ender </, there should be a > that follows it. if not, bail.
if (iComplexEnd >= 0)
{
iComplexEnd++;
return iComplexEnd;
}
}
// Console.WriteLine("Warning: malformed tags detected in source '" + source + "' (no end tag)");
return -1;
}
private static bool IsInsideOfIgnoredArea(ref String content, int index)
{
int lastTag = content.LastIndexOf("<![CDATA[", index + 1);
if (lastTag >= 0)
{
int lastEnd = content.LastIndexOf("]]>", index + 1);
if (lastEnd < lastTag)
{
return true;
}
}
return false;
}
public static int CountCharsInBetween(ref String source, String token, int start, int end)
{
int count = 0;
int next = source.IndexOf(token, start);
while (next >= 0 && next < end)
{
count++;
next = source.IndexOf(token, next + token.Length);
}
return count;
}
public static bool IsInQuote(ref String source, int index)
{
if (index < 0)
{
return false;
}
return ((TextUtilities.CountCharsInBetween(ref source, "\"", 0, index) % 2) == 1);
}
public static bool IsInComment(String source, int index)
{
int lastStart = source.LastIndexOf("/*", index);
if (lastStart >= 0)
{
int lastEnd = source.LastIndexOf("*/", index);
if (lastEnd < lastStart)
{
return true;
}
}
lastStart = source.LastIndexOf("//", index);
if (lastStart >= 0)
{
int lastEnd = source.LastIndexOf("\n", index);
if (lastEnd < lastStart)
{
return true;
}
}
return false;
}
public static int GetStartOfComment(String source, int index)
{
return GetStartOfComment(source, index, "/*", "//", "<!--");
}
private static int GetStartOfComment(String source, int index, String slashStar, String slashSlash, String lessThanQuestionMark)
{
int previousSlashStar = source.LastIndexOf(slashStar, index);
int previousSlashSlash = source.LastIndexOf(slashSlash, index);
int previousLessThanQuestionMark = source.LastIndexOf(lessThanQuestionMark, index);
if (previousSlashStar >= 0 && previousSlashSlash < previousSlashStar && previousLessThanQuestionMark < previousSlashStar)
{
return previousSlashStar;
}
if (previousSlashSlash >= 0 && previousLessThanQuestionMark < previousSlashSlash)
{
previousSlashSlash = source.LastIndexOf('\n', previousSlashSlash) + 1;
return previousSlashSlash;
}
return previousLessThanQuestionMark;
}
public static int GetEndOfComment(String source, int index)
{
int start = GetStartOfComment(source, index);
Debug.Assert(start >= 0);
String end;
if (source.Substring(start, 2) == "/*")
{
end = "*/";
}
else if (source.Substring(start, 4) == "<!--")
{
end = "-->";
}
else
{
end = "\n";
}
return source.IndexOf(end, index) + end.Length;
}
public static int GetTagDepth(String source, int tagStartIndex)
{
int iTagDepth = 0;
String sPrior = source.Substring(0, tagStartIndex);
Regex regexSingleTagEnd = new Regex("/>");
Regex regexComplexTagEnd = new Regex("</");
Regex regexTagStart = new Regex("<[\\w|?]");
Match matchNextTagStart = regexTagStart.Match(sPrior);
Match matchNextTagEnd = regexComplexTagEnd.Match(sPrior);
while (matchNextTagStart.Success || matchNextTagEnd.Success)
{
if ( (matchNextTagEnd.Index < matchNextTagStart.Index && matchNextTagEnd.Success) ||
!matchNextTagStart.Success)
{
iTagDepth --;
matchNextTagEnd = regexComplexTagEnd.Match(sPrior, matchNextTagEnd.Index + 1);
}
// is the next thing a tag start?
else if ( (matchNextTagStart.Index < matchNextTagEnd.Index && matchNextTagStart.Success) ||
!matchNextTagEnd.Success)
{
iTagDepth++;
Match matchNextSingleTagEnd = regexSingleTagEnd.Match(sPrior, matchNextTagStart.Index + 2);
matchNextTagStart = regexTagStart.Match(sPrior, matchNextTagStart.Index + 1);
// see if this is a singleton tag... if so, subtract the count back out.
if (matchNextSingleTagEnd.Success)
{
if ( (!matchNextTagStart.Success ||
matchNextSingleTagEnd.Index < matchNextTagStart.Index) &&
(!matchNextTagEnd.Success ||
matchNextSingleTagEnd.Index < matchNextTagEnd.Index))
{
iTagDepth--;
}
}
}
else
{
Console.WriteLine("Warning: malformed tags detected in sPrior '" + sPrior + "' (mismatched tree)");
return -1;
}
}
return iTagDepth;
}
public static void ConvertTagNameUnderneathTag(ref String source, String underneathTagName, String oldTagName, String newTagName)
{
Regex regexSpecificTagStart = new Regex("<" +underneathTagName + "(\\s|>)");
Match matchStart = regexSpecificTagStart.Match(source);
while (matchStart.Success)
{
int iEndOfTag= GetEndOfTag(source, underneathTagName, matchStart.Index);
if ( iEndOfTag >= 0 )
{
String sTag = source.Substring(matchStart.Index, iEndOfTag - matchStart.Index);
ConvertTagName(ref sTag, oldTagName, newTagName);
source = source.Substring(0, matchStart.Index) + sTag + source.Substring(iEndOfTag, source.Length - iEndOfTag);
}
matchStart = regexSpecificTagStart.Match(source, matchStart.Index + 1);
}
}
public static void ConvertTagName(ref String source, String oldTagName, String newTagName)
{
// stupid but effective...
source = source.Replace("<" + oldTagName + "\t", "<" + newTagName + "\t");
source = source.Replace("<" + oldTagName + " ", "<" + newTagName + " ");
source = source.Replace("<" + oldTagName + ">", "<" + newTagName + ">");
source = source.Replace("<" + oldTagName + "/>", "<" + newTagName + "/>");
source = source.Replace("</" + oldTagName + ">", "</" + newTagName + ">");
}
public static String GetAttribute(ref String source, String attributeName)
{
return GetAttribute(ref source, attributeName, 0);
}
public static int GetAttributeStart(ref String source, String attributeName, int startIndex)
{
// Regex attributeInitial = new Regex(attributeName + "=(\"|\')");
Regex attributeInitial = new Regex(attributeName + "=");
Match attributeStartMatch = attributeInitial.Match(source, startIndex);
if (!attributeStartMatch.Success)
{
return -1;
}
return attributeStartMatch.Index + attributeStartMatch.Length;
}
public static int GetAttributeEnd(ref String source, String attributeName, int startIndex)
{
char sep = source[startIndex];
Match attributeEndMatch = null;
if (sep == '\'' || sep == '\"')
{
Regex attributeEnd = new Regex("(\"|\')");
attributeEndMatch = attributeEnd.Match(source, startIndex + 1);
if (!attributeEndMatch.Success)
{
return -1;
}
}
else
{
Regex attributeEnd = new Regex("(/|\\s|>)");
attributeEndMatch = attributeEnd.Match(source, startIndex);
if (!attributeEndMatch.Success)
{
return -1;
}
}
return attributeEndMatch.Index;
}
public static String GetAttribute(ref String source, String attributeName, int startIndex)
{
int startOfAttributeContent = GetAttributeStart(ref source, attributeName, startIndex);
if (startOfAttributeContent < 0)
{
return null;
}
int endOfAttributeContent = GetAttributeEnd(ref source, attributeName, startOfAttributeContent);
if (endOfAttributeContent < 0)
{
return null;
}
return source.Substring(startOfAttributeContent + 1, endOfAttributeContent - (startOfAttributeContent + 1));
}
public static void StripAttributeFromTags(ref String source, String[] tagNames, String attributeName)
{
foreach (String tagName in tagNames)
StripAttributeFromTag(ref source, tagName, attributeName);
}
public static void StripAttributeFromTag(ref String source, String tagName, String attributeName)
{
// find tag starts
int i = source.IndexOf("<" + tagName);
while (i >= 0)
{
// find the end of the tag
int iEnd = source.IndexOf(">", i);
// if there is no closer for this tag, then things are bad. bail.
if (iEnd < i)
{
Console.WriteLine("Incomplete tag detected in source '" + source + "'. Parts of this file could not be processed.");
return;
}
// now find the attribute within the tag.
int iAttributeStart = source.IndexOf(attributeName + "=", i);
if (iAttributeStart > i && iAttributeStart < iEnd)
{
// get the quote char in use... is it ' or " ?