Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/json unicode escape #719

Merged
merged 2 commits into from
Feb 2, 2021
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
79 changes: 77 additions & 2 deletions Assets/UniGLTF/Runtime/UniJSON/Json/JsonString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void Escape(Utf8String s, IStore w)
}

var it = s.GetIterator();
while(it.MoveNext())
while (it.MoveNext())
{
var l = it.CurrentByteLength;
if (l == 1)
Expand Down Expand Up @@ -174,6 +174,43 @@ public static Utf8String Quote(Utf8String s)
#endregion

#region Unquote
static byte CheckHex(int b)
{
switch ((char)b)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A':
case 'a':
return 10;
case 'B':
case 'b':
return 11;
case 'C':
case 'c':
return 12;
case 'D':
case 'd':
return 13;
case 'E':
case 'e':
return 14;
case 'F':
case 'f':
return 15;
}

throw new ArgumentOutOfRangeException();
}

public static int Unescape(string src, IStore w)
{
int writeCount = 0;
Expand Down Expand Up @@ -223,6 +260,17 @@ public static int Unescape(string src, IStore w)
Write('\t');
i += 2;
continue;
case 'u':
{
var u0 = CheckHex(src[i + 2]);
var u1 = CheckHex(src[i + 3]);
var u2 = CheckHex(src[i + 4]);
var u3 = CheckHex(src[i + 5]);
var u = (u0 << 12) + (u1 << 8) + (u2 << 4) + u3;
Write((char)u);
i += 6;
}
continue;
}
}

Expand Down Expand Up @@ -250,7 +298,7 @@ public static int Unescape(Utf8String s, IStore w)
};

var it = s.GetIterator();
while(it.MoveNext())
while (it.MoveNext())
{
var l = it.CurrentByteLength;
if (l == 1)
Expand Down Expand Up @@ -288,6 +336,33 @@ public static int Unescape(Utf8String s, IStore w)
Write((Byte)'\t');
it.MoveNext();
continue;
case (Byte)'u':
{
// skip back slash
it.MoveNext();
// skip u
it.MoveNext();

var u0 = CheckHex((char)it.Current);
it.MoveNext();

var u1 = CheckHex((char)it.Current);
it.MoveNext();

var u2 = CheckHex((char)it.Current);
it.MoveNext();

var u3 = CheckHex((char)it.Current);

var u = (u0 << 12) + (u1 << 8) + (u2 << 4) + u3;
var utf8 = Utf8String.From(new string(new char[] { (char)u }));
// var utf8 = Utf8String.From((int)u);
foreach (var x in utf8.Bytes)
{
Write(x);
}
}
continue;
}
}

Expand Down
40 changes: 39 additions & 1 deletion Assets/UniGLTF/Tests/UniJSON/Utf8StringTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using System.IO;
using System.Linq;

using System.Text;

namespace UniJSON
{
Expand Down Expand Up @@ -164,5 +165,42 @@ public void FromStringTest()
Assert.AreEqual(src, utf8.ToString());
}
}

[Test]
public void UnicodeEscapeTest()
{
// UTF-8 Encoding: 0xE8 0xA1 0xA8
// UTF-16 Encoding: 0x8868
// UTF-32 Encoding: 0x00008868

{
var src = "\"\\u8868\\u60c5\"";
var json = src.ParseAsJson();
var decoded = json.GetString();
Assert.AreEqual("表情", decoded);
}

{
Assert.AreEqual("表"[0], (char)0x8868);
}

{
var src = Utf8String.From("表");
var dst = src.Bytes.ToArray();
Assert.AreEqual(0xE8, dst[0]);
Assert.AreEqual(0xA1, dst[1]);
Assert.AreEqual(0xA8, dst[2]);
}

{
var src = Utf8String.From("\\u8868");
var ms = new MemoryStream();
JsonString.Unescape(src, new StreamStore(ms));
var dst = ms.ToArray();
Assert.AreEqual(0xE8, dst[0]);
Assert.AreEqual(0xA1, dst[1]);
Assert.AreEqual(0xA8, dst[2]);
}
}
}
}