Skip to content

Commit

Permalink
escape chars (#390)
Browse files Browse the repository at this point in the history
Co-authored-by: Jimmy <[email protected]>
  • Loading branch information
Ashuaidehao and Jim8y authored Nov 28, 2023
1 parent 4240bee commit 41f38c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/neoxp/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ public static bool TryGetBytesFromBase64String(this string text, out Span<byte>




public static string EscapeString(this string text)
{
if (text.Any(IsInvisibleChar))
{
var sb = new StringBuilder();
foreach (var c in text)
{
if (c.IsInvisibleChar())
{
sb.Append($"\\u{(int)c:x4}");
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
return text;
}

public static bool IsInvisibleChar(this char c)
{
return char.IsControl(c) || char.IsHighSurrogate(c) || char.IsLowSurrogate(c);
}

public static void WriteJson(this Newtonsoft.Json.JsonWriter writer, Neo.Json.JToken? json)
{
switch (json)
Expand Down
2 changes: 1 addition & 1 deletion src/neoxp/TransactionExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ static async Task WriteStackItemAsync(System.IO.TextWriter writer, Neo.VM.Types.
case Neo.VM.Types.ByteString byteString:
if (byteString.GetSpan().TryGetUtf8String(out var text))
{
await WriteLineAsync($"{Neo.Helper.ToHexString(byteString.GetSpan())}({text})").ConfigureAwait(false);
await WriteLineAsync($"{Neo.Helper.ToHexString(byteString.GetSpan())}({text.EscapeString()})").ConfigureAwait(false);
}
else
{
Expand Down

0 comments on commit 41f38c8

Please sign in to comment.