123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Wayne.Lib
- {
-
-
-
-
- public static class Any
- {
- public static int Id = int.MinValue;
- public static string IdReplacement = "$";
- public static string String = "*";
- public static string Wildcard = "#";
- public static bool IsResolvedAddress(string address)
- {
- return !address.Contains(Any.String);
- }
- public static string Convert(int id)
- {
- if (id == Id)
- {
- return IdReplacement;
- }
- return id.ToString(System.Globalization.CultureInfo.InvariantCulture);
- }
-
-
-
-
-
-
- public static int[] GetIds(string pattern, string address)
- {
- ParamGuard.AssertIsNotNull(pattern, "pattern");
- ParamGuard.AssertIsNotNull(address, "address");
- string[] actorAddressSplit = address.Split('.');
- string[] actorAddressPatternSplit = pattern.Split('.');
- if (actorAddressSplit.Length != actorAddressPatternSplit.Length)
- {
- return new int[0];
- }
- List<int> result = new List<int>();
- for (int i = 0; i < actorAddressSplit.Length; i++)
- {
- string actualElement = actorAddressSplit[i];
- string patternElement = actorAddressPatternSplit[i];
- if (actualElement != patternElement)
- {
- if (patternElement == "$")
- {
- int intResult;
- if (TryParseInt(actualElement, out intResult))
- {
- result.Add(intResult);
- }
- }
- }
- }
- return result.ToArray();
- }
-
-
-
-
-
-
- private static bool TryParseInt(string possibleInt, out int i)
- {
- if (!string.IsNullOrEmpty(possibleInt))
- {
- try
- {
- if (possibleInt.All(char.IsNumber))
- {
- i = int.Parse(possibleInt);
- return true;
- }
- }
- catch (FormatException)
- { }
- }
- i = 0;
- return false;
- }
- public static string Convert(string val)
- {
- if (val == String)
- return String;
- return val;
- }
-
-
-
-
-
-
-
-
-
- public static bool Match(string address, string pattern)
- {
- ParamGuard.AssertIsNotNull(address, "address");
- ParamGuard.AssertIsNotNull(pattern, "pattern");
- if (pattern == Wildcard)
- {
- return true;
- }
- string[] actorAddressSplit = address.Split('.');
- string[] actorAddressPatternSplit = pattern.Split('.');
- if (actorAddressSplit.Length != actorAddressPatternSplit.Length)
- {
- return false;
- }
- for (int i = 0; i < actorAddressSplit.Length; i++)
- {
- string actualElement = actorAddressSplit[i];
- string patternElement = actorAddressPatternSplit[i];
- if (actualElement != patternElement)
- {
- switch (patternElement)
- {
- case "*":
- continue;
- case "$":
- if (!actualElement.All(Char.IsNumber))
- {
- return false;
- }
- continue;
- default:
- return false;
- }
- }
- }
- return true;
- }
- }
- }
|