From 7edf902fea82f612e251a9b5a09af7bdfb782a6b Mon Sep 17 00:00:00 2001 From: Simon Lindgren Date: Thu, 5 Jul 2012 14:48:13 +0200 Subject: [PATCH] [Utils] More error handling in CompositeFormatStringParser. --- .../CompositeFormatStringParserTests.cs | 20 ++++++++++++++++ .../CompositeFormatStringParser.cs | 24 +++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs b/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs index b07bbcc99f..0896009190 100644 --- a/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs +++ b/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs @@ -244,6 +244,26 @@ namespace ICSharpCode.NRefactory.Utils var errors = SegmentTest(1, segments.Skip(1).First()); ErrorTest(errors[0], "", "0", 11, 11); } + + [Test] + public void MissingAlignment() + { + var segments = ParseTest("Some text {0,}", + new TextSegment("Some text "), + new FormatItem(0, 0) { StartLocation = 10, EndLocation = 14 }); + var errors = SegmentTest(1, segments.Skip(1).First()); + ErrorTest(errors[0], "", "0", 13, 13); + } + + [Test] + public void MissingEveryThing() + { + var segments = ParseTest("{,:", new FormatItem(0, 0, "") { StartLocation = 0, EndLocation = 3 }); + var errors = SegmentTest(3, segments.First()); + ErrorTest(errors[0], "", "0", 1, 1); + ErrorTest(errors[1], "", "0", 2, 2); + ErrorTest(errors[2], "", "}", 3, 3); + } } } diff --git a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs index 0093604305..c663ee1e65 100644 --- a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs +++ b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs @@ -25,6 +25,7 @@ // THE SOFTWARE. using System; using System.Collections.Generic; +using System.Globalization; namespace ICSharpCode.NRefactory.Utils { @@ -151,11 +152,15 @@ namespace ICSharpCode.NRefactory.Utils var originalText = format.Substring (alignmentBegin); var message = string.Format ("Unexpected end of string: '{0}'", originalText); AddMissingEndBraceError(alignmentBegin, i, message, originalText); - } else if (format [i] == '-') { - ++i; - return -int.Parse (GetUntil (format, ":}", ref i)); } else { - return int.Parse (GetUntil (format, ":}", ref i)); + var number = GetUntil (format, ":}", ref i); + int value; + if (int.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { + return value; + } else { + AddInvalidNumberFormatError(i, number, "0"); + return 0; + } } } return null; @@ -269,6 +274,17 @@ namespace ICSharpCode.NRefactory.Utils hasMissingEndBrace = true; } + void AddInvalidNumberFormatError (int i, string number, string replacementText) + { + AddError (new DefaultFormatStringError { + StartLocation = i, + EndLocation = i + number.Length, + Message = string.Format ("Invalid number '{0}'", number), + OriginalText = number, + SuggestedReplacementText = replacementText + }); + } + IList GetErrors () { return errors;