Skip to content

Commit

Permalink
Merge pull request #908 from VitaliyKurokhtin/apim-hexstring-notation…
Browse files Browse the repository at this point in the history
…-support

Adding support to write c-style hex strings as strings
  • Loading branch information
baywet authored Jun 23, 2022
2 parents d2b2774 + e125a56 commit 5fa5c65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 5fa5c65

Please sign in to comment.