/// <summary> /// Compares "wildcard" with "fullwildcard" and returns the movement of "wildcard", i.e. which intervals exists between the elements of "wildcard". /// </summary> /// <param name="wildcard"></param> /// <param name="fullwildcard"></param> /// <returns>The movements</returns> private IKeyMovement getWildcardMovement(Wildcard wildcard, Wildcard fullwildcard) { //check if linear: int a; int b; int i = 0; while (fullwildcard.getChars()[i] != wildcard.getChars()[0]) { i++; } b = i; i++; while (fullwildcard.getChars()[i] != wildcard.getChars()[1]) { i++; } a = i - b; bool linear = true; for (int c = 0; c < wildcard.getLength(); c++) { if (fullwildcard.getChars()[c * a + b] != wildcard.getChars()[c]) { linear = false; break; } } if (linear) { return(new LinearKeyMovement(a, b, wildcard.getLength())); } //not linear, so just list the keys: List <int> keyList = new List <int>(); for (int c = 0; c < wildcard.getLength(); c++) { for (int x = 0; x < fullwildcard.getLength(); x++) { if (fullwildcard.getChars()[x] == wildcard.getChars()[c]) { keyList.Add(x); } } } return(new ListKeyMovement(keyList)); }
/** * tests, if 'wildcardKey' matches 'pattern'. **/ public static bool testWildcardKey(string wildcardKey, string pattern) { try { int kcount = 0; int pcount = 0; while (kcount < wildcardKey.Length && pcount < pattern.Length) { if (pattern[pcount] != '[') { if (pattern[pcount] != wildcardKey[kcount]) { return(false); } kcount++; pcount++; } else { Wildcard patternWc = new Wildcard(pattern.Substring(pcount, pattern.IndexOf(']', pcount) + 1 - pcount)); while (pattern[pcount++] != ']') { ; } Wildcard wildcardKeyWc = null; if (wildcardKey[kcount] == '[') { wildcardKeyWc = new Wildcard(wildcardKey.Substring(kcount, wildcardKey.IndexOf(']', kcount) + 1 - kcount), patternWc); if (wildcardKeyWc.getLength() == 0) { throw new Exception("Invalid wildcard pattern: wildcard must contain at least one character!"); } while (wildcardKey[++kcount] != ']') { ; } } else if (wildcardKey[kcount] != '*') { wildcardKeyWc = new Wildcard("" + wildcardKey[kcount]); } if (!patternWc.contains(wildcardKeyWc) && !(wildcardKey[kcount] == '*')) { return(false); } kcount++; } } if (pcount != pattern.Length || kcount != wildcardKey.Length) { return(false); } return(true); } catch (Exception) { return(false); } }