internal static string ReadUntilDelimiter(StringBuffer fullString, ICollection <char> delimeters) { var value = new StringBuffer(); char nextChar; while (!fullString.IsEmpty && !delimeters.Contains(nextChar = fullString.PeekStart())) { if (nextChar == SpChar_EscapeChar) { fullString.PopFromStart(); // Skip the escape char. value.PushToEnd(fullString.PopFromStart()); // Allow the escaped char into the value. } else if (nextChar == SpChar_LiteralStringBlock) { fullString.PopFromStart(); // Skip the escape char. while (!fullString.IsEmpty && (nextChar = fullString.PopFromStart()) != SpChar_LiteralStringBlock) { value.PushToEnd(nextChar); // Grab everything contained between the " chars except the " chars } } else { value.PushToEnd(fullString.PopFromStart()); // Basic case } } fullString.PopFromStart(); // Skip delimeter return(value.ToString()); }
private static StringBuffer CleanValue(StringBuffer rawValue, bool stopAtKey = false) { var cleanValue = new StringBuffer(); while (!rawValue.IsEmpty) { switch (rawValue.PeekStart()) { case ' ': // spaces rawValue.TrimStart(' '); break; case '\t': // tabs rawValue.TrimStart('\t'); break; case '\r': // line breaks case '\n': rawValue.TrimStart('\r', '\n'); break; case SpChar_CommentBlock: // comments rawValue.PopFromStart(); // Pop first # char poppedChar; do { poppedChar = rawValue.PopFromStart(); } while (!rawValue.IsEmpty && poppedChar != SpChar_CommentBlock); break; case SpChar_KeyDelimiter when stopAtKey: return(cleanValue); case SpChar_LiteralStringBlock: cleanValue.PushToEnd(rawValue.PopFromStart()); // Add first " char popped; do { popped = rawValue.PopFromStart(); cleanValue.PushToEnd(popped); } while (popped != SpChar_LiteralStringBlock); break; case SpChar_EscapeChar: cleanValue.PushToEnd(rawValue.PopFromStart()); // Pop escape char cleanValue.PushToEnd(rawValue.PopFromStart()); // Add escaped char break; default: cleanValue.PushToEnd(rawValue.PopFromStart()); break; } } return(cleanValue); }
public void Replace(char[] original, char[] replacement) { if (IsEmpty) { return; } var finalValue = new StringBuffer(this.Count); var limbo = new StringBuffer(this.Count); int cIndex = 0; int oCount = original.Length; do { if (this.PeekStart() == original[cIndex]) { limbo.PushToEnd(this.PopFromStart()); cIndex++; if (cIndex == oCount) // Full match { foreach (char c in replacement) { finalValue.PushToEnd(c); } limbo.Clear(); cIndex = 0; } } else { cIndex = 0; while (!limbo.IsEmpty) { finalValue.PushToEnd(limbo.PopFromStart()); } finalValue.PushToEnd(this.PopFromStart()); } } while (!this.IsEmpty); do { this.PushToEnd(finalValue.PopFromStart()); } while (!finalValue.IsEmpty); }
internal static string EscapeSpecialCharacters(string unescapedValue) { if (string.IsNullOrEmpty(unescapedValue)) { return(string.Empty); } var original = new StringBuffer(unescapedValue); var escaped = new StringBuffer(); if (original.Contains(' ')) { escaped.PushToEnd(SpChar_LiteralStringBlock); escaped.TransferToEnd(original); escaped.PushToEnd(SpChar_LiteralStringBlock); } else { while (!original.IsEmpty) { switch (original.PeekStart()) { case SpChar_KeyDelimiter: case SpChar_ValueDelimiter: case SpChar_BeginComplexValue: case SpChar_FinishComplexValue: case SpChar_ListItemSplitter: case SpChar_CommentBlock: case SpChar_EscapeChar: escaped.PushToEnd(SpChar_EscapeChar); break; default: break; } escaped.PushToEnd(original.PopFromStart()); } } return(escaped.ToString()); }
protected virtual StringBuffer ExtractKey(StringBuffer fullString) { var key = new StringBuffer(); while (fullString.Count > 0 && fullString.PeekStart() != ':') { key.PushToEnd(fullString.PopFromStart()); } fullString.PopFromStart(); // Skip : separator return(key); }
protected override string ExtractValue(StringBuffer fullString) { string serialValues = $"{SpChar_BeginComplexValue}"; int openParens = 0; var buffer = new StringBuffer(); do { switch (fullString.PeekStart()) { case SpChar_ValueDelimiter when openParens == 0: // End of ComplexList case SpChar_ListItemSplitter when openParens == 0 && fullString.Count > 0: // End of a nested property belonging to this collection fullString.PopFromStart(); // Skip delimiter var collection = (ListedType)Template.Copy(); collection.FromString($"{this.Key}{SpChar_KeyDelimiter}{buffer.ToString()}{SpChar_ValueDelimiter}"); this.Values.Add(collection); buffer.Clear(); serialValues += $"{collection.SerializedValue}{SpChar_ListItemSplitter}"; break; case SpChar_BeginComplexValue: openParens++; goto default; case SpChar_FinishComplexValue: openParens--; if (openParens < 0) { throw new EmException(UnbalancedContainersError, buffer); } goto default; default: buffer.PushToEnd(fullString.PopFromStart()); break; } } while (fullString.Count > 0); if (openParens != 0) { throw new EmException(UnbalancedContainersError, buffer); } return(serialValues.TrimEnd(SpChar_ListItemSplitter) + SpChar_FinishComplexValue); }
public string PrettyPrint() { string originalValue = ToString(); if (string.IsNullOrEmpty(originalValue)) { return(string.Empty); } var originalString = new StringBuffer(originalValue); var prettyString = new StringBuffer(); int indentLevel = 0; const int indentSize = 4; do { switch (originalString.PeekStart()) { case SpChar_BeginComplexValue: prettyString.PushToEnd('\r', '\n'); prettyString.PushToEnd(' ', indentLevel * indentSize); indentLevel++; prettyString.PushToEnd(originalString.PopFromStart()); prettyString.PushToEnd('\r', '\n'); prettyString.PushToEnd(' ', indentLevel * indentSize); prettyString.PushToEnd(originalString.PopFromStart()); break; case SpChar_ValueDelimiter: prettyString.PushToEnd(originalString.PopFromStart()); if (originalString.IsEmpty || originalString.PeekStart() == SpChar_FinishComplexValue) { indentLevel--; } prettyString.PushToEnd('\r', '\n'); prettyString.PushToEnd(' ', indentLevel * indentSize); break; case SpChar_CommentBlock: prettyString.PushToEnd(originalString.PopFromStart()); do { prettyString.PushToEnd(originalString.PopFromStart()); } while (!originalString.IsEmpty && prettyString.PeekEnd() != SpChar_CommentBlock); prettyString.PushToEnd('\r', '\n'); prettyString.PushToEnd(' ', indentLevel * indentSize); break; case SpChar_KeyDelimiter: prettyString.PushToEnd(originalString.PopFromStart()); prettyString.PushToEnd(' '); // Add a space after every KeyDelilmiter break; default: prettyString.PushToEnd(originalString.PopFromStart()); break; } } while (!originalString.IsEmpty); return(prettyString.ToString()); }