private static string GetGetValueErrorMessage <TKey, TValue>(IEnumerable <KeyValuePair <TKey, TValue> > dictionary, TKey key) { const int lengthCap = 500; var keysCharList = Alg.Intersperse(",", dictionary.Select(x => x.Key.ToString())).SelectMany(x => x.ToCharArray()); var errorMessage = "Failed to find key " + key.ToString() + " in dictionary containing keys [" + Alg.MergedChars(keysCharList.Take(lengthCap)) + (keysCharList.Skip(lengthCap).Any() ? " ...<truncated for length>" : "") + "]"; return(errorMessage); }
public static IEnumerable <string> CharsToLines(IEnumerable <char> chars) { List <char> accumulator = new List <char>(); ReadPoint <char> currentChar = Alg.ReadPoint(chars); while (!currentChar.AtEnd) { char c = currentChar.Value; if (c == '\n') { yield return(Alg.MergedChars(accumulator)); accumulator = new List <char>(); } else if (c == '\r') { // have to handle '\r'-only case. if (!currentChar.Next.AtEnd) { if (currentChar.Next.Value != '\n') { yield return(Alg.MergedChars(accumulator)); accumulator = new List <char>(); } } } else { accumulator.Add(c); } currentChar = currentChar.Next; } if (accumulator.Count != 0) { yield return(Alg.MergedChars(accumulator)); accumulator = new List <char>(); } }
public static IEnumerable <string> LineToCSFEntries(string line, char delimiter, bool quotesHaveMeaning) { ReadPoint <char> currentChar = Alg.ReadPoint(line.ToCharArray()); List <char> accumulatedElement = new List <char>(); bool inQuote = false; bool haveSeenAComma = false; while (!currentChar.AtEnd) { char c = currentChar.Value; if (inQuote) { if (c == '\"') { // may be a double-quote if (IsAtCSVQuote(currentChar)) { accumulatedElement.Add('\"'); currentChar = currentChar.Next; // next add will be done at end of this loop. } else { accumulatedElement.Add(c); inQuote = false; } } else { accumulatedElement.Add(c); } } else { if (c == delimiter) { haveSeenAComma = true; yield return(Alg.MergedChars(accumulatedElement).Trim()); accumulatedElement = new List <char>(); } else { if (quotesHaveMeaning && (c == '\"')) { inQuote = true; accumulatedElement.Add(c); } else { accumulatedElement.Add(c); } } } currentChar = currentChar.Next; } if ((accumulatedElement.Count != 0) || haveSeenAComma) { yield return(Alg.MergedChars(accumulatedElement).Trim()); // deliberately return "last" accumulatedElement = new List <char>(); } }
public static string StringFromFile(IEnumerable <byte> fileContents) { return(Alg.MergedChars(BytesToChars(fileContents))); }