diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua index 1c158808..8fc0541c 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua @@ -163,7 +163,7 @@ local function get(t, index) if v == nil then throw(ArgumentOutOfRangeException("index")) end - if v ~= null then + if v ~= null then return v end return nil diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua index 601b61bb..bf19c27d 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua @@ -501,13 +501,11 @@ local function fromBase64Decode(s, offset, len, t, resultLength) local coroutine local c = s:get(i + offset) i = i + 1 - if c >= 65 then - if c <= 90 then - c = c - 65 - elseif c <= 122 then - c = c - 71 - end - elseif c >= 48 then + if c >= 65 and c <= 90 then + c = c - 65 + elseif c >= 97 and c <= 122 then + c = c - 71 + elseif c >= 48 and c <= 57 then c = c + 4 else if c == 43 then @@ -545,7 +543,7 @@ local function fromBase64Decode(s, offset, len, t, resultLength) throw(FormatException("Format_BadBase64CharArrayLength")) end - if j < 2 then + if resultLength - j < 2 then return - 1 end @@ -555,7 +553,7 @@ local function fromBase64Decode(s, offset, len, t, resultLength) codes = 0x000000ff else while i < len - 1 do - c = s:get(i + offset) + local c = s:get(i + offset) if c ~= 32 and c ~= 10 and c ~= 13 and c ~= 9 then break end @@ -596,7 +594,6 @@ local function fromBase64CharPtr(s, i, n) n = n - 1 end local resultLength = fromBase64ComputeResultLength(s, i, n) - print(resultLength .. " xxx") local t = {} fromBase64Decode(s, i, n, t, resultLength) return System.arrayFromTable(t, Byte) diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua index ea6deaed..d5bba97e 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua @@ -73,13 +73,50 @@ local function checkIndex(value, startIndex, count) return startIndex, count, len end +-- https://stackoverflow.com/questions/7983574/how-to-write-a-unicode-symbol-in-lua +local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} } +local function utf8(decimal) + if decimal < 128 then return char(decimal) end + local charbytes = {} + for i = 1, #bytemarkers do + local vals = bytemarkers[i] + if decimal<=vals[1] then + for b = i + 1, 2, -1 do + local mod = decimal%64 + decimal = (decimal-mod)/64 + charbytes[b] = char(128+mod) + end + charbytes[1] = char(vals[2]+decimal) + break + end + end + return tconcat(charbytes) +end + local function ctor(String, value, startIndex, count) if type(value) == "number" then if startIndex <= 0 then throw(ArgumentOutOfRangeException("count")) end return rep(char(value), startIndex) end startIndex, count = checkIndex(value, startIndex, count) - return char(unpack(value, startIndex + 1, startIndex + count)) + local found + for i = startIndex + 1, startIndex + count do + local c = value[i] + if c >= 128 then + found = true + break + end + end + if not found then + return char(unpack(value, startIndex + 1, startIndex + count)) + end + local t, index = {}, 1 + for i = startIndex + 1, startIndex + count do + local c = value[i] + t[index] = utf8(c) + index = index + 1 + end + return tconcat(t) end local function get(this, index) diff --git a/test/BridgeNetTests/Batch1/src/BasicCSharp/TestFromIssue.cs b/test/BridgeNetTests/Batch1/src/BasicCSharp/TestFromIssue.cs index 42405786..eb954c70 100644 --- a/test/BridgeNetTests/Batch1/src/BasicCSharp/TestFromIssue.cs +++ b/test/BridgeNetTests/Batch1/src/BasicCSharp/TestFromIssue.cs @@ -123,6 +123,14 @@ public static void TestOf458() .ToList(); } + [Test] + public static void TestOf441() + { + var input = "aGVsbG8gd29ybGQ="; + var bytes = Convert.FromBase64String(input); + Assert.AreEqual(input, Convert.ToBase64String(bytes)); + } + private class BattleModelSlotPrototype { public string a; public string b; diff --git a/test/BridgeNetTests/Batch1/src/Convert/ConvertFromBase64Tests.cs b/test/BridgeNetTests/Batch1/src/Convert/ConvertFromBase64Tests.cs index a9807609..8f52765e 100644 --- a/test/BridgeNetTests/Batch1/src/Convert/ConvertFromBase64Tests.cs +++ b/test/BridgeNetTests/Batch1/src/Convert/ConvertFromBase64Tests.cs @@ -5,7 +5,7 @@ using Bridge.Test.NUnit; using System; -#if false +#if true namespace Bridge.ClientTest.ConvertTests { [Category(Constants.MODULE_CONVERT)]