Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/PerfView.Tests/Utilities/RangeUtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public static class RangeUtilitiesTests
[InlineData(TestCultureInfo.enUSCulture, "XXXXXXXXXXXXXXX 234,567,890.123", default(double), default(double), false)]
[InlineData(TestCultureInfo.enUSCulture, "123,456,789.123 XXXXXXXXXXXXXXX", default(double), default(double), false)]
[InlineData(TestCultureInfo.enUSCulture, "123,456,789.123 234,567,890.123", 123456789.123, 234567890.123, true)]
// Test cases for pipe-enclosed format (markdown table format)
[InlineData(TestCultureInfo.enUSCulture, "| 1,395.251\t 2,626.358 |", 1395.251, 2626.358, true)]
[InlineData(TestCultureInfo.enUSCulture, "| 123,456,789.123 234,567,890.123 |", 123456789.123, 234567890.123, true)]
[InlineData(TestCultureInfo.enUSCulture, "|123,456,789.123 234,567,890.123|", 123456789.123, 234567890.123, true)]
[InlineData(TestCultureInfo.ruRUCulture, "", default(double), default(double), false)]
[InlineData(TestCultureInfo.ruRUCulture, "XXXXXXXXXXXXXXX|234 567\u00A0890,123", default(double), default(double), false)]
[InlineData(TestCultureInfo.ruRUCulture, "123\u00A0456 789,123|XXXXXXXXXXXXXXX", default(double), default(double), false)]
[InlineData(TestCultureInfo.ruRUCulture, "123\u00A0456 789,123|234\u00A0567\u00A0890,123", 123456789.123, 234567890.123, true)]
// Test cases for pipe-enclosed format with Russian culture
[InlineData(TestCultureInfo.ruRUCulture, "| 123\u00A0456 789,123|234\u00A0567\u00A0890,123 |", 123456789.123, 234567890.123, true)]
[InlineData(TestCultureInfo.ptPTCulture, "", default(double), default(double), false)]
[InlineData(TestCultureInfo.ptPTCulture, "XXXXXXXXXXXXXXX|234 567 890,123", default(double), default(double), false)]
[InlineData(TestCultureInfo.ptPTCulture, "123 456 789,123|XXXXXXXXXXXXXXX", default(double), default(double), false)]
Expand All @@ -34,6 +40,8 @@ public static class RangeUtilitiesTests
[InlineData(TestCultureInfo.customCulture2, "XXXXXXXXXXXXXXX,234 567 890.123", default(double), default(double), false)]
[InlineData(TestCultureInfo.customCulture2, "123 456 789.123|XXXXXXXXXXXXXXX", default(double), default(double), false)]
[InlineData(TestCultureInfo.customCulture2, "123 456 789.123|234 567 890.123", 123456789.123, 234567890.123, true)]
// Test cases for pipe-enclosed format with custom culture
[InlineData(TestCultureInfo.customCulture2, "| 123 456 789.123|234 567 890.123 |", 123456789.123, 234567890.123, true)]
public static void TryParseTests(string culture, string text, double expectedStart, double expectedEnd, bool expectedResult)
{
var runner = RangeUtilitiesRunner.Create(culture);
Expand Down
7 changes: 7 additions & 0 deletions src/PerfView/Utilities/RangeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ private static string[] ResolveRangeSeparators()
/// <returns><see langword="true"/> if s was converted successfully; otherwise, <see langword="false"/>.</returns>
public static bool TryParse(string text, out double start, out double end)
{
// Trim whitespace and pipe symbols from the beginning and end to handle markdown table format
// e.g., "| 1,395.251 2,626.358 |" becomes "1,395.251 2,626.358"
if (!string.IsNullOrEmpty(text))
{
text = text.Trim(' ', '\t', '\n', '\r', '|');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be better to include those as range separators and leave it to text.Split? We are lucky here since string is a weird beast, hence we are not actually mutating the incoming text parameter, but in general it feels uneasy when the function parameter is being modified without explicitly being marked by ref or something like that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is trimming beginning and end. Split would split it everywhere. I guess we could probably after splitting work out only the ones at the start/end of resulting array, but that would be cumbersome. But anyway, feel free to propose better implementation.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh. Missed the part that it would split everywhere. So another approach could be "string index walk" identifying each char and collecting desired indices to be able to cut after a single walk. But I digress, most probably an overkill here.

}

// The user can set any character as a separator and that character can turn a regular expression invalid.
var parts = text.Split(rangeSeparators, StringSplitOptions.RemoveEmptyEntries);

Expand Down