/// <summary> /// 获取字符类中包含的所有字符。 /// </summary> /// <param name="charClass">要获取所有字符的字符类。</param> /// <returns>字符类中包含的所有字符。</returns> private static CharSet GetCharClassSet(string charClass) { CharSet set = new CharSet(); if (RegexCharClass.IsSubtraction(charClass) || RegexCharClass.ContainsCategory(charClass)) { for (int i = 0; i <= char.MaxValue; i++) { if (RegexCharClass.CharInClass((char)i, charClass)) { set.Add((char)i); } } } else { // 如果不包含差集和 Unicode 字符分类的话,可以更快。 string ranges = RegexCharClass.GetCharClassRanges(charClass); if (RegexCharClass.IsNegated(charClass)) { int s = 0; for (int i = 0; i < ranges.Length; i++) { for (int j = s; j < ranges[i]; j++) { set.Add((char)j); } i++; s = i < ranges.Length ? ranges[i] : char.MaxValue + 1; } for (int j = s; j <= char.MaxValue; j++) { set.Add((char)j); } } else { for (int i = 0; i < ranges.Length; i++) { int j = ranges[i++]; int end = i < ranges.Length ? ranges[i] : char.MaxValue + 1; for (; j < end; j++) { set.Add((char)j); } } } } return(set); }