From af7d2149c9910e946561f1815eb9bef79ef25be1 Mon Sep 17 00:00:00 2001 From: Simon Lindgren Date: Thu, 5 Jul 2012 15:30:17 +0200 Subject: [PATCH] [Utils] Some CompositeFormatStringParser cleanups. --- .../CompositeFormatStringParser.cs | 43 ++++++------------- .../TextSegment.cs | 10 ----- 2 files changed, 13 insertions(+), 40 deletions(-) diff --git a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs index c663ee1e65..bc3bb896cd 100644 --- a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs +++ b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs @@ -83,7 +83,7 @@ namespace ICSharpCode.NRefactory.Utils } else { // This is the end of the text segment and the start of a FormatItem if (i - start > 0) { - yield return TextSegment.FromUnescapedText (format.Substring (start, i - start)); + yield return new TextSegment (UnEscape (format.Substring (start, i - start))); start = i; } } @@ -121,7 +121,7 @@ namespace ICSharpCode.NRefactory.Utils } // Handle remaining text if (start < length) { - yield return new TextSegment (TextSegment.UnEscape (format.Substring (start)), start); + yield return new TextSegment (UnEscape (format.Substring (start)), start); } } @@ -153,12 +153,11 @@ namespace ICSharpCode.NRefactory.Utils var message = string.Format ("Unexpected end of string: '{0}'", originalText); AddMissingEndBraceError(alignmentBegin, i, message, originalText); } else { - var number = GetUntil (format, ":}", ref i); - int value; - if (int.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { - return value; + int? number = GetNumber(format, ref i); + if (number.HasValue) { + return number; } else { - AddInvalidNumberFormatError(i, number, "0"); + AddInvalidNumberFormatError(i, "", "0"); return 0; } } @@ -187,33 +186,11 @@ namespace ICSharpCode.NRefactory.Utils } } var escaped = format.Substring (begin, i - begin); - return TextSegment.UnEscape (escaped); + return UnEscape (escaped); } return null; } - string GetFormatItemText (string src, int index, out int endIndex) - { - int length = src.Length; - int begin = index; - while (index < length) { - var c = src [index]; - ++index; - if (c != '}') { - continue; - } else if (index + 1 < length && src [index + 1] == '}') { - // Step past escape sequence - ++index; - continue; - } else { - // This is the end of the FormatItem - break; - } - } - endIndex = index < length ? index : length; - return src.Substring (begin, endIndex - begin); - } - void CheckForMissingEndBrace (string format, int i, int length) { if (i == length && format [length - 1] != '}') { @@ -251,6 +228,11 @@ namespace ICSharpCode.NRefactory.Utils return positive ? sum : -sum; } + public static string UnEscape (string unEscaped) + { + return unEscaped.Replace ("{{", "{").Replace ("}}", "}"); + } + IList errors; bool hasMissingEndBrace = false; @@ -262,6 +244,7 @@ namespace ICSharpCode.NRefactory.Utils void AddMissingEndBraceError(int start, int end, string message, string originalText) { + // Only add a single missing end brace per format item if (hasMissingEndBrace) return; AddError (new DefaultFormatStringError { diff --git a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/TextSegment.cs b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/TextSegment.cs index 2d8f1e0eea..9b143ba80d 100644 --- a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/TextSegment.cs +++ b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/TextSegment.cs @@ -35,16 +35,6 @@ namespace ICSharpCode.NRefactory.Utils EndLocation = endLocation ?? startLocation + text.Length; } - public static IFormatStringSegment FromUnescapedText (string text) - { - return new TextSegment (UnEscape (text)); - } - - public static string UnEscape (string unEscaped) - { - return unEscaped.Replace ("{{", "{").Replace ("}}", "}"); - } - public string Text { get; set; } #region Equality