(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / StringType.cs
blob746ff644f9b5a2990aba9152650948c812183bf3
1 //
2 // StringType.cs
3 //
4 // Author:
5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Francesco Delfino (pluto@tipic.com)
7 // Dennis Hayes (dennish@raytek.com)
8 //
9 // (C) 2002 Chris J Breisch
10 // 2002 Tipic, Inc. (http://www.tipic.com)
13 * Copyright (c) 2002-2003 Mainsoft Corporation.
14 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 * DEALINGS IN THE SOFTWARE.
35 using System;
36 using System.Globalization;
37 using System.Text;
38 using System.ComponentModel;
40 namespace Microsoft.VisualBasic.CompilerServices {
41 [StandardModule, EditorBrowsableAttribute(EditorBrowsableState.Never)]
42 sealed public class StringType {
43 private StringType () {}
45 /**
46 * This method is called when a seconf asterisk appears in the pattern.
47 * @param pattern the relevant part of the original user pattern
48 * @param source the relevanr part of the original user source string
49 * @param count number of characters that where skipped due to the first
50 * asterisk.
51 * @param compareOption the comparision method Binary or Text
52 * @return int number of characters that should be skipped due to all the
53 * asterisk that where found.
55 private static int multipleAsteriskSkip(
56 string pattern,
57 string source,
58 int count,
59 CompareMethod compareOption) {
60 string subString;
61 bool isLike;
62 int length = (source == null) ? 0 : source.Length;
63 while (count < length) {
64 subString = source.Substring(length - count);
65 try {
66 isLike = StrLike(subString, pattern, compareOption);
68 catch /*(Exception exp)*/ {
69 isLike = false;
71 if (!isLike)
72 count = count + 1;
74 return count;
77 /**
78 * This method determines the number of characters that can be skipped due to
79 * the asterisk.
80 * @param pattern the relevant part of the original user pattern
81 * @param source the relevanr part of the original user source string
82 * @param compareOption the comparision method Binary or Text
83 * @return int number of characters that should be skipped due to all the
84 * asterisk that where found.
87 private static int asteriskSkip(
88 string pattern,
89 string source,
90 CompareMethod compareOption) {
91 int sourceLength = source.Length;
92 int numberOfSkipedChars = 0;
93 int patternLen;
94 int patternIndex = 0;
95 bool exactMatch = false;
96 string sub;
97 char currentPatternChar;
99 //java patternLen = Strings.Len(pattern); /elsewhere patteren.toString is used, why not here?
100 patternLen = pattern.Length;
101 while (patternIndex < patternLen) {
103 currentPatternChar = pattern[patternIndex];
104 if (currentPatternChar == '*') {
105 //if in the original pattern the where two asterisks together the
106 //second is ignored
107 if (numberOfSkipedChars > 0) {
108 if (exactMatch) {
109 numberOfSkipedChars =
110 multipleAsteriskSkip(
111 pattern,
112 source,
113 numberOfSkipedChars,
114 compareOption);
115 return sourceLength - numberOfSkipedChars;
117 sub = pattern.Substring(0, patternIndex);
118 numberOfSkipedChars =
119 Strings.InStrRev(
120 source,
121 sub,
122 source.Length,
123 compareOption);
124 return numberOfSkipedChars;
127 else if (currentPatternChar == '[') {
128 sub = pattern.Substring(patternIndex);
129 int skipCharInPattern = sub.IndexOf(']');
130 if (skipCharInPattern < 0)
131 break;
132 numberOfSkipedChars ++;
133 patternIndex += skipCharInPattern;
135 else if (currentPatternChar == ']' || currentPatternChar == '?' ||
136 currentPatternChar == '#' || currentPatternChar == '!' ||
137 currentPatternChar == '-') {
138 numberOfSkipedChars++;
139 patternIndex ++;
140 exactMatch = true;
142 else {
143 numberOfSkipedChars++;
144 patternIndex++;
147 //this value is returned if there are not more '*' in the pattern
148 return sourceLength - numberOfSkipedChars;
151 public static string FromBoolean (bool Value) {
152 return Convert.ToString(Value);
155 public static string FromByte(byte Value) {
156 return Convert.ToString(Value);
159 public static string FromChar(char Value) {
160 return Convert.ToString(Value);
163 private static string FromCharAndCount(char Value, int count) {
164 //return StringStaticWrapper.Ctor(Value,count);
165 return Value.ToString().Substring(0,count);
168 private static string FromCharArray(char[] Value) {
169 return new string(Value);
172 private static string FromCharArraySubset(
173 char[] Value,
174 int startIndex,
175 int length) {
176 return new string(Value, startIndex, length);
179 public static string FromDate(DateTime Value) {
180 //TODO: which is right, Mono or Mainsoft
181 //implmented in mono by
182 //return Convert.ToString(Value);
184 //implmented in java
185 long lTime;
186 TimeSpan ts;
188 ts = Value.TimeOfDay;
189 lTime = ts.Ticks;
190 //if only the part of the hours ,minute ... is relevant (not day, month,
191 // year) the format is "T"
192 if (lTime == Value.Ticks ||
193 (Value.Year == 1899
194 && Value.Month == 12
195 && Value.Day == 30))
196 return Value.ToString("T", null);
197 // only the part of day,month and year is relevant , the format is "d".
198 if (lTime == 0)
199 return Value.ToString("d", null);
200 return Value.ToString("G", null);
203 public static string FromDecimal(Decimal Value) {
204 return FromDecimal(Value, null);
207 public static string FromDecimal(Decimal Value, NumberFormatInfo NumberFormat) {
208 return Convert.ToString(Value, NumberFormat);
209 //java code return Value.ToString("G", numberFormat);
212 public static string FromDouble(double Value) {
213 return Convert.ToString(Value);
214 //java return FromDouble(Value, null);
217 public static string FromDouble(double Value, NumberFormatInfo NumberFormat) {
218 return Convert.ToString(Value,NumberFormat);
219 //return new ClrDouble(Value).ToString("G");
222 public static string FromInteger(int Value) {
223 return Value.ToString();
226 public static string FromLong(long Value) {
227 return Value.ToString();
228 //return Convert.ToString(Value);
231 public static string FromShort(short Value) {
232 return Value.ToString();
235 public static string FromSingle(float Value) {
236 return Convert.ToString(Value);
237 //return FromSingle(Value, null);
240 public static string FromSingle(float Value, NumberFormatInfo NumberFormat) {
241 return Convert.ToString(Value,NumberFormat);
242 //return new ClrSingle(Value).ToString(NumberFormat);
245 public static string FromObject(object Value) {
246 if (Value == null)
247 return null;
249 if (Value is string)
250 return (string) Value;
252 if (
253 (Value is char[])
254 && ((Array)Value).Rank == 1)
255 return new string(CharArrayType.FromObject(Value));
257 return Convert.ToString(Value);
263 * This method replace in the reference strDesRef parameter the characters
264 * from position startPosition. the number of characters that are been replaced
265 * is the minimum between maxInsertLength and the length of sInsert.
266 * @param strDesRef the destination string reference.
267 * @param startPosition the index from which the change should be done.
268 * @param maxInsertLength the maximum number of characters that should be change.
269 * @param sInsert the string from which the character should be taken
272 public static void MidStmtStr(ref string strDesRef, int startPosition, int maxInsertLength, string sInsert)
274 int destLen = 0;
275 int insertLen = 0;
276 string dest = strDesRef;
278 if (dest != null)
279 destLen = dest.Length;
280 if (sInsert != null)
281 insertLen = sInsert.Length;
282 else
283 return;
285 //change to java location in array.
286 startPosition = startPosition - 1;
287 if (startPosition < 0 || startPosition >= destLen) {
288 throw new ArgumentException("Invalid Argument Value", "Start");
289 //throw new IllegalArgumentException(//java
290 // Utils.GetResourceString("Argument_InvalidValue1", "Start"));
292 if (maxInsertLength < 0) {
293 throw new ArgumentException("Invalid Argument Value", "Length");
294 //throw new IllegalArgumentException(//java
295 // Utils.GetResourceString("Argument_InvalidValue1", "Length"));
297 if (insertLen > maxInsertLength)
298 insertLen = maxInsertLength;
299 if (insertLen > destLen - startPosition)
300 insertLen = destLen - startPosition;
301 if (insertLen == 0)
302 return;
303 //TODO: are the next two lines equvlent to the 8 that follow?
304 dest.Remove(startPosition,insertLen);
305 dest.Insert(startPosition,sInsert.Substring(0,insertLen));
306 //Java version
307 //sb = new StringBuilder(dest);
308 //if (sInsert.Length == insertLen)
309 // sb.Replace(startPosition ,startPosition + insertLen, sInsert);
310 //else
311 // sb.Replace(
312 // startPosition,
313 // startPosition + insertLen,
314 // sInsert.Substring(0, insertLen));
317 //strDesRef.setValue(sb.ToString());//java
318 strDesRef = dest;//sb.ToString();
321 public static int StrCmp(string sLeft, string sRight, bool TextCompare) {
322 if (sLeft == null)
323 sLeft = "";
324 if (sRight == null)
325 sRight = "";
327 if (TextCompare)
328 return string.Compare(sLeft, sRight, TextCompare);
329 //return StringStaticWrapper.Compare(sLeft, sRight, TextCompare);
330 return sLeft.CompareTo(sRight);
331 // return StringStaticWrapper.CompareOrdinal(sLeft, sRight);
334 internal static string ToHalfwidthNumbers(string s) {
335 return s;
338 private static bool compareBinary(
339 bool seenNot,
340 bool match,
341 char patternChar,
342 char sourceChar) {
343 // if (seenNot ^ notMatch ) == true then a previous pattern character
344 // matched the current source character . the current comparision is not
345 // required
346 if (seenNot ^ match )
347 return match;
348 else if (seenNot && match)
349 return patternChar != sourceChar;
350 else
351 return patternChar == sourceChar;
354 private static bool compare(
355 bool seenNot,
356 bool match,
357 char patternChar,
358 char sourceChar) {
359 // if (seenNot ^ notMatch ) == true then a previous pattern character
360 // matched the current source character . the current comparision is not
361 // required
362 //if (seenNot ^ match )
363 if (seenNot || match )
364 return match;
365 else if (seenNot && match)
366 return string.Compare(FromChar(patternChar), FromChar(sourceChar)) != 0;
367 //return StringStaticWrapper.Compare(FromChar(patternChar), FromChar(sourceChar)) != 0;
368 //else
369 return string.Compare(FromChar(patternChar), FromChar(sourceChar)) == 0;
370 // return StringStaticWrapper.Compare(FromChar(patternChar), FromChar(sourceChar)) != 0;
371 // return StringStaticWrapper.Compare(FromChar(patternChar), FromChar(sourceChar)) != 0;
372 // else
373 // return StringStaticWrapper.Compare(FromChar(patternChar), FromChar(sourceChar)) == 0;
377 * check if a specified string is an hex or oct representation of
378 * an integer number
379 * @param Value The string Value
380 * @param res a long array (minimum size 1).
381 * @return true if <code>Value<\code> can be parse into integer Value.
382 * the result of parsing is located in the <code>res[0]<\code>.
384 internal static bool IsHexOrOctValue(string Value, long[] res) {
385 try {
386 // if the string starts with '&h' or '&H' it represents an Hex number
387 if (Value.StartsWith("&H") || Value.StartsWith("&h"))
388 res[0] = Convert.ToInt64(Value.Substring(2), 16);
389 // if the string starts with '&o' or '&O' it represents an Oct number
390 else if (Value.StartsWith("&O") || Value.StartsWith("&o"))
391 res[0] = Convert.ToInt64(Value.Substring(2), 8);
392 else
393 return false;
395 catch /*(Java catches NumberFormatException, presumable passing all other excptions back up to the caller
396 * //TODO: we should narrow the ececptions that we catch here)*/
398 //catch (NumberFormatException e) {
399 return false;
401 return true;
405 * This method matches a pattern between brackets and the relevant character.
406 * The pattern matching is for binary comparision
407 * @param pattern the part of the pattern that is between the brackets without
408 * the brackets
409 * @param sourceChar the relevant character in the source string
410 * @return bool true if the character matches the pattern and false otherwise.
412 private static bool inBracketBinary(string pattern, char sourceChar) {
413 char currentPatternChar = (char)0;
414 char currentCharInRange = (char)0;
415 char previousCharInRange = (char)0;
416 int patternIndex = 0;
417 bool isMatch = false;
418 bool isNotSignAppears = false;
419 bool specialChar = false;
420 bool isRangeSignAppears = false;
421 int patternLength = (pattern == null)? 0 : pattern.Length;
422 while (patternIndex < patternLength) {
423 currentPatternChar = pattern[patternIndex];
424 if (currentPatternChar == '*') {
425 isMatch =
426 compareBinary(
427 isNotSignAppears,
428 isMatch,
429 currentPatternChar,
430 sourceChar);
431 specialChar = true;
433 else if (currentPatternChar == '?') {
434 // the pattern is '[previousCharInRange-?
435 if (isRangeSignAppears) {
436 isRangeSignAppears = false;
437 currentCharInRange = currentPatternChar;
438 if (previousCharInRange > currentCharInRange)
439 throw new Exception("Bad patteren string");
440 //throw (Exception) ExceptionUtils.VbMakeException(
441 // vbErrors.BadPatStr);
442 //when the previous char in the range matches the char in
443 // the source
444 if (!(isNotSignAppears || isMatch)){
445 if (sourceChar == previousCharInRange)
446 isMatch = true;
447 if (isNotSignAppears)
448 isMatch = !isMatch;
451 //the first place in the range '[?'
452 else {
453 previousCharInRange = currentPatternChar;
454 specialChar = true;
455 isMatch =
456 compareBinary(
457 isNotSignAppears,
458 isMatch,
459 currentPatternChar,
460 sourceChar);
463 else if (currentPatternChar == '#') {
464 //the pattern is like '[previousCharInRange-#'
465 if (isRangeSignAppears) {
466 isRangeSignAppears = false;
467 currentCharInRange = currentPatternChar;
468 if (previousCharInRange > currentCharInRange)
469 throw new Exception("Bad patteren string");
470 //throw (
471 // Exception) ExceptionUtils.VbMakeException(
472 // vbErrors.BadPatStr);
473 //when the previous char in the range matches the char in
474 // the source
475 if (!(isNotSignAppears || isMatch)){
476 if (sourceChar == previousCharInRange)
477 isMatch = true;
478 if (isNotSignAppears)
479 isMatch = !isMatch;
482 //the first place in range.
483 else {
484 previousCharInRange = currentPatternChar;
485 specialChar = true;
486 isMatch =
487 compareBinary(
488 isNotSignAppears,
489 isMatch,
490 currentPatternChar,
491 sourceChar);
494 else if (currentPatternChar == '-') {
495 //this pattern is not valid example is '[9--'
496 if (isRangeSignAppears && specialChar)
497 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Pattern"));
498 if (!(specialChar) && !(isRangeSignAppears)) {
499 isMatch =
500 compareBinary(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
502 isRangeSignAppears = true;
504 else if (currentPatternChar == '!') {
505 //this is the first symbol is the range
506 if (!(isNotSignAppears)) {
507 isNotSignAppears = true;
508 isMatch = true;
510 //this is the second '!' in the range '[!! ' or appears
511 //as a symbol in the pattern
512 else {
513 specialChar = true;
514 isMatch =
515 compareBinary(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
518 else if (currentPatternChar == '[') {
519 if (isRangeSignAppears) {
520 isRangeSignAppears = false;
521 currentCharInRange = currentPatternChar;
522 if (previousCharInRange > currentCharInRange)
523 throw new Exception("Bad patteren string");
524 //throw (
525 // Exception) ExceptionUtils.VbMakeException(
526 // vbErrors.BadPatStr);
527 //when the previous char in the range matches the char in
528 // the source
529 if (!(isNotSignAppears ^ isMatch)){
530 if (sourceChar == previousCharInRange)
531 isMatch = true;
532 if (isNotSignAppears)
533 isMatch = !isMatch;
536 //the first sign in the range of chars
537 else {
538 previousCharInRange = currentPatternChar;
539 specialChar = true;
540 isMatch =
541 compareBinary(
542 isNotSignAppears,
543 isMatch,
544 currentPatternChar,
545 sourceChar);
548 else if (currentPatternChar == ']') {
549 isMatch =
550 compareBinary(
551 isNotSignAppears,
552 isMatch,
553 currentPatternChar,
554 sourceChar);
555 if (!(isMatch))
556 break;
558 //this pattern char appears in range of chars.
559 else {
560 specialChar = true;
561 if (isRangeSignAppears) {
562 isRangeSignAppears = false;
563 currentCharInRange = currentPatternChar;
564 if (previousCharInRange > currentCharInRange)
565 throw new Exception("Bad patteren string");
566 //throw (Exception)ExceptionUtils.VbMakeException(vbErrors.BadPatStr);
567 if (!(isNotSignAppears || isMatch)){
568 if (sourceChar <= previousCharInRange || sourceChar > currentCharInRange)
569 isMatch = true;
570 else
571 isMatch = false;
572 if (isNotSignAppears)
573 isMatch = !isMatch;
576 else {
577 previousCharInRange = currentPatternChar;
578 isMatch =
579 compareBinary(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
582 patternIndex++;
585 if (isNotSignAppears ^ isMatch) return true;
586 if (isRangeSignAppears && !isMatch)
587 return false;
588 else if (isNotSignAppears) {
589 if ('!' != sourceChar)
590 return false;
592 return true;
596 * this method checks if the soutce string matches the pattern according to
597 * binary comparision.
598 * @param source the source string
599 * @param pattern the pattern string
600 * @return bool True if the source match the pattern and false otherwise.
603 public static bool StrLikeBinary(string source, string pattern) {
604 bool startRangeSignAppears = false;
605 bool isMatch = false;
606 char currentPatternChar = (char)0;
607 int patternLength = (char)0;
608 int patternIndex = (char)0;
609 char currentSourceChar = (char)0;
610 //bool isRangeSignAppears = false;
611 //bool specialChar = false;
612 bool isNotSignAppears = false;
613 int numberOfSkipedChars = 0;
614 int sourceLength = 0;
615 int sourceIndex = 0;
618 patternLength = (pattern == null)? 0 : pattern.Length;
619 sourceLength = (source == null)? 0 : source.Length ;
621 if (sourceIndex < sourceLength)
622 currentSourceChar = source[sourceIndex];
623 while (patternIndex < patternLength) {
624 currentPatternChar = pattern[patternIndex];
625 if (currentPatternChar == '*') {
626 numberOfSkipedChars =
627 asteriskSkip(
628 pattern.Substring(patternIndex + 1),
629 source.Substring(sourceIndex),
630 CompareMethod.Binary);
631 if (numberOfSkipedChars < 0) {
632 break;
634 if (numberOfSkipedChars > 0) {
635 sourceIndex += numberOfSkipedChars;
636 if (sourceIndex < sourceLength)
637 currentSourceChar = source[sourceIndex];
640 else if (currentPatternChar == '?') {
641 sourceIndex++;
642 if (sourceIndex < sourceLength)
643 currentSourceChar = source[sourceIndex];
645 else if (currentPatternChar == '#') {
646 if (!(char.IsDigit(currentSourceChar)))
647 break;
648 sourceIndex++;
649 if (sourceIndex < sourceLength)
650 currentSourceChar = source[sourceIndex];
652 else if (currentPatternChar == '-') {
653 isMatch =
654 compareBinary(
655 isNotSignAppears,
656 isMatch,
657 currentPatternChar,
658 currentSourceChar);
659 sourceIndex++;
660 if (sourceIndex < sourceLength)
661 currentSourceChar = source[sourceIndex];
662 //isRangeSignAppears = true;
664 else if (currentPatternChar == '!') {
665 //specialChar = true;
666 isMatch =
667 compareBinary(
668 isNotSignAppears,
669 isMatch,
670 currentPatternChar,
671 currentSourceChar);
672 sourceIndex++;
673 if (sourceIndex < sourceLength)
674 currentSourceChar = source[sourceIndex];
676 else if (currentPatternChar == '[') {
677 string sub = pattern.Substring(patternIndex);
678 int indexOfEndBracket = sub.IndexOf(']');
679 startRangeSignAppears = true;
680 if (indexOfEndBracket == -1)
681 break;
682 sub = sub.Substring(1, indexOfEndBracket);
683 startRangeSignAppears = false;
684 bool isOk = inBracketBinary(sub, currentSourceChar);
685 if (!isOk)
686 break;
687 sourceIndex++;
688 if (sourceIndex < sourceLength)
689 currentSourceChar = source[sourceIndex];
690 patternIndex += (sub.Length + 2);
691 continue;
693 else if (currentPatternChar == ']') {
694 isMatch =
695 compareBinary(
696 isNotSignAppears,
697 isMatch,
698 currentPatternChar,
699 currentSourceChar);
700 if (!(isMatch))
701 break;
702 sourceIndex++;
703 if (sourceIndex < sourceLength)
704 currentSourceChar = source[sourceIndex];
705 isMatch = false;
706 //specialChar = false;
707 isNotSignAppears = false;
708 //isRangeSignAppears = false;
710 else if (currentPatternChar == currentSourceChar || isNotSignAppears) {
711 //specialChar = true;
712 isNotSignAppears = false;
713 sourceIndex++;
714 if (sourceIndex < sourceLength)
715 currentSourceChar = source[sourceIndex];
716 else if (sourceIndex > sourceLength)
717 return false;
719 else
720 break;
722 patternIndex++;
724 if (startRangeSignAppears) {
725 if (sourceLength == 0)
726 return false;
727 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Pattern"));
729 if (patternIndex != patternLength || sourceIndex != sourceLength)
730 return false;
731 return true;
734 public static bool StrLike(
735 string source,
736 string pattern,
737 CompareMethod compareOption) {
738 if(compareOption == CompareMethod.Text)
739 return StrLikeBinary(source, pattern);
740 else
741 return StrLikeText(source, pattern);
744 // /**
745 // * this method checks if the soutce string matches the pattern according to
746 // * the comparision method.
747 // * @param source the source string
748 // * @param pattern the pattern string
749 // * @param compareOption this param determines if the comparision is binary
750 // * or text
751 // * @return bool True if the source match the pattern and false otherwise.
752 // */
754 // public static bool StrLike(
755 // string source,
756 // string pattern,
757 // int compareOption) {
758 // if (compareOption == 0)
761 // }
765 * this method checks if the soutce string matches the pattern according to
766 * binary comparision.
767 * @param source the source string
768 * @param pattern the pattern string
769 * @return bool True if the source match the pattern and false otherwise.
772 public static bool StrLikeText(string source, string pattern) {
773 //char currentCharInRange = (char)0;
774 bool startRangeSignAppears = false;
775 bool isMatch = false;
776 char currentPatternChar = (char)0;
777 int patternLength = 0;
778 int patternIndex = 0;
779 char currentSourceChar = (char)0;
780 //bool isRangeSignAppears = false;
781 //bool specialChar = false;
782 bool isNotSignAppears = false;
783 int numberOfSkipedChars = 0;
784 int sourceLength = 0;
785 int sourceIndex = 0;
786 //char previousCharInRange = (char)0;
788 patternLength = (pattern == null)? 0 : pattern.Length;
789 sourceLength = (source == null)? 0 : source.Length ;
791 if (sourceIndex < sourceLength)
792 currentSourceChar = source[sourceIndex];
794 while (patternIndex < patternLength) {
795 currentPatternChar = pattern[patternIndex];
796 if (currentPatternChar == '*') {
797 numberOfSkipedChars =
798 asteriskSkip(
799 pattern.Substring(patternIndex + 1),
800 source.Substring(sourceIndex),
801 CompareMethod.Text);
802 if (numberOfSkipedChars < 0)
803 return false;
804 if (numberOfSkipedChars > 0) {
805 sourceIndex += numberOfSkipedChars;
806 if (sourceIndex < sourceLength)
807 currentSourceChar = source[sourceIndex];
810 else if (currentPatternChar == '?') {
811 sourceIndex++;
812 if (sourceIndex < sourceLength)
813 currentSourceChar = source[sourceIndex];
815 else if (currentPatternChar == '#') {
816 if (!(char.IsDigit(currentSourceChar)))
817 break;
818 sourceIndex++;
819 if (sourceIndex < sourceLength)
820 currentSourceChar = source[sourceIndex];
822 else if (currentPatternChar == '-') {
823 isMatch =
824 compare(
825 isNotSignAppears,
826 isMatch,
827 currentPatternChar,
828 currentSourceChar);
829 sourceIndex++;
830 if (sourceIndex < sourceLength)
831 currentSourceChar = source[sourceIndex];
832 //isRangeSignAppears = true;
834 else if (currentPatternChar == '!') {
835 //specialChar = true;
836 isMatch =
837 compare(
838 isNotSignAppears,
839 isMatch,
840 currentPatternChar,
841 currentSourceChar);
842 sourceIndex++;
843 if (sourceIndex < sourceLength)
844 currentSourceChar = source[sourceIndex];
846 else if (currentPatternChar == '[') {
847 string sub = pattern.Substring(patternIndex);
848 startRangeSignAppears = true;
849 int indexOfEndBracket = sub.IndexOf(']');
850 if (indexOfEndBracket == -1)
851 break;
852 sub = sub.Substring(1, indexOfEndBracket);
853 startRangeSignAppears = false;
854 bool isOk = inBracketBinary(sub, currentSourceChar);
855 if (!isOk)
856 break;
857 sourceIndex++;
858 if (sourceIndex < sourceLength)
859 currentSourceChar = source[sourceIndex];
860 patternIndex += (sub.Length + 2);
861 continue;
863 else if (currentPatternChar == ']') {
864 isMatch =
865 compare(
866 isNotSignAppears,
867 isMatch,
868 currentPatternChar,
869 currentSourceChar);
870 //specialChar = true;
871 if (!(isMatch))
872 break;
873 sourceIndex++;
874 if (sourceIndex < sourceLength)
875 currentSourceChar = source[sourceIndex];
876 isMatch = false;
877 //specialChar = false;
878 isNotSignAppears = false;
879 //isRangeSignAppears = false;
881 else {
882 //specialChar = true;
883 if (!(currentPatternChar == currentSourceChar || isNotSignAppears))
884 break;
885 isNotSignAppears = false;
886 sourceIndex++;
887 if (sourceIndex < sourceLength)
888 currentSourceChar = source[sourceIndex];
889 else if (sourceIndex > sourceLength)
890 return false;
892 patternIndex++;
894 if (startRangeSignAppears) {
895 if (sourceLength == 0)
896 return false;
897 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Pattern"));
899 if (patternIndex != patternLength || sourceIndex != sourceLength)
900 return false;
901 return true;
905 * This method matches a pattern between brackets and the relevant character.
906 * The pattern matching is for text comparision
907 * @param pattern the part of the pattern that is between the brackets without
908 * the brackets
909 * @param sourceChar the relevant character in the source string
910 * @return bool true if the character matches the pattern and false otherwise.
913 private static bool inBracketText(string pattern, char sourceChar) {
914 char currentPatternChar = (char)0;
915 char currentCharInRange = (char)0;
916 char previousCharInRange = (char)0;
917 int patternIndex = 0;
918 bool isMatch = false;
919 bool isNotSignAppears = false;
920 bool specialChar = false;
921 bool isRangeSignAppears = false;
922 int patternLength = (pattern == null)? 0 : pattern.Length;
923 while (patternIndex < patternLength) {
924 currentPatternChar = pattern[patternIndex];
925 if (currentPatternChar == '*') {
926 isMatch =
927 compare(
928 isNotSignAppears,
929 isMatch,
930 currentPatternChar,
931 sourceChar);
932 specialChar = true;
934 else if (currentPatternChar == '?') {
935 if (isRangeSignAppears) {
936 isRangeSignAppears = false;
937 currentCharInRange = currentPatternChar;
938 if (previousCharInRange > currentCharInRange)
939 throw new Exception("Bad patteren string");
940 //throw (
941 // Exception) ExceptionUtils.VbMakeException(
942 // vbErrors.BadPatStr);
943 //when the previous char in the range matches the char in
944 // the source
945 if (!(isNotSignAppears ^ isMatch)){
946 if (!(string.Compare(
947 FromChar(previousCharInRange),
948 FromChar(sourceChar),
949 true)
950 >= 0
951 || string.Compare(
952 FromChar(currentCharInRange),
953 FromChar(sourceChar),
954 true)
955 < 0))
956 isMatch = true;
957 if (isNotSignAppears)
958 isMatch = (isMatch == false);
961 // the first place in the range '[?'
962 else {
963 previousCharInRange = currentPatternChar;
964 specialChar = true;
965 isMatch =
966 compare(
967 isNotSignAppears,
968 isMatch,
969 currentPatternChar,
970 sourceChar);
973 else if (currentPatternChar == '#') {
974 // the pattern is '[previousCharInRange-#
975 if (isRangeSignAppears) {
976 isRangeSignAppears = false;
977 currentCharInRange = currentPatternChar;
978 if (previousCharInRange > currentCharInRange)
979 throw new Exception("Bad pattern string");
980 //throw (Exception)ExceptionUtils.VbMakeException(vbErrors.BadPatStr);
981 if (!(isNotSignAppears || isMatch)){
982 if (!(string.Compare(
983 FromChar(previousCharInRange),
984 FromChar(sourceChar),
985 true)
986 >= 0
987 || string.Compare(
988 FromChar(currentCharInRange),
989 FromChar(sourceChar),
990 true)
991 < 0))
992 isMatch = true;
993 if (isNotSignAppears)
994 isMatch = isMatch == false;
997 // the first place in the range '[#'
998 else {
999 previousCharInRange = currentPatternChar;
1000 specialChar = true;
1001 isMatch =
1002 compare(
1003 isNotSignAppears,
1004 isMatch,
1005 currentPatternChar,
1006 sourceChar);
1009 else if (currentPatternChar == '-') {
1010 //this pattern is not valid example is '[9--'
1011 if (isRangeSignAppears && specialChar)
1012 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Pattern"));
1013 if (!(specialChar) && !(isRangeSignAppears)) {
1014 isMatch =
1015 compare(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
1017 isRangeSignAppears = true;
1019 else if (currentPatternChar == '!') {
1020 //this is the first symbol is the range
1021 if (!(isNotSignAppears)) {
1022 isNotSignAppears = true;
1023 isMatch = true;
1025 //this is the second '!' in the range '[!! ' or appears
1026 //as a symbol in the pattern
1027 else {
1028 specialChar = true;
1029 isMatch =
1030 compare(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
1033 else if (currentPatternChar == '[') {
1034 if (isRangeSignAppears) {
1035 isRangeSignAppears = false;
1036 currentCharInRange = currentPatternChar;
1037 if (previousCharInRange > currentCharInRange)
1038 throw new Exception("Bad patteren string");
1039 //throw (
1040 // Exception) ExceptionUtils.VbMakeException(
1041 // vbErrors.BadPatStr);
1042 //when the previous char in the range matches the char in
1043 // the source
1044 if (!(isNotSignAppears || isMatch)){
1045 if (!(string.Compare(
1046 FromChar(previousCharInRange),
1047 FromChar(sourceChar),
1048 true)
1049 >= 0
1050 || string.Compare(
1051 FromChar(currentCharInRange),
1052 FromChar(sourceChar),
1053 true)
1054 < 0))
1055 isMatch = true;
1056 if (isNotSignAppears)
1057 isMatch = (isMatch == false);
1060 // the first sign in the range of chars
1061 else {
1062 previousCharInRange = currentPatternChar;
1063 specialChar = true;
1064 isMatch = compare(isNotSignAppears,isMatch,
1065 currentPatternChar,sourceChar);
1068 else if (currentPatternChar == ']') {
1069 isMatch =
1070 compare(
1071 isNotSignAppears,
1072 isMatch,
1073 currentPatternChar,
1074 sourceChar);
1075 if (!(isMatch))
1076 break;
1078 //this pattern char appears in range of chars.
1079 else {
1080 specialChar = true;
1081 if (isRangeSignAppears) {
1082 isRangeSignAppears = false;
1083 currentCharInRange = currentPatternChar;
1084 if (previousCharInRange > currentCharInRange)
1085 throw new Exception("Bad patteren string");
1086 //throw (Exception)ExceptionUtils.VbMakeException(vbErrors.BadPatStr);
1087 if (!(isNotSignAppears ^ isMatch)){
1088 if (sourceChar <= previousCharInRange || sourceChar > currentCharInRange)
1089 isMatch = true;
1090 else
1091 isMatch = false;
1092 if (isNotSignAppears)
1093 isMatch = !isMatch;
1096 else {
1097 previousCharInRange = currentPatternChar;
1098 isMatch =
1099 compare(isNotSignAppears, isMatch, currentPatternChar, sourceChar);
1102 patternIndex++;
1105 if (isNotSignAppears || isMatch) return true;
1106 if (isRangeSignAppears && !isMatch)
1107 return false;
1108 else if (isNotSignAppears) {
1109 if ('!' != sourceChar)
1110 return false;
1112 return true;