public static string Decompress(string input) { var outputBuilder = new StringBuilder(); var inputBuilder = new StringBuilder(input); while(inputBuilder.Length > 0) { switch (inputBuilder.FirstChar()) { case MultiVerbatimCharMarker: inputBuilder.PopChar(); var length = inputBuilder.PopChar(); outputBuilder.AppendAndPopCharFrom(inputBuilder, length); break; case SingleVerbatimCharMarker: inputBuilder.PopChar(); outputBuilder.AppendAndPopCharFrom(inputBuilder); break; default: outputBuilder.Append(PartDictionary[inputBuilder.PopChar()]); break; } } return outputBuilder.ToString(); }
public static string Compress(string input) { var outputBuilder = new StringBuilder(); var inputBuilder = new StringBuilder(input); var verbatimBuilder = new StringBuilder(); while (inputBuilder.Length > 0) { var possibleParts = PartDictionary .Where(p => inputBuilder.ToString().StartsWith(p)) .ToList(); if (verbatimBuilder.Length == char.MaxValue) FlushVerbatim(outputBuilder, verbatimBuilder); if (!possibleParts.Any()) { verbatimBuilder.AppendAndPopCharFrom(inputBuilder); continue; } FlushVerbatim(outputBuilder, verbatimBuilder); var bestFit = possibleParts.OrderByDescending(p => p.Length).First(); outputBuilder.Append((char)PartDictionary.ToList().IndexOf(bestFit)); inputBuilder.Remove(0, bestFit.Length); } FlushVerbatim(outputBuilder, verbatimBuilder); return outputBuilder.ToString(); }