Skip to content

Commit

Permalink
Add some characters to International CTD encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 28, 2020
1 parent 8ba842e commit 2170831
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
12 changes: 12 additions & 0 deletions OpenKh.Bbs/Messages/Internals/InternationalCtdEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class InternationalCtdEncoder : ICtdMessageEncoder
{
private static readonly string _mapping0 =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[¥]^_`abcdefghijklmnopqrstuvwxyz{|}~";
private static readonly string _mapping99 =
"ÀÁÂÄÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖŒÙÚÛÜßàáâäæçèéêëìíîïñòóôõöùúûüœ¿¡";

private static readonly Dictionary<char, int> _inverseMapping = _mapping0
.Select((x, i) => new { Ch = x, Data = i + 0x20 })
Expand Down Expand Up @@ -43,9 +45,19 @@ private static char GetCharacter(byte[] data, ref int index)
{
var ch = data[index++];

if (ch >= 0x00 && ch < 0x20)
return (char)ch;

if (ch >= 0x20 && ch < 0x80)
return _mapping0[ch - 0x20];

var ch2 = data[index++];
switch (ch)
{
case 0x99:
return _mapping99[ch2 - 0x80];
}

throw new Exception($"Data {ch:X02} cannot be decoded.");
}
}
Expand Down
34 changes: 25 additions & 9 deletions OpenKh.Tests/Bbs/CtdEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@ public class CtdEncodingTests
[Theory]
[InlineData(' ', 0x20)]
[InlineData('A', 0x41)]
public void InternationalEncodingTest(char ch, byte expected, byte? expected2 = null)
[InlineData('~', 0x7e)]
public void InternationalEncodingSimpleTest(char ch, byte expected)
{
var actual = euEnc.Encode($"{ch}");
Assert.Equal(expected, actual[0]);
if (expected2.HasValue)
Assert.Equal(expected2, actual[1]);
}

[Theory]
[InlineData('Ò', 0x99, 0x8f)]
[InlineData('ç', 0x99, 0x9f)]
[InlineData('ú', 0x99, 0xaf)]
public void InternationalEncodingExtTest(char ch, byte expected, byte expected2)
{
var actual = euEnc.Encode($"{ch}");
Assert.Equal(expected, actual[0]);
Assert.Equal(expected2, actual[1]);
}

[Theory]
[InlineData(' ', 0x20)]
[InlineData('A', 0x41)]
public void InternationalDecodingTest(char expected, byte ch, byte? ch2 = null)
[InlineData('~', 0x7e)]
public void InternationalDecodingSimpleTest(char expected, byte ch)
{
string actual;
if (ch2.HasValue)
actual = euDec.Decode(new byte[] { ch, ch2.Value });
else
actual = euDec.Decode(new byte[] { ch });
string actual = euDec.Decode(new byte[] { ch });
Assert.Equal(expected, actual[0]);
}

[Theory]
[InlineData('Ò', 0x99, 0x8f)]
[InlineData('ç', 0x99, 0x9f)]
[InlineData('ú', 0x99, 0xaf)]
public void InternationalDecodingExtTest(char expected, byte ch, byte ch2)
{
string actual = euDec.Decode(new byte[] { ch, ch2 });
Assert.Equal(expected, actual[0]);
}
}
Expand Down

0 comments on commit 2170831

Please sign in to comment.