diff --git a/.gitignore b/.gitignore index f3e6490..85249d3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ /Python/Abstracts/AnPMap.py /Python/Abstracts/Applications.py .sass-cache -__pycache__ \ No newline at end of file +__pycache__ +/CSharp/obj +/CSharp/bin \ No newline at end of file diff --git a/CSharp/ErrorsManager.cs b/CSharp/ErrorsManager.cs index c783236..e7aa567 100644 --- a/CSharp/ErrorsManager.cs +++ b/CSharp/ErrorsManager.cs @@ -1,40 +1,45 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; -using System.Net.Mail; using System.Text.RegularExpressions; namespace ErrorsManager{ - // public abstract record Error{ - - // private Error(){} - - // public sealed record String(string value):Error; - // public sealed record Array(byte[] value):Error; - // public sealed record Integer(int value):Error; - - // } - - class ErrorsManager{ + public class ErrorsManager{ public static readonly char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".ToCharArray(); + public static readonly string[] ERRORS_MESSAGES = new string[]{ + "invalid_alphabet", + "invalid_base", + "invalid_alphabet_type", + "too_short_alphabet", + "repeated_characters_in_alphabet", + "too_long_alphabet", + "base_lower_than_2", + "base_greater_than_128", + "base_greater_than_alphabet" + }; + public static readonly Regex RE_KEY = new Regex(@"^[a-z_][a-z0-9_]*$", RegexOptions.IgnoreCase | RegexOptions.Compiled); - private Error error = 0; - private char[] alphabet; + private int error = 0; + private char[] alphabet = new char[0]; private Dictionary dictionary = new Dictionary(); private byte _base; private byte power; - public ErrorsManager(object? inputs = null){ + public ErrorsManager(object inputs = null){ - set_alphabet(get("alphabet", inputs, ALPHABET)); + set_alphabet( + get("alphabet", inputs, ALPHABET), + get("base", inputs, 64) + ); } - public int set_alphabet(object? alphabet = null){ + public int set_alphabet(object alphabet = null, int _base = 64){ int original_length; @@ -63,10 +68,19 @@ namespace ErrorsManager{ error |= 1 << 4; if(this.alphabet.Length > 128) error |= 1 << 5; + + error |= ( + _base < 2 ? 1 << 0 : + _base > 128 ? 1 << 1 : + _base >= this.alphabet.Length ? 1 << 2 : + 0) << 6; + + if(error >> 6 == 0) + this.alphabet = this.alphabet.Take(_base).ToArray(); this.alphabet = this.alphabet.Take( - _base = (byte)Math.Pow(2, - power = (byte)Math.Log2(this.alphabet.Length) + this._base = (byte)Math.Pow(2, + power = (byte)Math.Log2(this.alphabet.Take(128).Count()) ) ).ToArray(); @@ -81,73 +95,96 @@ namespace ErrorsManager{ return new string(alphabet); } - public byte[] to_array(object code){ - if(code is string _string) - return _string.Select(character => dictionary[character]).ToArray(); - if(code is byte[] array) - return array; - if(code is int integer){ - - List hexas = new List(); - - while(integer != 0){ - hexas.Add((byte)(integer % _base)); - integer /= _base; - } - - return hexas.ToArray(); - } - return new byte[]{}; + public byte[] to_array(string code){ + return code.Select(character => dictionary[character]).ToArray(); } - public string to_string(object code){ - if(code is string _string) - return _string; - if(code is byte[] array) - return new string(array.Select(value => alphabet[value]).ToArray()); - if(code is int integer){ - - string hexas = new string(); - - while(integer != 0){ - hexas += alphabet[(byte)(integer % _base)]; - integer /= _base; - } - - return hexas; - } - return ""; + public byte[] to_array(byte[] code){ + return code; } - public int to_integer(object code){ - if(code is string _string){ - - int integer = 0; + public byte[] to_array(int code){ - for(byte i = 0; i < _string.Length; i ++) - integer = integer * _base + dictionary[_string[i]]; + List hexas = new List(); - return integer; + while(code != 0){ + hexas.Add((byte)(code % _base)); + code /= _base; } - if(code is byte[] array){ - - int integer = 0; - for(byte i = 0; i < array.Length; i ++) - integer = integer * _base + array[i]; - - return integer; - } - if(code is int integer) - return integer; - return 0; + return hexas.ToArray(); } - public (int, string)[] process(Error code, IEnumerable messages){ + public string to_string(byte[] code){ + return new string(code.Select(value => alphabet[value]).ToArray()); + } + + public string to_string(int code){ + string hexas = ""; + + while(code != 0){ + hexas += alphabet[(byte)(code % _base)]; + code /= _base; + } + + return hexas; + } + + public string to_string(string code){ + return code; + } + + public int to_integer(string code){ + + int _string = 0; + + for(int i = code.Length - 1; i >= 0; i --) + _string = _string * _base + dictionary[code[i]]; + + return _string; + } + + public int to_integer(byte[] code){ + + int array = 0; + + for(int i = code.Length - 1; i >= 0; i --) + array = array * _base + code[i]; + + return array; + } + + public int to_integer(int code){ + return code; + } + + public string to_string_binary(string code){ + return string.Join("", code.Reverse().Select(character => Convert.ToString(dictionary[character], 2).PadLeft(power, '0'))); + } + + public string to_string_binary(byte[] code){ + return string.Join("", code.Reverse().Select(hexa => Convert.ToString(hexa, 2).PadLeft(power, '0'))); + } + + public string to_string_binary(int code){ + + string binary = Convert.ToString(code, 2); + int remainer = binary.Length % power; + + return ( + remainer == 0 ? binary : + binary.PadLeft(binary.Length + power - remainer, '0')); + } + + public (int, string)[] process(string code, IEnumerable messages){ + return process(to_array(code), messages); + } + + public (int, string)[] process(byte[] code, IEnumerable messages){ List<(int, string)> response = new List<(int, string)>(); - for_each_enumerate(get_hexas_from(code), (hexa, i) => { + for_each_enumerate(code, (hexa, i) => { for(byte j = 0; j < power && (hexa & 1 << j) <= hexa; j ++) if((hexa & 1 << j) != 0){ @@ -161,81 +198,112 @@ namespace ErrorsManager{ return response.ToArray(); } - public int get_bits(object code){ - if(code is string _string) - return _string.Length == 0 ? 0 : (_string.Length - 1) * power + (int)Math.Ceiling(Math.Log2(dictionary[_string.Last()] + 1)); - if(code is byte[] array) - return array.Length == 0 ? 0 : (array.Length - 1) * power + (int)Math.Ceiling(Math.Log2(array.Last() + 1)); - if(code is int integer) - return (int)Math.Ceiling(Math.Log2(integer + 1)); - return 0; + public (int, string)[] process(int code, IEnumerable messages){ + return process(to_array(code), messages); } - public T bitwise(T code, int bits){ - if(code is string _string) - return (T)(object)to_string(bitwise(to_array(_string), bits)); - if(code is byte[] array){ + public int get_bits(string code){ + return code.Length == 0 ? 0 : (code.Length - 1) * power + (int)Math.Ceiling(Math.Log2(dictionary[code.Last()] + 1)); + } - byte shift; - int mask; - List hexas; + public int get_bits(byte[] code){ + return code.Length == 0 ? 0 : (code.Length - 1) * power + (int)Math.Ceiling(Math.Log2(code.Last() + 1)); + } - if(bits < 0){ - if(array.Length < (int)((bits *= -1) / power)) - return (T)(object)new byte[]{0}; - - shift = (byte)(bits % power); - mask = _base - 1; - hexas = array.ToList(); + public int get_bits(int code){ + return (int)Math.Ceiling(Math.Log2(code + 1)); + } - hexas.RemoveRange(0, (int)(bits / power)); + public string clean(string code){ - if(shift != 0 && hexas.Count != 0){ + int l = code.Length; - int l = hexas.Count - 1; + while(l > 0 && code[l - 1] == alphabet[0]) + l --; - for(int i = 0; i < l; i ++) - hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask)); - hexas[hexas.Count - 1] >>= shift; + return ( + code.Length == l ? code : + l == 0 ? alphabet[0].ToString() : + code.Substring(0, l)); + } - } + public byte[] clean(byte[] code){ - return (T)(object)hexas.ToArray(); - }; - if(bits > 0){ + int l = code.Length; - shift = (byte)(bits % power); - mask = _base - 1; - hexas = array.ToList(); + while(l > 0 && code[l - 1] == 0) + l --; - if(shift != 0){ + return ( + l == code.Length ? code : + l == 0 ? new byte[]{0} : + code.Take(l).ToArray()); + } - int last_hexa = hexas[hexas.Count - 1] << shift; - - for(int i = hexas.Count - 1; i > 0; i --) - hexas[i] = (byte)((hexas[i] << shift) | ((hexas[i - 1] >> (power - shift)) & mask)); - hexas[0] = (byte)((hexas[0] << shift) & mask); - - if(last_hexa >= _base) - hexas.Add((byte)(last_hexa >> power)); - - } - for(int i = bits / power; i > 0; i --) - hexas.Insert(0, 0); - - return (T)(object)hexas.ToArray(); - } - return (T)(object)array; - } - if(code is int integer) - return (T)(object)( - bits > 0 ? integer << bits : - bits < 0 ? integer >> -bits : - integer); + public int clean(int code){ return code; } - public void get_from_bits(object code, ref int from, ref int bits){ + public string bitwise(string code, int bits){ + return to_string(bitwise(to_array(code), bits)); + } + public byte[] bitwise(byte[] code, int bits){ + if(code.Length == 0 || bits == 0) + return code; + + byte shift = (byte)(Math.Abs(bits) % power); + int mask = _base - 1; + List hexas = code.ToList(); + + if(bits < 0){ + + hexas.RemoveRange(0, (int)(-bits / power)); + + if(shift != 0 && hexas.Count != 0){ + + int l = hexas.Count - 1; + + for(int i = 0; i < l; i ++) + hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask)); + hexas[hexas.Count - 1] >>= shift; + + } + + }else{ + + if(shift != 0){ + + int last_hexa = hexas[hexas.Count - 1] << shift; + + for(int i = hexas.Count - 1; i > 0; i --) + hexas[i] = (byte)(((hexas[i] << shift) & mask) | (hexas[i - 1] >> (power - shift))); + hexas[0] = (byte)((hexas[0] << shift) & mask); + + if(last_hexa >= _base) + hexas.Add((byte)(last_hexa >> power)); + + } + + for(int i = bits / power; i > 0; i --) + hexas.Insert(0, 0); + + } + + return clean(hexas.ToArray()); + } + + public int bitwise(int code, int bits){ + return ( + bits > 0 ? code << bits : + bits < 0 ? code >> -bits : + code); + } + + public void get_from_bits(string code, ref int from, ref int bits){ + get_from_bits(to_array(code), ref from, ref bits); + } + + public void get_from_bits(byte[] code, ref int from, ref int bits){ if(from < 0){ from = get_bits(code) + from; if(from < 0) @@ -251,118 +319,214 @@ namespace ErrorsManager{ } } - public T reset(T code, int from, int bits = 0, bool reversed = false){ - if(bits == 0 || (from == 0 && bits < 0)) - return code; - if(code is string _string) - return (T)(object)to_string(reset(to_array(_string), from, bits, reversed)); - if(code is byte[] array){ + public void get_from_bits(int code, ref int from, ref int bits){ + get_from_bits(to_array(code), ref from, ref bits); + } - List hexas = array.ToList(); - int hexa_from; - int hexa_to; - int l; - byte from_mask; - byte to_mask; + public string reset(string code, int from, int bits = 0, bool reversed = false){ + return to_string(reset(to_array(code), from, bits, reversed)); + } - get_from_bits(array, ref from, ref bits); - hexa_from = (int)(from / power); - hexa_to = (int)((from + bits) / power); + public byte[] reset(byte[] code, int from, int bits = 0, bool reversed = false){ - if(reversed){ + List hexas = code.ToList(); + int hexa_from; + int hexa_to; + int l; + byte from_mask; + byte to_mask; - l = from % power; - from_mask = (byte)(~-(1 << power - l) << l); - to_mask = (byte)~-(1 << (from + bits) % power); + get_from_bits(code, ref from, ref bits); + hexa_from = (int)(from / power); + hexa_to = (int)((from + bits) / power); - for(int i = 0; i < hexas.Count; i ++) - if(i < hexa_from || i > hexa_to) - hexas[i] = 0; + if(reversed){ - if(hexa_from == hexa_to) - hexas[hexa_to] &= (byte)(from_mask & to_mask); - else{ - hexas[hexa_from] &= from_mask; - if(hexa_to < hexas.Count) - hexas[hexa_to] &= to_mask; - } + l = from % power; + from_mask = (byte)(~-(1 << power - l) << l); + to_mask = (byte)~-(1 << (from + bits) % power); + for(int i = 0; i < hexas.Count; i ++) + if(i < hexa_from || i > hexa_to) + hexas[i] = 0; + + if(hexa_from == hexa_to) + hexas[hexa_to] &= (byte)(from_mask & to_mask); + else{ + hexas[hexa_from] &= from_mask; + if(hexa_to < hexas.Count) + hexas[hexa_to] &= to_mask; + } + + }else{ + + l = (from + bits) % power; + from_mask = (byte)~-(1 << (from % power)); + to_mask = (byte)(~-(1 << power - l) << l); + + if(hexa_from == hexa_to){ + hexas[hexa_to] &= (byte)(from_mask | to_mask); }else{ - - l = (from + bits) % power; - from_mask = (byte)~-(1 << (from % power)); - to_mask = (byte)(~-(1 << power - l) << l); - - if(hexa_from == hexa_to){ - hexas[hexa_to] &= (byte)(from_mask | to_mask); - }else{ - hexas[hexa_from] &= from_mask; - for(int i = hexa_from + 1; i < hexa_to && i < hexas.Count; i ++) - hexas[i] = 0; - if(hexa_to < hexas.Count) - hexas[hexa_to] &= to_mask; - } + hexas[hexa_from] &= from_mask; + for(int i = hexa_from + 1; i < hexa_to && i < hexas.Count; i ++) + hexas[i] = 0; + if(hexa_to < hexas.Count) + hexas[hexa_to] &= to_mask; } - - return (T)(object)hexas.ToArray(); } - if(code is int integer){ - get_from_bits(integer, ref from, ref bits); - - return (T)(object)(integer & (reversed ? - ~-(1 << bits) << from : - (~-(1 << get_bits(integer)) << from + bits) | ~-(1 << from))); - } - return code; + return hexas.ToArray(); } - public T get_range(T code, int from, int length = 0){ - if(code is string _string) - return to_string(get_range(to_array(_string), length)); - if(code is byte[] array){ + public int reset(int code, int from, int bits = 0, bool reversed = false){ - List hexas = new List(); + get_from_bits(code, ref from, ref bits); + if(from + bits > 31) + bits = 31 - from; - if(from > 0){ + return code & (reversed ? + ~-(1 << bits) << from : + (~-(1 << get_bits(code)) << from + bits) | ~-(1 << from)); - byte shift = (byte)(from % power); - int mask = ~-_base; + } - hexas.AddRange(array.Take(from / power)); - if(shift != 0){ - for(int i = hexas.Count - 2; i >= 0; i --) - hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask)); - hexas[hexas.Count - 1] = (byte)(hexas.Last() >> shift); - } + public string get_range(string code, int from, int bits = 0){ + return to_string(get_range(to_array(code), from, bits)); + } - } - if(length > 0){ + public byte[] get_range(byte[] code, int from, int bits = 0){ - byte shift = (byte)(length % power); + List hexas; - hexas.Take((int)Math.Ceiling(hexas.Count / (double)power)); - if(shift != 0) - hexas[hexas.Count - 1] = (byte)(hexas.Last() & ~-(1 << shift)); + get_from_bits(code, ref from, ref bits); + + if(bits == 0) + bits = get_bits(code) - from; + if(bits <= 0) + return new byte[]{0}; + + hexas = code.ToList(); + + if(from > 0){ + + byte shift = (byte)(from % power); + int mask = ~-_base; + + hexas = hexas.Skip(from / power).ToList(); + if(shift != 0 && hexas.Count > 0){ + + int l = hexas.Count - 1; + for(int i = 0; i < l; i ++) + hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask)); + hexas[hexas.Count - 1] >>= shift; } - return to_integer(array.Skip(from / power).Take((int)Math.Ceiling((length != 0 ? length : get_bits(array) - from) / (double)power)).ToArray()); } - if(code is int integer) - return (from > 0 ? integer >> from : integer) & (length > 0 ? ~(-1 << length) : integer); - return code; + if(bits > 0){ + + byte shift = (byte)(bits % power); + + hexas = hexas.Take((int)Math.Ceiling(bits / (double)power)).ToList(); + if(shift != 0 && hexas.Count > 0) + hexas[hexas.Count - 1] &= (byte)((1 << shift) - 1); + + } + + return clean(hexas.ToArray()); } - public bool has(Error code, int? bits = null){ - return code switch{ - Error.String _string => _string.value.ToList().Take(get_hexas_from_bits(bits)).Length * power > bits, - Error.Array array => array.value.ToList().Take(Math.Ceiling(Math.Log2(bits))).Where(hexa => hexa != 0).Count() != 0, - Error.Integer integer => (bits != null && bits != 0 ? ~-(1 << bits) & integer.value : integer.value) != 0, - _ => false - }; + public int get_range(int code, int from, int bits = 0){ + + get_from_bits(code, ref from, ref bits); + + if(from > 0) + code = (code >> from) & ((1 << (31 - from)) - 1); + + if(bits <= 0 || bits >= 31) + return code; + + if(from + bits > 31) + bits = 31 - from; + + return code & ((1 << bits) - 1); } - public static List get_keys(object? items){ + public bool has(string code, int from = 0, int bits = 0){ + foreach(char character in get_range(code, from, bits)) + if(character != alphabet[0]) + return true; + return false; + } + + public bool has(byte[] code, int from = 0, int bits = 0){ + foreach(byte hexa in get_range(code, from, bits)) + if(hexa != 0) + return true; + return false; + } + + public bool has(int code, int from = 0, int bits = 0){ + return get_range(code, from, bits) != 0; + } + + public string set(string error, string code, int _from = 0, int bits = 0){ + return to_string(set(to_array(error), to_array(code), _from, bits)); + } + + public string set(string error, byte[] code, int _from = 0, int bits = 0){ + return to_string(set(to_array(error), code, _from, bits)); + } + + public string set(string error, int code, int _from = 0, int bits = 0){ + return to_string(set(to_array(error), to_array(code), _from, bits)); + } + + public byte[] set(byte[] error, string code, int _from = 0, int bits = 0){ + return set(error, to_array(code), _from, bits); + } + + public byte[] set(byte[] error, byte[] code, int _from = 0, int bits = 0){ + + int l; + int m = error.Length; + int n = code.Length; + List results = new List(); + + if(bits != 0) + error = reset(error, _from, bits); + if(_from != 0) + n = (code = bitwise(code, _from)).Length; + l = m > n ? m : n; + + for(int i = 0; i < l; i ++) + results.Add((byte)( + (i < m ? error[i] : (byte)0) | (i < n ? code[i] : (byte)0) + )); + + return clean(results.ToArray()); + } + + public byte[] set(byte[] error, int code, int _from = 0, int bits = 0){ + return set(error, to_array(code), _from, bits); + } + + public int set(int error, string code, int _from = 0, int bits = 0){ + return to_integer(set(error, to_integer(code), _from, bits)); + } + + public int set(int error, byte[] code, int _from = 0, int bits = 0){ + return to_integer(set(error, to_integer(code), _from, bits)); + } + + public int set(int error, int code, int _from = 0, int bits = 0){ + if(bits != 0) + error = reset(error, _from, bits); + if(_from != 0) + code = bitwise(code, _from); + return error | code; + } + + public static List get_keys(object items){ List keys = new List(); @@ -373,8 +537,8 @@ namespace ErrorsManager{ foreach(string item_i in strings) if(!keys.Contains(item_i) && RE_KEY.IsMatch(item_i)) keys.Add(item_i); - }else if(items is IEnumerable list) - foreach(object? item in list){ + }else if(items is IEnumerable list) + foreach(object item in list){ if(item == null) continue; if(item is string string_item){ @@ -389,27 +553,27 @@ namespace ErrorsManager{ return keys; } - public static List> get_dictionaries(object? items){ + public static List> get_dictionaries(object items){ - List> dictionaries = new List>(); + List> dictionaries = new List>(); - if(items is Dictionary dictionary) + if(items is Dictionary dictionary) dictionaries.Add(dictionary); - else if(items is IEnumerable list) - foreach(object? item in list) + else if(items is IEnumerable list) + foreach(object item in list) dictionaries.AddRange(get_dictionaries(item)); return dictionaries; } - public static T? get(object keys, object? dictionaries, T? _default = default(T?)){ + public static T get(object keys, object dictionaries, T _default = default(T)){ List keys_list = get_keys(keys); if(keys_list.Count != 0) - foreach(Dictionary dictionary in get_dictionaries(dictionaries)) + foreach(Dictionary dictionary in get_dictionaries(dictionaries)) foreach(string key in keys_list) - if(dictionary.TryGetValue(key, out object? value) && value is T typed) + if(dictionary.TryGetValue(key, out object value) && value is T typed) return typed; return _default; } diff --git a/CSharp/ErrorsManager.csproj b/CSharp/ErrorsManager.csproj index 41964d3..b77cc88 100755 --- a/CSharp/ErrorsManager.csproj +++ b/CSharp/ErrorsManager.csproj @@ -1,23 +1,17 @@ Exe + net10.0;net462 - enable + enable - latest - AnP - AnP + ErrorsManager + ErrorsManager + false - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - + + + \ No newline at end of file diff --git a/CSharp/ErrorsManager.slnx b/CSharp/ErrorsManager.slnx new file mode 100644 index 0000000..ecad6ca --- /dev/null +++ b/CSharp/ErrorsManager.slnx @@ -0,0 +1,3 @@ + + + diff --git a/CSharp/Program.cs b/CSharp/Program.cs index 777ff2d..d258702 100755 --- a/CSharp/Program.cs +++ b/CSharp/Program.cs @@ -2,12 +2,17 @@ using System; namespace ErrorsManager{ class Program{ + public static void Main(string[] args){ - static void Main(string[] args){ - - Console.WriteLine("Hello World!"); + // Tests.errors(); + // Tests.conversions(); + // Tests.alphabet(); + // Tests.bitwise(); + // Tests.bitwise_sucesive(); + // Tests.bits(); + // Tests.reset(); + Tests.ranges(); } - } } \ No newline at end of file diff --git a/CSharp/Tests.cs b/CSharp/Tests.cs new file mode 100644 index 0000000..8ce62db --- /dev/null +++ b/CSharp/Tests.cs @@ -0,0 +1,241 @@ +using System; + +namespace ErrorsManager{ + + class FullError{ + + public int integer; + public string _string; + public byte[] array; + + public FullError(ErrorsManager errors, int integer){ + this.integer = integer; + _string = errors.to_string(integer); + array = errors.to_array(integer); + } + + public FullError(ErrorsManager errors, string _string){ + integer = errors.to_integer(_string); + this._string = _string; + array = errors.to_array(_string); + } + + public FullError(ErrorsManager errors, byte[] array){ + integer = errors.to_integer(array); + _string = errors.to_string(array); + this.array = array; + } + + public static string print(byte[] array){ + return "[" + string.Join(", ", array) + "]"; + } + + } + + class Tests{ + + public static void errors(){ + + ErrorsManager errors = new ErrorsManager(); + FullError error = new FullError(errors, 217934237); + FullError reset = new FullError(errors, 0); + + reset.integer = errors.reset(error.integer, -5, 12); + reset._string = errors.reset(error._string, -5, 12); + reset.array = errors.reset(error.array, -5, 12); + + Console.WriteLine($"RESET: from {-5} bits {12}"); + Console.WriteLine($"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(reset.integer)}"); + Console.WriteLine($"STRING: {errors.to_string_binary(error._string)} - {errors.to_string_binary(reset._string)}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(reset.array)}"); + Console.WriteLine(); + + } + + public static ErrorsManager conversions(int tests = 10, object inputs = null){ + + ErrorsManager errors = new ErrorsManager(inputs); + Random seed = new Random(); + + for(int i = 0; i < tests; i ++){ + + FullError error = new FullError(errors, seed.Next(0, 1 << 16)); + + Console.WriteLine($"INTEGER: {errors.to_integer(error.integer)}, {errors.to_integer(error._string)}, {errors.to_integer(error.array)}"); + Console.WriteLine($"STRING: {errors.to_string(error.integer)}, {errors.to_string(error._string)}, {errors.to_string(error.array)}"); + Console.WriteLine($"ARRAY: {FullError.print(errors.to_array(error.integer))}, {FullError.print(errors.to_array(error._string))}, {FullError.print(errors.to_array(error.array))}"); + Console.WriteLine(); + + } + + return errors; + } + + public static void alphabet(int tests = 10){ + + Random seed = new Random(); + + for(int i = 0; i < tests; i ++){ + + ErrorsManager errors = conversions(1, new Dictionary() { + {"base", seed.Next(2, 128)} + }); + + Console.WriteLine($"^^^ ALPHABET: {string.Join(", ", errors.get_alphabet())} ^^^"); + Console.WriteLine(); + + } + } + + public static void bitwise(int tests = 10){ + + ErrorsManager errors = new ErrorsManager(); + Random seed = new Random(); + + for(int i = 0; i < tests; i ++){ + + FullError error = new FullError(errors, seed.Next(0, 1 << seed.Next(0, 16))); + FullError shifted = new FullError(errors, 0); + FullError unshifted = new FullError(errors, 0); + int bitwise = 10 - seed.Next(0, 20); + + Console.WriteLine($"BITWISE: {bitwise}"); + + shifted.integer = errors.bitwise(error.integer, bitwise); + shifted._string = errors.bitwise(error._string, bitwise); + shifted.array = errors.bitwise(error.array, bitwise); + unshifted.integer = errors.bitwise(shifted.integer, -bitwise); + unshifted._string = errors.bitwise(shifted._string, -bitwise); + unshifted.array = errors.bitwise(shifted.array, -bitwise); + + Console.WriteLine($"INTEGER: {errors.to_string_binary(error.integer)} - {error.integer}"); + Console.WriteLine($"STRING: {errors.to_string_binary(error._string)} - {error._string}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(error.array)} - {FullError.print(error.array)}"); + Console.WriteLine($"INTEGER: {errors.to_string_binary(shifted.integer)} - {shifted.integer}"); + Console.WriteLine($"STRING: {errors.to_string_binary(shifted._string)} - {shifted._string}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}"); + Console.WriteLine($"INTEGER: {errors.to_string_binary(unshifted.integer)} - {unshifted.integer}"); + Console.WriteLine($"STRING: {errors.to_string_binary(unshifted._string)} - {unshifted._string}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(unshifted.array)} - {FullError.print(unshifted.array)}"); + Console.WriteLine(); + + } + + } + + public static void bitwise_sucesive(int tests = 10){ + + ErrorsManager errors = new ErrorsManager(); + FullError error = new FullError(errors, new Random().Next(0, 1 << 16)); + + Console.WriteLine($"INTEGER: {errors.to_string_binary(error.integer)} - {error.integer}"); + Console.WriteLine($"STRING: {errors.to_string_binary(error._string)} - {error._string}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(error.array)} - {FullError.print(error.array)}"); + Console.WriteLine(); + + for(int i = -tests; i < tests; i ++){ + + FullError shifted = new FullError(errors, 0); + + Console.WriteLine($"BITWISE: {i}"); + + shifted.integer = errors.bitwise(error.integer, i); + shifted._string = errors.bitwise(error._string, i); + shifted.array = errors.bitwise(error.array, i); + + + Console.WriteLine($"INTEGER: {errors.to_string_binary(shifted.integer)} - {shifted.integer}"); + Console.WriteLine($"STRING: {errors.to_string_binary(shifted._string)} - {shifted._string}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}"); + Console.WriteLine(); + + } + + } + + public static void bits(int tests = 10){ + + ErrorsManager errors = new ErrorsManager(); + Random seed = new Random(); + + for(int i = 0; i < tests; i++){ + + FullError error = new FullError(errors, seed.Next(0, 1 << seed.Next(0, 28))); + int from_value = seed.Next(-15, 15); + int bits_value = seed.Next(-13, 13); + int[] from = new int[]{from_value, from_value, from_value}; + int[] bits = new int[]{bits_value, bits_value, bits_value}; + + errors.get_from_bits(error.integer, ref from[0], ref bits[0]); + errors.get_from_bits(error._string, ref from[1], ref bits[1]); + errors.get_from_bits(error.array, ref from[2], ref bits[2]); + + + Console.WriteLine($"CODE: {error.integer} - {error._string} - {FullError.print(error.array)}"); + Console.WriteLine($"ERROR: {errors.to_string_binary(error.integer)} - {errors.get_bits(error.integer)} - {errors.get_bits(error._string)} - {errors.get_bits(error.array)}"); + Console.WriteLine($"FROM: {from_value} - {from[0]}, {from[1]}, {from[2]}"); + Console.WriteLine($"BITS: {bits_value} - {bits[0]}, {bits[1]}, {bits[2]}"); + Console.WriteLine(); + + } + + } + + public static void reset(int tests = 10){ + + ErrorsManager errors = new ErrorsManager(); + Random seed = new Random(); + + for(int i = 0; i < tests; i ++){ + + FullError error = new FullError(errors, seed.Next(0, 1 << 28)); + int from = seed.Next(-15, 15); + int bits = seed.Next(-13, 13); + FullError reset = new FullError(errors, 0); + + reset.integer = errors.reset(error.integer, from, bits); + reset._string = errors.reset(error._string, from, bits); + reset.array = errors.reset(error.array, from, bits); + + Console.WriteLine($"RESET: from {from} bits {bits}"); + Console.WriteLine($"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(reset.integer)}"); + Console.WriteLine($"STRING: {errors.to_string_binary(error._string)} - {errors.to_string_binary(reset._string)}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(reset.array)}"); + Console.WriteLine(); + + } + + } + + public static void ranges(int tests = 10){ + + ErrorsManager errors = new ErrorsManager(); + Random seed = new Random(); + + for(int i = 0; i < tests; i ++){ + + FullError error = new FullError(errors, seed.Next(0, 1 << 28)); + int from = seed.Next(-15, 15); + int bits = seed.Next(-13, 13); + FullError range = new FullError(errors, 0); + + Console.WriteLine($"RANGE: from {from} bits {bits}"); + errors.get_from_bits(error._string, ref from, ref bits); + Console.WriteLine($"REAL: from {from} bits {bits}"); + + range.integer = errors.get_range(error.integer, from, bits); + range._string = errors.get_range(error._string, from, bits); + range.array = errors.get_range(error.array, from, bits); + + Console.WriteLine($"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(range.integer)}"); + Console.WriteLine($"STRING: {errors.to_string_binary(error._string)} - {errors.to_string_binary(range._string)}"); + Console.WriteLine($"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(range.array)}"); + Console.WriteLine(); + + } + + } + + } + +} \ No newline at end of file diff --git a/README.md b/README.md index c23fee9..0062c9e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,190 @@ # ErrorsManager -Errors codes manager for binary errors system, unlocking the 32 bits limit. \ No newline at end of file +**ErrorsManager** es una librería que nos permite hacer una gestión de errores mediante códigos de error en base binaria o agrupación de base numérica. La idea es poder trasladar un análisis global de errores entre plataformas de forma simple con el condicionante que ambas plataformas han de conocer estructuralmente el sistema de errores. El propósito de dicho sistema es conseguir un sistema unificado de errores que permita el envío compacto de los mismos evitando un uso innecesario de red. + +# Funcionamiento + +La idea es crear un objeto `ErrorsManager` para gestionar los errores dentro del código y hacer uso de `set`, `has` y `process` para gestionar los errores, aunque de por sí tenga más funcionalidades. + +> **NOTA**: Este manual ordenará los métodos y estructuras conforme su utilidad e importancia fuera del ámbito de la propia librería para uso general. + +## Constructor + +`errors_manager:ErrorsManager = new ErrorsManager(inputs:dict[str, any|null])` + +- **alphabet**: Alfabeto String o Array de Caracteres. +- **base**: Entero que representa la máxima base 2 numérica que se usará: 16, 32 o 64, por ejemplo. + +El alfabeto no puede contener caracteres iguales, y si los contiene, se encargará de eliminarlos, reduciendo la posibilidad de base. + +## set + +`error:int = errors_manager.set(from:int, code:int|list[byte]|str, from:int = 0, bits:int = 0)` + +`error:list[byte]= errors_manager.set(from:list[byte], code:int|list[byte]|str, from:int = 0, bits:int = 0)` + +`error:str = errors_manager.set(from:str, code:int|list[byte]|str, from:int = 0, bits:int = 0)` + +1. **error**: Código de error original. Puede ser un String, Array de enteros que representan los Hexas o un entero. +2. **code**: Código de error a establecer. Puede ser un String, Array de enteros que representan los Hexas o un entero. +3. **from**: Determina si se quiere desplazar `code`. 0 indica que no. +4. **bits**: Determina si se pasan a 0 los bits que de `error` desde `from`. 0 indica que no. + +El `from` negativo indica que se cuente desde el final, y el `bits` negativo que se cuente para atrás. Esto se establece desde `ErrorsManager.get_from_bits`. + +Retorna el resultado de la unión de `error` con `code` a partir de las posibles modificaciones de `from` y `bits`. + +## has + +`has:bool = errors_manager.has(code:int|list[byte]|str)` + +Determina si un código de error contiene errores o no. + +## process + +`errors_messages:list[tuple[int, str]] = errors_manager.process(code:int|list[byte]|str, messages:list[str] = [ ... ])` + +Permite recoger el conjunto de mensajes conforme al código de error. Cada mensaje irá acompañado de su Bit de posición, de ahí que retorne una lista de tuplas de un valor entero, que es el Bit de posición; y el String, que es el mensaje en cuestión. + +`[position, message for position:int, message:str in errors_messages]` + +Si retorna una lista vacía es que no hay errores. + +# .NET + +- Docker Hub del SDK: https://hub.docker.com/r/microsoft/dotnet-sdk +- Git del SDK: https://github.com/dotnet/sdk + +Para crear los SLN de la Solución: + +```sh +#!/bin/bash + +docker exec -it anp-dotnet bash + +cd CSharp +dotnet new sln -n ErrorsManager +dotnet sln add ErrorsManager.csproj +cd .. + +exit + +sudo chown -R root:$USER CSharp/ErrorsManager.slnx + +``` + +Luego hay que pelar el archivo de definición de proyecto `ErrorsManager.csproj`: + +```xml + + + Exe + + net10.0;net462 + enable + enable + ErrorsManager + ErrorsManager + false + + + + + + + +``` + +Instalación del SDK para desarrollo VSCode. + +```sh +#!/bin/bash +wget https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb +sudo dpkg -i packages-microsoft-prod.deb +rm packages-microsoft-prod.deb +sudo apt update +sudo apt install -y dotnet-sdk-10.0 +``` + +Luego, instalar el Pluggin Nuget de Visual Studio Code `C/C++ DevTools`. + +Para ejecutar un proyecto .NET desde Docker. + +```sh +#!/bin/bash +docker exec -it anp-dotnet dotnet run --project /workspace/CSharp/ErrorsManager.csproj -f net10.0 +``` + +Para compilar DLL primero configurar el archivo de definición de proyecto `ErrorsManager.csproj`. + +```xml + + + + Library + net10.0;net462 + enable + enable + ErrorsManager + ErrorsManager + false + + + + + + +``` + +Y luego ejecutar: + +```sh +#!/bin/bash +dotnet build ErrorsManager.csproj -c Release +``` + +# Objetivos + +Leyenda: + +- **Py**: Python +- **PHP** +- **JS**: JavaScript/ECMAScript +- **MSL**: SQLServer/TransactSQL +- **MyL**: MySQL/MariaDB +- **CS**: C#/CSharp +- **VB**: VisualBasic +- **Go**: Golang +- **RS**: Rust +- **C** +- **CPP**: C++ + +Tabla de objetivos: + +| Objetivo | Py | PHP | JS | MSL | MyS | CS | VB | Go | RS | C | CPP | +|-------------------|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----| +| Common base | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| set_alphabet | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| get_alphabet | [ ] | [ ] | [ ] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| to_array | [X] | [ ] | [X] | | | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| to_integer | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| to_string | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| to_string_binary | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| process | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| get_bits | [ ] | [ ] | [ ] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| bitwise | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| get_from_bits | [ ] | [ ] | [ ] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| reset | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| get_range | [ ] | [ ] | [ ] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| has | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| clean | [ ] | [ ] | [ ] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | +| set | [X] | [ ] | [X] | [ ] | [ ] | [X] | [ ] | [ ] | [ ] | [ ] | [ ] | + +> **NOTA**: Los Checkbox indican el estado siendo los siguientes: + +- [ ] Sin cubrir, está por hacerse. +- [-] Están en desarrollo actualmente. +- [X] Ya está hecho y es usable. + +> **NOTA**: Según lenguajes que tenga cierta rigidez, como es el caso de Golang, los nombres de los métodos y atributos pueden cambiar a Camel o Pascal. \ No newline at end of file diff --git a/version b/version index 340ff46..f477849 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.0.2.1 \ No newline at end of file +0.2.2 \ No newline at end of file