using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace FlexFramework.Excel { /// /// Base class of all custom converters. /// /// The data type to convert a string input to public abstract class CustomConverter : IConverter { Type IConverter.Type { get { return typeof(T); } } /// /// Convert input string to /// /// String input /// Output value public abstract T Convert(string input); /// /// Converts the string value to . A return value indicates whether the conversion succeeded. /// /// String input /// Output value /// true if s was converted successfully; otherwise, false. public bool TryConvert(string input, out T value) { value = default(T); try { value = Convert(input); } catch (Exception) { return false; } return true; } /// /// Split array with specified separators /// /// String value to split /// Delimiters /// A string array protected string[] Split(string input, params char[] separators) { string pattern = string.Empty; for (int i = 0; i < separators.Length; i++) { pattern = i > 0 ? "|" : string.Empty + pattern + @"(? /// Split grouped array like [a,b][c,d]... /// /// String value to split /// Left anchor /// Right anchor /// Split value /// Group expression invalid protected IEnumerable SplitGroup(string input, char start, char end) { string pattern = @"\G\[(\\\[|\\\]|[^\[\]])+\]".Replace('[', start).Replace(']', end); foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline)) { string val = match.Value; if (!match.Success) throw new ArgumentException("Group expression invalid", input); //val = Regex.Unescape(val); val = val.TrimStart(start).TrimEnd(end); val = val.Replace("\\" + start, start.ToString()).Replace("\\" + end, end.ToString()); if (Regex.IsMatch(val, @"(?<=\([^]]*),(?=[^]]*\))")) val = Regex.Replace(val, @"(?<=\([^]]*),(?=[^]]*\))", @"\,"); yield return val; } } object IConverter.Convert(string input) { return this.Convert(input); } } }