From 2b7518e81f58e8813c59c663581dca18aecd07fb Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 26 Oct 2023 16:54:31 -0700 Subject: [PATCH] initial commits --- symbols.go | 78 ++++++++++++++ symbols_test.go | 271 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 349 insertions(+) create mode 100644 symbols.go create mode 100644 symbols_test.go diff --git a/symbols.go b/symbols.go new file mode 100644 index 0000000..d1ceb91 --- /dev/null +++ b/symbols.go @@ -0,0 +1,78 @@ +package hexadeca + +// SymbolLowerCase is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using lower-case symbols from ASCII / Unicode UTF-8. +// I.e., +// +// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' +// +// Example usage: +// +// mostSignificiant, leastSignificant := hexadeca.EncodeByte(value, hexadeca.SymbolLowerCase) +// +// If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'f' and and ‘leastSignificant’ would be 'e'. +func SymbolLowerCase(value byte) rune { + const table string = "0123456789abcdef" + + var index int = int(value) % len(table) + + var result rune = rune(table[index]) + + return result +} + +// SymbolPersian is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using Persian symbols from Unicode UTF-8. +// I.e., +// +// '۰' (zero), '۱' (one), '۲' (two), '۳' (three), '۴' (four), '۵' (five), '۶' (six), '۷' (seven), '۸' (eight) ,'۹' (nine), 'ا' (alef), 'ب ' (be), 'پ ' (pe), 'ت ' (te), 'ث ' (s̱e), 'ج' (jim) +// +// Example usage: +// +// mostSignificiant, leastSignificant := hexadeca.EncodeByte(value, hexadeca.SymbolPersian) +// +// If ‘value’ was 90 (== 0x5A) then ‘mostSignificiant’ would be '۵' and and ‘leastSignificant’ would be '\u0627'. +func SymbolPersian(value byte) rune { + var table [16]rune = [16]rune{ + '\u06F0', // ۰ (zero) + '\u06F1', // ۱ (one) + '\u06F2', // ۲ (two) + '\u06F3', // ۳ (three) + '\u06F4', // ۴ (four) + '\u06F5', // ۵ (five) + '\u06F6', // ۶ (six) + '\u06F7', // ۷ (seven) + '\u06F8', // ۸ (eight) + '\u06F9', // ۹ (nine) + '\u0627', // ا (alef) + '\u0628', // ب (be) + '\u067E', // پ (pe) + '\u062A', // ت (te) + '\u062B', // ث (s̱e) + '\u062C', // ج (jim) + } + + var index int = int(value) % len(table) + + var result rune = table[index] + + return result +} + +// SymbolUpperCase is used with hexadeca.EncodeByte() to encode a byte into hexadecimal symbols using upper-case symbols from ASCII / Unicode UTF-8. +// I.e., +// +// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +// +// Example usage: +// +// mostSignificiant, leastSignificant := hexadeca.EncodeByte(value, hexadeca.SymbolUpperCase) +// +// If ‘value’ was 254 (== 0xFE) then ‘mostSignificiant’ would be 'F' and and ‘leastSignificant’ would be 'E'. +func SymbolUpperCase(value byte) rune { + const table string = "0123456789ABCDEF" + + var index int = int(value) % len(table) + + var result rune = rune(table[index]) + + return result +} diff --git a/symbols_test.go b/symbols_test.go new file mode 100644 index 0000000..13fa4d7 --- /dev/null +++ b/symbols_test.go @@ -0,0 +1,271 @@ +package hexadeca_test + +import ( + "testing" + + "sourcecode.social/reiver/go-hexadeca" +) + +func TestSymbolLowerCase(t *testing.T) { + + tests := []struct{ + Value byte + Expected rune + }{ + { + Value: 0, + Expected: '0', + }, + { + Value: 1, + Expected: '1', + }, + { + Value: 2, + Expected: '2', + }, + { + Value: 3, + Expected: '3', + }, + { + Value: 4, + Expected: '4', + }, + { + Value: 5, + Expected: '5', + }, + { + Value: 6, + Expected: '6', + }, + { + Value: 7, + Expected: '7', + }, + { + Value: 8, + Expected: '8', + }, + { + Value: 9, + Expected: '9', + }, + { + Value: 10, + Expected: 'a', + }, + { + Value: 11, + Expected: 'b', + }, + { + Value: 12, + Expected: 'c', + }, + { + Value: 13, + Expected: 'd', + }, + { + Value: 14, + Expected: 'e', + }, + { + Value: 15, + Expected: 'f', + }, + } + + for testNumber, test := range tests { + + actual := hexadeca.SymbolLowerCase(test.Value) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %q (%d)", expected, expected) + t.Logf("ACTUAL: %q (%d)", actual, actual) + t.Logf("VALUE: %x (%d)", test.Value, test.Value) + continue + } + + } +} + +func TestSymbolPersian(t *testing.T) { + + tests := []struct{ + Value byte + Expected rune + }{ + { + Value: 0, + Expected: '۰', + }, + { + Value: 1, + Expected: '۱', + }, + { + Value: 2, + Expected: '۲', + }, + { + Value: 3, + Expected: '۳', + }, + { + Value: 4, + Expected: '۴', + }, + { + Value: 5, + Expected: '۵', + }, + { + Value: 6, + Expected: '۶', + }, + { + Value: 7, + Expected: '۷', + }, + { + Value: 8, + Expected: '۸', + }, + { + Value: 9, + Expected: '۹', + }, + { + Value: 10, + Expected: 'ا', + }, + { + Value: 11, + Expected: 'ب', + }, + { + Value: 12, + Expected: 'پ', + }, + { + Value: 13, + Expected: 'ت', + }, + { + Value: 14, + Expected: 'ث', + }, + { + Value: 15, + Expected: 'ج', + }, + } + + for testNumber, test := range tests { + + actual := hexadeca.SymbolPersian(test.Value) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %q (%d)", expected, expected) + t.Logf("ACTUAL: %q (%d)", actual, actual) + t.Logf("VALUE: %x (%d)", test.Value, test.Value) + continue + } + + } +} + +func TestSymbolUpperCase(t *testing.T) { + + tests := []struct{ + Value byte + Expected rune + }{ + { + Value: 0, + Expected: '0', + }, + { + Value: 1, + Expected: '1', + }, + { + Value: 2, + Expected: '2', + }, + { + Value: 3, + Expected: '3', + }, + { + Value: 4, + Expected: '4', + }, + { + Value: 5, + Expected: '5', + }, + { + Value: 6, + Expected: '6', + }, + { + Value: 7, + Expected: '7', + }, + { + Value: 8, + Expected: '8', + }, + { + Value: 9, + Expected: '9', + }, + { + Value: 10, + Expected: 'A', + }, + { + Value: 11, + Expected: 'B', + }, + { + Value: 12, + Expected: 'C', + }, + { + Value: 13, + Expected: 'D', + }, + { + Value: 14, + Expected: 'E', + }, + { + Value: 15, + Expected: 'F', + }, + } + + for testNumber, test := range tests { + + actual := hexadeca.SymbolUpperCase(test.Value) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %q (%d)", expected, expected) + t.Logf("ACTUAL: %q (%d)", actual, actual) + t.Logf("VALUE: %x (%d)", test.Value, test.Value) + continue + } + + } +}