Skip to content

Commit

Permalink
Fix hex/decimal and binary conversion with an odd number of digits (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Mar 6, 2022
1 parent a9c920f commit d35a5f9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
14 changes: 10 additions & 4 deletions site/doc/api/misc.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ See the following examples.
### Example

```kalk
>>> bin 10
# bin(10)
out = "00001010 00000000 00000000 00000000"
>>> bin 13
# bin(13)
out = "00001101 00000000 00000000 00000000"
>>> bin out
# bin(out)
out = 10
out = 13
>>> bin "111111111011"
# bin("111111111011")
out = 4091
>>> bin 0xff030201
# bin(-16580095)
out = "00000001 00000010 00000011 11111111"
Expand Down Expand Up @@ -326,6 +329,9 @@ See the following examples.
>>> hex 10
# hex(10)
out = "0A"
>>> hex "12c"
# hex("12c")
out = 300
>>> hex "0a"
# hex("0a")
out = 10
Expand Down
3 changes: 0 additions & 3 deletions src/Directory.Build.props

This file was deleted.

14 changes: 10 additions & 4 deletions src/Kalk.Core/KalkEngine.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6539,6 +6539,9 @@ >>> date
descriptor.Example = @" >>> hex 10
# hex(10)
out = ""0A""
>>> hex ""12c""
# hex(""12c"")
out = 300
>>> hex ""0a""
# hex(""0a"")
out = 10
Expand Down Expand Up @@ -6587,12 +6590,15 @@ >>> hex(""1a,2b;3c 4d-5e_6f"")
When the binary input string can be converted to an integral less than or equal 8 bytes (64 bits)
it will convert it to a single integral result, otherwise it will convert to a bytebuffer.
See the following examples.";
descriptor.Example = @" >>> bin 10
# bin(10)
out = ""00001010 00000000 00000000 00000000""
descriptor.Example = @" >>> bin 13
# bin(13)
out = ""00001101 00000000 00000000 00000000""
>>> bin out
# bin(out)
out = 10
out = 13
>>> bin ""111111111011""
# bin(""111111111011"")
out = 4091
>>> bin 0xff030201
# bin(-16580095)
out = ""00000001 00000010 00000011 11111111""
Expand Down
62 changes: 49 additions & 13 deletions src/Kalk.Core/Modules/MiscModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ public IEnumerable Values(object obj)
/// >>> hex 10
/// # hex(10)
/// out = "0A"
/// >>> hex "12c"
/// # hex("12c")
/// out = 300
/// >>> hex "0a"
/// # hex("0a")
/// out = 10
Expand Down Expand Up @@ -423,12 +426,15 @@ public object Hexadecimal(object value, bool prefix = false, string separator =
/// </remarks>
/// <example>
/// ```kalk
/// >>> bin 10
/// # bin(10)
/// out = "00001010 00000000 00000000 00000000"
/// >>> bin 13
/// # bin(13)
/// out = "00001101 00000000 00000000 00000000"
/// >>> bin out
/// # bin(out)
/// out = 10
/// out = 13
/// >>> bin "111111111011"
/// # bin("111111111011")
/// out = 4091
/// >>> bin 0xff030201
/// # bin(-16580095)
/// out = "00000001 00000010 00000011 11111111"
Expand Down Expand Up @@ -1001,17 +1007,31 @@ private object Hexadecimal(object value, bool prefix, string separator, bool ret

void FlushBytes()
{
if (count > 0)
bool isOdd = count > 0;
if (isOdd)
{
array.Add((byte)hexa);
temp.Add((byte)(hexa << 4));
}
count = 0;

if (temp.Count > 0)
{
for (int j = temp.Count - 1; j >= 0; j--)
if (isOdd)
{
for (int j = temp.Count - 1; j >= 0; j--)
{
var hb = j - 1 >= 0 ? (temp[j - 1] & 0xF) << 4 : 0;
var lb = temp[j] >> 4;
var fullByte = (byte)(hb | lb);
array.Add(fullByte);
}
}
else
{
array.Add(temp[j]);
for (int j = temp.Count - 1; j >= 0; j--)
{
array.Add(temp[j]);
}
}

temp.Clear();
Expand Down Expand Up @@ -1197,25 +1217,41 @@ private object Binary(object value, bool prefix, string separator, bool returnSt

void FlushBytes()
{
bool isOdd = count > 0;
var shift = 8 - count;
if (count > 0)
{
array.Add((byte)bin);
temp.Add((byte)(bin << shift));
}
bin = 0;
count = 0;

if (temp.Count > 0)
{
for (int j = temp.Count - 1; j >= 0; j--)
if (isOdd)
{
array.Add(temp[j]);
for (int j = temp.Count - 1; j >= 0; j--)
{
var hb = j - 1 >= 0 ? (byte)((temp[j - 1]) << shift) : (byte)0;
var lb = temp[j] >> count;
var fullByte = (byte)(hb | lb);
array.Add(fullByte);
}
}
else
{
for (int j = temp.Count - 1; j >= 0; j--)
{
array.Add(temp[j]);
}
}

temp.Clear();
}

count = 0;
}

for (int i = 0; i < str.Length; i++)
for (int i = 0; i < str.Length; i++)
{
var c = str[i];
if (char.IsWhiteSpace(c) || c == ',' || c == ':' || c == ';' || c == '_' || c == '-')
Expand Down
14 changes: 10 additions & 4 deletions src/Kalk.Tests/KalkTests.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,7 @@ size float4(1,2,3,4)
/// Test for <see cref="M:Kalk.Core.MiscModule.Hexadecimal(System.Object,System.Boolean,System.String)"/> or `hex`.
/// </summary>
[TestCase(@"hex 10
hex ""12c""
hex ""0a""
hex ""0xff030201""
hex out
Expand All @@ -1800,6 +1801,8 @@ hex out
hex out
hex float4(1,2,3,4)", @"# hex(10)
out = ""0A""
# hex(""12c"")
out = 300
# hex(""0a"")
out = 10
# hex(""0xff030201"")
Expand Down Expand Up @@ -1831,18 +1834,21 @@ hex int (12345789)", @"# hex(short(12345))
/// <summary>
/// Test for <see cref="M:Kalk.Core.MiscModule.Binary(System.Object,System.Boolean,System.String)"/> or `bin`.
/// </summary>
[TestCase(@"bin 10
[TestCase(@"bin 13
bin out
bin ""111111111011""
bin 0xff030201
bin out
bin ""11111111000000110000001000000001""
bin(byte(5))
bin(long(6))
bin(out)
kind(out)", @"# bin(10)
out = ""00001010 00000000 00000000 00000000""
kind(out)", @"# bin(13)
out = ""00001101 00000000 00000000 00000000""
# bin(out)
out = 10
out = 13
# bin(""111111111011"")
out = 4091
# bin(-16580095)
out = ""00000001 00000010 00000011 11111111""
# bin(out)
Expand Down

0 comments on commit d35a5f9

Please sign in to comment.