public static string ToStringJoin(this IEnumerable<string> ienum, string separator = ", ") { if(ienum == null) { return ""; } if(separator == null) { separator = ""; } var ls = ListCache<string>.Take(); var size = 0; foreach(string item in ienum) { if (!string.IsNullOrWhiteSpace(item)) { var str = item.Trim(); size += str.Length + separator.Length; ls.Add(str); } } var sb = StringBuilderCache.Take(size); for(var i = 0; i < ls.Count; i++) { sb.Append(ls[i]); if (i < ls.Count - 1) { sb.Append(separator); } } ListCache<string>.Return(ref ls); return StringBuilderCache.Release(ref sb); }
public void Add(string key, T value) { ListCache <string> .Take(); List <string> words = ListCache <string> .Take(); GetNormalizedTerms(key, words); for (var i = 0; i < words.Count; i++) { string word = words[i]; CharNode current = RootNode; for (int idx = 0; idx < word.Length; idx++) { char ch = word[idx]; if (current.Children == null) { current.Children = new Dictionary <char, CharNode>(); } current = current.Children.TryGetValue(ch, out CharNode next) ? next : AddNodeChild(current, ch); } AddNodeItem(current, word, value); } ListCache <string> .Return(ref words); }
private static void GetNormalizedTerms(string input, List <string> lsTerms) { if (String.IsNullOrWhiteSpace(input)) { return; } string str = input.Normalize(NormalizationForm.FormD); StringBuilder sbWord = StringBuilderCache.Take(); List <string> words = ListCache <string> .Take(); char ch; for (var i = 0; i < str.Length; i++) { ch = str[i]; UnicodeCategory cat = CharUnicodeInfo.GetUnicodeCategory(ch); switch (cat) { case UnicodeCategory.TitlecaseLetter: case UnicodeCategory.UppercaseLetter: sbWord.Append(char.ToLower(ch)); break; case UnicodeCategory.LowercaseLetter: sbWord.Append(ch); break; case UnicodeCategory.DecimalDigitNumber: sbWord.Append(ch); break; case UnicodeCategory.DashPunctuation: case UnicodeCategory.ConnectorPunctuation: if (sbWord.Length > 0 && sbWord[sbWord.Length - 1] != '-') { sbWord.Append('-'); } break; case UnicodeCategory.SpaceSeparator: if (sbWord.Length > 0) { words.Add(sbWord.ToString()); sbWord.Clear(); } break; case UnicodeCategory.LineSeparator: case UnicodeCategory.ParagraphSeparator: case UnicodeCategory.OtherPunctuation: if (sbWord.Length > 0) { words.Add(sbWord.ToString()); WordsToTerms(); } break; } } if (sbWord.Length > 0) { words.Add(sbWord.ToString()); } WordsToTerms(); StringBuilderCache.Return(ref sbWord); ListCache <string> .Return(ref words); void WordsToTerms() { int n = lsTerms.Count; sbWord.Clear(); for (var i = 0; i < words.Count; i++) { if (i > 0) { sbWord.Append(" "); } sbWord.Append(words[i]); if (i > 0) { lsTerms.Add(sbWord.ToString()); } } sbWord.Clear(); lsTerms.Reverse(n, lsTerms.Count - n); for (var i = 0; i < words.Count; i++) { lsTerms.Add(words[i]); } words.Clear(); } }