Skip to content

Commit 73bd5ce

Browse files
committed
add Base10
1 parent ad7ea41 commit 73bd5ce

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

src/Base10.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <copyright file="Base10.cs" company="Sedat Kapanoglu">
2+
// Copyright (c) 2014-2025 Sedat Kapanoglu
3+
// Licensed under Apache-2.0 License (see LICENSE.txt file for details)
4+
// </copyright>
5+
6+
using System;
7+
8+
namespace SimpleBase;
9+
10+
/// <summary>
11+
/// Base10 encoder.
12+
/// </summary>
13+
/// <param name="alphabet">Alphabet to use.</param>
14+
public class Base10(Base10Alphabet alphabet): DividingCoder<Base10Alphabet>(alphabet)
15+
{
16+
/// <summary>
17+
/// Default Base10 implementation.
18+
/// </summary>
19+
static readonly Lazy<Base10> @default = new(() => new Base10(Base10Alphabet.Default));
20+
21+
/// <summary>
22+
/// Default Base10 implementation.
23+
/// </summary>
24+
public static Base10 Default => @default.Value;
25+
}

src/Base10Alphabet.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace SimpleBase;
4+
5+
/// <summary>
6+
/// Base 10 encoding alphabet.
7+
/// </summary>
8+
/// <param name="alphabet">Characters to use for encoding.</param>
9+
public class Base10Alphabet(string alphabet) : CodingAlphabet(10, alphabet)
10+
{
11+
/// <summary>
12+
/// Default Base10 alphabet.
13+
/// </summary>
14+
static readonly Lazy<Base10Alphabet> @default = new(() => new Base10Alphabet("0123456789"));
15+
16+
/// <summary>
17+
/// Standard Base10 alphabet ("0123456789").
18+
/// </summary>
19+
public static Base10Alphabet Default => @default.Value;
20+
}

src/PublicAPI.Unshipped.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
SimpleBase.Base2
1+
SimpleBase.Base10
2+
SimpleBase.Base10.Base10(SimpleBase.Base10Alphabet! alphabet) -> void
3+
SimpleBase.Base10Alphabet
4+
SimpleBase.Base10Alphabet.Base10Alphabet(string! alphabet) -> void
5+
SimpleBase.Base2
26
SimpleBase.Base2.Base2() -> void
37
SimpleBase.Base2.Decode(System.IO.TextReader! input, System.IO.Stream! output) -> void
48
SimpleBase.Base2.Decode(System.ReadOnlySpan<char> text) -> byte[]!
@@ -22,5 +26,7 @@ SimpleBase.Base8.GetSafeByteCountForDecoding(System.ReadOnlySpan<char> text) ->
2226
SimpleBase.Base8.GetSafeCharCountForEncoding(System.ReadOnlySpan<byte> buffer) -> int
2327
SimpleBase.Base8.TryDecode(System.ReadOnlySpan<char> input, System.Span<byte> output, out int bytesWritten) -> bool
2428
SimpleBase.Base8.TryEncode(System.ReadOnlySpan<byte> input, System.Span<char> output, out int numCharsWritten) -> bool
29+
static SimpleBase.Base10.Default.get -> SimpleBase.Base10!
30+
static SimpleBase.Base10Alphabet.Default.get -> SimpleBase.Base10Alphabet!
2531
static SimpleBase.Base2.Default.get -> SimpleBase.Base2!
2632
static SimpleBase.Base8.Default.get -> SimpleBase.Base8!

test/Base10Test.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2014-2025 Sedat Kapanoglu
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
using NUnit.Framework;
17+
using SimpleBase;
18+
19+
namespace SimpleBaseTest;
20+
21+
[TestFixture]
22+
class Base10Test
23+
{
24+
static readonly object[][] zeroPrefixedTestData =
25+
[
26+
[new byte[] { 0x00, 0x01 }, "01"],
27+
[new byte[] { 0x00, 0x00, 0xFF }, "00255"],
28+
[new byte[] { 0x00, 0x01, 0x00 }, "0256"],
29+
];
30+
31+
static readonly object[][] testData =
32+
[
33+
[new byte[] { }, ""],
34+
[new byte[] { 0x01, 0x00 }, "256"],
35+
[new byte[] { 0x01, 0x00, 0x00 }, "65536"],
36+
];
37+
38+
[Test]
39+
[TestCaseSource(nameof(testData))]
40+
public void Encode_EncodesCorrectly(byte[] decoded, string encoded)
41+
{
42+
string result = Base10.Default.Encode(decoded);
43+
Assert.That(result, Is.EqualTo(encoded));
44+
}
45+
46+
[Test]
47+
[TestCaseSource(nameof(zeroPrefixedTestData))]
48+
public void Encode_ZeroPrefixed_EncodesCorrectly(byte[] decoded, string encoded)
49+
{
50+
string result = Base10.Default.Encode(decoded);
51+
Assert.That(result, Is.EqualTo(encoded));
52+
}
53+
54+
[Test]
55+
[TestCaseSource(nameof(testData))]
56+
public void Decode_DecodesCorrectly(byte[] decoded, string encoded)
57+
{
58+
var bytes = Base10.Default.Decode(encoded);
59+
Assert.That(bytes, Is.EqualTo(decoded));
60+
}
61+
}

0 commit comments

Comments
 (0)