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
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ internal static string GetYamlCompatibleString(this string input)
return $"'{input}'";
}

// If string can be mistaken as a number, a boolean, or a timestamp,
// wrap it in quote to indicate that this is indeed a string, not a number, a boolean, or a timestamp
// If string can be mistaken as a number, c-style hexadecimal notation, a boolean, or a timestamp,
// wrap it in quote to indicate that this is indeed a string, not a number, c-style hexadecimal notation, a boolean, or a timestamp
if (decimal.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var _) ||
IsHexadecimalNotation(input) ||
bool.TryParse(input, out var _) ||
DateTime.TryParse(input, out var _))
{
Expand Down Expand Up @@ -225,5 +226,10 @@ internal static string GetJsonCompatibleString(this string value)

return $"\"{value}\"";
}

internal static bool IsHexadecimalNotation(string input)
{
return input.StartsWith("0x") && int.TryParse(input.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var _);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ from inputExpected in new[] {
new[]{ "Test\\Test", "\"Test\\\\Test\""},
new[]{ "Test\"Test", "\"Test\\\"Test\""},
new[]{ "StringsWith\"Quotes\"", "\"StringsWith\\\"Quotes\\\"\""},
new[]{ "0x1234", "\"0x1234\""},
}
from shouldBeTerse in shouldProduceTerseOutputValues
select new object[] { inputExpected[0], inputExpected[1], shouldBeTerse };
Expand Down Expand Up @@ -79,6 +80,7 @@ public void WriteStringWithSpecialCharactersAsJsonWorks(string input, string exp
[InlineData("trailingspace ", " 'trailingspace '")]
[InlineData(" trailingspace", " ' trailingspace'")]
[InlineData("terminal:", " 'terminal:'")]
[InlineData("0x1234", " '0x1234'")]
public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string expected)
{
// Arrange
Expand Down