#wip: Building js, py, sql server and vb.
This commit is contained in:
parent
e1c0d98a68
commit
e7d8e3826c
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,3 +8,5 @@ __pycache__
|
||||
/CSharp/bin
|
||||
/VB/obj
|
||||
/VB/bin
|
||||
/Data
|
||||
/Public/data
|
||||
@ -379,7 +379,7 @@ namespace ErrorsManager{
|
||||
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[i] = (byte)(((hexas[i] << shift) | (hexas[i - 1] >> (power - shift))) & mask);
|
||||
hexas[0] = (byte)((hexas[0] << shift) & mask);
|
||||
|
||||
if(last_hexa >= _base)
|
||||
@ -430,7 +430,7 @@ namespace ErrorsManager{
|
||||
hexas[i] = 0;
|
||||
|
||||
if(hexa_from == hexa_to)
|
||||
hexas[hexa_to] &= (byte)(from_mask & to_mask);
|
||||
hexas[hexa_to] &= (byte)(from_mask | to_mask);
|
||||
else{
|
||||
hexas[hexa_from] &= from_mask;
|
||||
if(hexa_to < hexas.Count)
|
||||
@ -452,6 +452,7 @@ namespace ErrorsManager{
|
||||
if(hexa_to < hexas.Count)
|
||||
hexas[hexa_to] &= to_mask;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return clean(hexas.ToArray<byte>());
|
||||
@ -463,8 +464,8 @@ namespace ErrorsManager{
|
||||
if(from + bits > 31)
|
||||
bits = 31 - from;
|
||||
|
||||
return code & (reversed ?
|
||||
~-(1 << bits) << from :
|
||||
return code & (
|
||||
reversed ? ~-(1 << bits) << from :
|
||||
(~-(1 << get_bits(code)) << from + bits) | ~-(1 << from));
|
||||
}
|
||||
|
||||
@ -537,10 +538,12 @@ namespace ErrorsManager{
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -97,7 +97,7 @@ namespace ErrorsManager{
|
||||
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);
|
||||
int bitwise = seed.Next(-10, 10);
|
||||
|
||||
Console.WriteLine($"BITWISE: {bitwise}");
|
||||
|
||||
@ -282,7 +282,7 @@ namespace ErrorsManager{
|
||||
set_string.array = errors.set(error.array, value._string, from, bits);
|
||||
set_array.array = errors.set(error.array, value.array, from, bits);
|
||||
|
||||
Console.WriteLine($"SET: from {from} bits {bits} to {value}");
|
||||
Console.WriteLine($"SET: from {from} bits {bits} to [{value._string}, {FullError.print(value.array)}, {value.integer}]");
|
||||
Console.WriteLine($"ERROR: {errors.to_string_binary(error.integer)} - VALUE: {errors.to_string_binary(value.integer)}");
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(set_integer.integer)} - {errors.to_string_binary(set_integer._string)} - {errors.to_string_binary(set_integer.array)}");
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(set_string.integer)} - {errors.to_string_binary(set_string._string)} - {errors.to_string_binary(set_string.array)}");
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
434
Public/ecma/Tests.ecma.js
Normal file
434
Public/ecma/Tests.ecma.js
Normal file
@ -0,0 +1,434 @@
|
||||
"use strict";
|
||||
|
||||
import {ErrorsManager} from "./ErrorsManager.ecma.js";
|
||||
|
||||
/**
|
||||
* @typedef {import("./ErrorsManager.ecma.js").ErrorsManager} ErrorsManager
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class FullError
|
||||
* @constructor
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
export const FullError = (function(){
|
||||
|
||||
/**
|
||||
* @constructs FullError
|
||||
* @param {!ErrorsManager} errors
|
||||
* @param {!(string|Array.<number>|number)} code
|
||||
* @returns {void}
|
||||
* @access private
|
||||
* @static
|
||||
*/
|
||||
const FullError = function(errors, code){
|
||||
|
||||
/** @type {FullError} */
|
||||
const self = this;
|
||||
|
||||
/** @type {number} */
|
||||
this.integer = 0;
|
||||
/** @type {string} */
|
||||
this.string = "";
|
||||
/** @type {Array.<number>} */
|
||||
this.array = [];
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
*/
|
||||
const constructor = () => {
|
||||
if(ErrorsManager.is_string(code)){
|
||||
self.string = code;
|
||||
self.integer = errors.to_integer(code);
|
||||
self.array = errors.to_array(code);
|
||||
}else if(ErrorsManager.is_array(code)){
|
||||
self.array = code;
|
||||
self.integer = errors.to_integer(code);
|
||||
self.string = errors.to_string(code);
|
||||
}else if(ErrorsManager.is_integer(code)){
|
||||
self.integer = code;
|
||||
self.string = errors.to_string(code);
|
||||
self.array = errors.to_array(code);
|
||||
};
|
||||
};
|
||||
|
||||
constructor();
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {!Array.<number>} array
|
||||
* @returns {string}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
FullError.print = array => "[" + array.join(", ") + "]";
|
||||
|
||||
return FullError;
|
||||
})();
|
||||
|
||||
/**
|
||||
* @class Tests
|
||||
* @constructor
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
export const Tests = (function(){
|
||||
|
||||
/**
|
||||
* @constructs Tests
|
||||
* @returns {void}
|
||||
* @access private
|
||||
* @static
|
||||
*/
|
||||
const Tests = function(){};
|
||||
|
||||
/**
|
||||
* @param {!number} from
|
||||
* @param {!number} to
|
||||
* @returns {!number}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.random = (from, to) => Math.floor(Math.random() * (to - from + 1)) + from >> 0;
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.errors = () => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager(),
|
||||
/** @type {FullError} */
|
||||
error = new FullError(errors, 217934237),
|
||||
/** @type {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.log(`RESET: from ${-5} bits ${12}`);
|
||||
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(reset.integer)}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(reset.string)}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(reset.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.conversions = (tests = 10, inputs = null) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager(inputs);
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << 16));
|
||||
|
||||
console.log(`INTEGER: ${errors.to_integer(error.integer)}, ${errors.to_integer(error.string)}, ${errors.to_integer(error.array)}`);
|
||||
console.log(`STRING: ${errors.to_string(error.integer)}, ${errors.to_string(error.string)}, ${errors.to_string(error.array)}`);
|
||||
console.log(`ARRAY: ${FullError.print(errors.to_array(error.integer))}, ${FullError.print(errors.to_array(error.string))}, ${FullError.print(errors.to_array(error.array))}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
return errors;
|
||||
};
|
||||
|
||||
Tests.alphabet = (tests = 10) => {
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = Tests.conversions(1, {
|
||||
"base" : Tests.random(2, 128)
|
||||
});
|
||||
|
||||
console.log(`^^^ ALPHABET: ${errors.get_alphabet()} ^^^`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.bitwise = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << Tests.random(0, 16))),
|
||||
/** @type {FullError} */
|
||||
shifted = new FullError(errors, 0),
|
||||
/** @type {FullError} */
|
||||
unshifted = new FullError(errors, 0),
|
||||
/** @type {number} */
|
||||
bitwise = Tests.random(-10, 10);
|
||||
|
||||
console.log(`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.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${error.integer}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${error.string}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${FullError.print(error.array)}`);
|
||||
console.log(`INTEGER: ${errors.to_string_binary(shifted.integer)} - ${shifted.integer}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(shifted.string)} - ${shifted.string}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(shifted.array)} - ${FullError.print(shifted.array)}`);
|
||||
console.log(`INTEGER: ${errors.to_string_binary(unshifted.integer)} - ${unshifted.integer}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(unshifted.string)} - ${unshifted.string}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(unshifted.array)} - ${FullError.print(unshifted.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.bitwise_sucesive = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager(),
|
||||
/** @type {FullError} */
|
||||
error = new FullError(errors, Tests.random(0, 1 << 16));
|
||||
|
||||
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${error.integer}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${error.string}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${FullError.print(error.array)}`);
|
||||
console.log();
|
||||
|
||||
for(let i = -tests; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const shifted = new FullError(errors, 0);
|
||||
|
||||
console.log(`BITWISE: ${i}`);
|
||||
|
||||
shifted.integer = errors.bitwise(error.integer, i);
|
||||
shifted.string = errors.bitwise(error.string, i);
|
||||
shifted.array = errors.bitwise(error.array, i);
|
||||
|
||||
|
||||
console.log(`INTEGER: ${errors.to_string_binary(shifted.integer)} - ${shifted.integer}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(shifted.string)} - ${shifted.string}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(shifted.array)} - ${FullError.print(shifted.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.bits = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << Tests.random(0, 28))),
|
||||
/** @type {number} */
|
||||
from_value = Tests.random(-15, 15),
|
||||
/** @type {number} */
|
||||
bits_value = Tests.random(-13, 13),
|
||||
/** @type {[number, number, number]} */
|
||||
from = [from_value, from_value, from_value],
|
||||
/** @type {[number, number, number]} */
|
||||
bits = [bits_value, bits_value, bits_value];
|
||||
|
||||
[from[0], bits[0]] = errors.get_from_bits(error.integer, from[0], bits[0]);
|
||||
[from[1], bits[1]] = errors.get_from_bits(error.string, from[1], bits[1]);
|
||||
[from[2], bits[2]] = errors.get_from_bits(error.array, from[2], bits[2]);
|
||||
|
||||
|
||||
console.log(`CODE: ${error.integer} - ${error.string} - ${FullError.print(error.array)}`);
|
||||
console.log(`ERROR: ${errors.to_string_binary(error.integer)} - ${errors.get_bits(error.integer)} - ${errors.get_bits(error.string)} - ${errors.get_bits(error.array)}`);
|
||||
console.log(`FROM: ${from_value} - ${from[0]}, ${from[1]}, ${from[2]}`);
|
||||
console.log(`BITS: ${bits_value} - ${bits[0]}, ${bits[1]}, ${bits[2]}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.reset = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << 28)),
|
||||
/** @type {number} */
|
||||
from = Tests.random(-15, 15),
|
||||
/** @type {number} */
|
||||
bits = Tests.random(-13, 13),
|
||||
/** @type {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.log(`RESET: from ${from} bits ${bits}`);
|
||||
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(reset.integer)}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(reset.string)}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(reset.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.ranges = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << 28)),
|
||||
/** @type {number} */
|
||||
from = Tests.random(-15, 15),
|
||||
/** @type {number} */
|
||||
bits = Tests.random(-13, 13),
|
||||
/** @type {FullError} */
|
||||
range = new FullError(errors, 0);
|
||||
|
||||
console.log(`RANGE: from ${from} bits ${bits}`);
|
||||
[from, bits] = errors.get_from_bits(error.string, from, bits);
|
||||
console.log(`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.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(range.integer)}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(range.string)}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(range.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.has = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << 28));
|
||||
const from = Tests.random(-15, 15);
|
||||
const bits = Tests.random(-13, 13);
|
||||
|
||||
console.log(`HAS: from ${from} bits ${bits}`);
|
||||
console.log(`INTEGER: ${errors.has(error.integer, from, bits)} - ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(errors.get_range(error.integer, from, bits))}`);
|
||||
console.log(`STRING: ${errors.has(error.string, from, bits)} - ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(errors.get_range(error.string, from, bits))}`);
|
||||
console.log(`ARRAY: ${errors.has(error.array, from, bits)} - ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(errors.get_range(error.array, from, bits))}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
Tests.set = (tests = 10) => {
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors = new ErrorsManager();
|
||||
|
||||
for(let i = 0; i < tests; i ++){
|
||||
|
||||
/** @type {FullError} */
|
||||
const error = new FullError(errors, Tests.random(0, 1 << 15)),
|
||||
/** @type {FullError} */
|
||||
value = new FullError(errors, Tests.random(0, 1 << 15)),
|
||||
/** @type {number} */
|
||||
from = Tests.random(-15, 15),
|
||||
/** @type {number} */
|
||||
bits = Tests.random(-13, 13),
|
||||
/** @type {FullError} */
|
||||
set_integer = new FullError(errors, 0),
|
||||
/** @type {FullError} */
|
||||
set_string = new FullError(errors, 0),
|
||||
/** @type {FullError} */
|
||||
set_array = new FullError(errors, 0);
|
||||
|
||||
set_integer.integer = errors.set(error.integer, value.integer, from, bits);
|
||||
set_string.integer = errors.set(error.integer, value.string, from, bits);
|
||||
set_array.integer = errors.set(error.integer, value.array, from, bits);
|
||||
set_integer.string = errors.set(error.string, value.integer, from, bits);
|
||||
set_string.string = errors.set(error.string, value.string, from, bits);
|
||||
set_array.string = errors.set(error.string, value.array, from, bits);
|
||||
set_integer.array = errors.set(error.array, value.integer, from, bits);
|
||||
set_string.array = errors.set(error.array, value.string, from, bits);
|
||||
set_array.array = errors.set(error.array, value.array, from, bits);
|
||||
|
||||
console.log(`SET: from ${from} bits ${bits} to [${value.string}, ${FullError.print(value.array)}, ${value.integer}]`);
|
||||
console.log(`ERROR: ${errors.to_string_binary(error.integer)} - VALUE: ${errors.to_string_binary(value.integer)}`);
|
||||
console.log(`INTEGER: ${errors.to_string_binary(set_integer.integer)} - ${errors.to_string_binary(set_integer.string)} - ${errors.to_string_binary(set_integer.array)}`);
|
||||
console.log(`STRING: ${errors.to_string_binary(set_string.integer)} - ${errors.to_string_binary(set_string.string)} - ${errors.to_string_binary(set_string.array)}`);
|
||||
console.log(`ARRAY: ${errors.to_string_binary(set_array.integer)} - ${errors.to_string_binary(set_array.string)} - ${errors.to_string_binary(set_array.array)}`);
|
||||
console.log();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
return Tests;
|
||||
})();
|
||||
@ -1,77 +0,0 @@
|
||||
<script type="module" charset="utf-8">
|
||||
"use strict";
|
||||
|
||||
import {ErrorsManager} from "./ecma/ErrorsManager.ecma.js";
|
||||
|
||||
/** @type {ErrorsManager} */
|
||||
const errors_manager = new ErrorsManager();
|
||||
|
||||
console.log(errors_manager.to_string(105));
|
||||
console.log(errors_manager.to_array(105));
|
||||
console.log(errors_manager.to_array_binary(105));
|
||||
console.log(errors_manager.process(105, "0123456789abcdef".split("").map((_, i) => "error_message_" + i)));
|
||||
|
||||
|
||||
console.log(errors_manager.bits(105));
|
||||
console.log(errors_manager.bits("pB"));
|
||||
console.log(errors_manager.bits([41, 1]));
|
||||
|
||||
/** @type {Array.<number>} */
|
||||
const options = [-10, -5, -3, -1, 0, 1, 3, 5, 10],
|
||||
/** @type {Array.<number|string|Array.<number>} */
|
||||
results = [];
|
||||
/** @type {string} */
|
||||
let filler = "";
|
||||
|
||||
while(filler.length < 30)
|
||||
filler += " ";
|
||||
|
||||
/**
|
||||
* @param {!string} value
|
||||
* @param {!number} character
|
||||
* @returns string
|
||||
* @access public
|
||||
*/
|
||||
const format = (value, characters) => " " + (
|
||||
JSON.stringify(value).replace(/,/g, ", ") + filler
|
||||
).slice(0, characters) + " ";
|
||||
|
||||
[105, "pB", [41, 1]].forEach((value, i) => options.forEach(bits => {
|
||||
|
||||
/** @type {string|number|Array.<number>} */
|
||||
const new_value = errors_manager.bitwise(value, bits);
|
||||
|
||||
results.push([
|
||||
format(value, 7),
|
||||
format(errors_manager.to_array_binary(value), 20),
|
||||
format(bits, 4),
|
||||
format(new_value, 12),
|
||||
format(errors_manager.to_array_binary(new_value), 30)
|
||||
]);
|
||||
// console.log(results[results.length - 1]);
|
||||
|
||||
}));
|
||||
|
||||
console.log(results);
|
||||
// console.log(results.map(result => result.join("|")).join("\n"));
|
||||
|
||||
// if ('speechSynthesis' in window) {
|
||||
// console.log("CARGANDO...");
|
||||
// window.speechSynthesis.onvoiceschanged = () => {
|
||||
// console.log("CARGADO");
|
||||
// const voices = window.speechSynthesis.getVoices();
|
||||
// console.log(voices);
|
||||
// const selectedVoice = voices.find(voice => voice.name === 'Google español'); // Ejemplo de selección de voz
|
||||
|
||||
// // Crear un objeto de síntesis de voz
|
||||
// const utterance = new SpeechSynthesisUtterance('Hola, mundo!');
|
||||
// utterance.voice = selectedVoice;
|
||||
|
||||
// // Iniciar la síntesis de voz
|
||||
// window.speechSynthesis.speak(utterance);
|
||||
// };
|
||||
// } else {
|
||||
// console.error('La API de SpeechSynthesis no es compatible con este navegador.');
|
||||
// };
|
||||
|
||||
</script>
|
||||
28
Public/tests.html
Normal file
28
Public/tests.html
Normal file
@ -0,0 +1,28 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title data-i18n="errors_manager_tests">Tests - ErrorsManager</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<script type="module" data-language="ECMAScript 2015" charset="utf-8">
|
||||
"use strict";
|
||||
|
||||
import {Tests} from "./ecma/Tests.ecma.js";
|
||||
|
||||
// Tests.errors();
|
||||
// Tests.conversions();
|
||||
// Tests.alphabet();
|
||||
Tests.bitwise();
|
||||
// Tests.bitwise_sucesive();
|
||||
// Tests.bits();
|
||||
// Tests.reset();
|
||||
// Tests.ranges();
|
||||
// Tests.has();
|
||||
// Tests.set();
|
||||
|
||||
</script>
|
||||
</head>GV9ZEFFAK
|
||||
<body></body>
|
||||
</html>
|
||||
@ -1,15 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from Abstracts.Applications import ApplicationsAbstract
|
||||
from Abstracts.AnPMap import AnP, Optional, Any
|
||||
from os.path import abspath as path_absolute
|
||||
from os.path import dirname as directory_name
|
||||
|
||||
class ErrorsManager(ApplicationsAbstract):
|
||||
|
||||
def __init__(self, anp:AnP|None, inputs:Optional[dict[str, Any|None]|tuple|list] = None) -> None:
|
||||
super().__init__(anp, "errors_manager", anp.path.get_parent(path_absolute(directory_name(__file__))), {
|
||||
**anp.get_dictionary(inputs),
|
||||
"errors_manager_default_settings_files" : "/JSON/ErrorsManager.py.settings.json"
|
||||
})
|
||||
@ -1,396 +1,2 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Any, Optional
|
||||
from math import log2
|
||||
|
||||
class ErrorsManager:
|
||||
|
||||
BASE64:list[str] = [*"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]
|
||||
|
||||
def __init__(self, alphabet:Optional[str|list[str]|tuple[str]] = None) -> None:
|
||||
|
||||
self.__error:int = 0
|
||||
# self.__re_hexa_error:REPattern|None = None
|
||||
self.__alphabet:list[str] = []
|
||||
|
||||
self.set_alphabet(alphabet)
|
||||
|
||||
@staticmethod
|
||||
def is_string(value:Any|None) -> bool:
|
||||
return isinstance(value, str)
|
||||
|
||||
@staticmethod
|
||||
def is_array(value:Any|None) -> bool:
|
||||
return isinstance(value, (list, tuple))
|
||||
|
||||
@staticmethod
|
||||
def is_integer(value:Any|None) -> bool:
|
||||
return isinstance(value, int)
|
||||
|
||||
def set_alphabet(self, alphabet:Optional[str] = None) -> int:
|
||||
|
||||
self.__error = (
|
||||
0 if alphabet == None else
|
||||
1 << 2 if not ErrorsManager.is_array(alphabet) and not ErrorsManager.is_string(alphabet) else
|
||||
0) << 1
|
||||
|
||||
if not self.__error:
|
||||
if alphabet:
|
||||
|
||||
original_length:int = len(self.__alphabet)
|
||||
final_length:int = 0
|
||||
character:str
|
||||
i:int
|
||||
|
||||
alphabet = (
|
||||
alphabet if isinstance(alphabet, list) else
|
||||
list(alphabet) if isinstance(alphabet, tuple) else
|
||||
[*alphabet])
|
||||
alphabet = [character for i, character in alphabet if isinstance(character, str) and len(character) == 1 and alphabet.index(character) == i]
|
||||
final_length = len(alphabet)
|
||||
|
||||
self.__error |= (
|
||||
1 << 0 if original_length != final_length else
|
||||
1 << 1 if final_length < 64 else
|
||||
0) << 5
|
||||
|
||||
self.__alphabet = (
|
||||
self.__alphabet if self.__alphabet and len(self.__alphabet) else
|
||||
ErrorsManager.BASE64) if self.__error or not alphabet else alphabet
|
||||
# self.__re_hexa_error = RECompile(r'[^' + alphabet[0] + ']')
|
||||
|
||||
return self.__error
|
||||
|
||||
def to_array(self,
|
||||
code:str|int|list[int]|tuple[int],
|
||||
length:Optional[int] = 1
|
||||
) -> list[int]:
|
||||
|
||||
array:list[int] = []
|
||||
|
||||
if ErrorsManager.is_string(code):
|
||||
|
||||
hexa:str
|
||||
|
||||
for hexa in code:
|
||||
array += [self.__alphabet.index(hexa)]
|
||||
|
||||
elif ErrorsManager.is_integer(code):
|
||||
while code:
|
||||
array += [code & 0x3F]
|
||||
code >>= 6
|
||||
elif ErrorsManager.is_array(code):
|
||||
|
||||
hexa:int
|
||||
|
||||
for hexa in code:
|
||||
array += [hexa]
|
||||
|
||||
while len(array) < length:
|
||||
array += [0]
|
||||
|
||||
return array
|
||||
|
||||
def process(self,
|
||||
error:int|str|list[int]|tuple[int],
|
||||
messages:list[str]|tuple[str]
|
||||
) -> list[str]:
|
||||
|
||||
response:list[str] = []
|
||||
m:int = len(messages)
|
||||
i:int
|
||||
hexa:int
|
||||
|
||||
for i, hexa in enumerate(self.to_array(error)):
|
||||
|
||||
j:int
|
||||
|
||||
for j in range(6):
|
||||
if hexa & (1 << j):
|
||||
|
||||
k:int = j + i * 6
|
||||
|
||||
response += [[k, messages[k] if k < m and messages[k] else "error_message_" + str(k)]]
|
||||
|
||||
return response
|
||||
|
||||
def get_alphabet(self) -> str:
|
||||
return "".join(self.__alphabet)
|
||||
|
||||
def bits(self, code:str|int|list[int]|tuple[int]) -> int|None:
|
||||
|
||||
if ErrorsManager.is_integer(code):
|
||||
return 1 if not code else int(log2(code) + 1)
|
||||
|
||||
if ErrorsManager.is_string(code):
|
||||
code = self.to_array(code)
|
||||
|
||||
if ErrorsManager.is_array(code):
|
||||
|
||||
code = self.compact(code)
|
||||
|
||||
l:int = len(code)
|
||||
|
||||
return 1 if not l or not code[-1] else (l - 1) * 6 + int(log2(code[-1]) + 1)
|
||||
return None
|
||||
|
||||
def get_error(self) -> int:
|
||||
return self.__error | 0
|
||||
|
||||
def to_array_binary(self, code:str|int|list[int]|tuple[int]) -> list[str]:
|
||||
|
||||
hexa:int
|
||||
|
||||
return [("000000" + "{0:b}".format(hexa))[-6:] for hexa in self.to_array(code)]
|
||||
|
||||
def to_integer(self, code:str|int|list[int]|tuple[int]) -> int:
|
||||
if ErrorsManager.is_integer(code):
|
||||
return code
|
||||
if ErrorsManager.is_array(code):
|
||||
|
||||
hexa:int
|
||||
i:int
|
||||
|
||||
return sum([hexa << i * 6 for i, hexa in enumerate(code)])
|
||||
if ErrorsManager.is_string(code):
|
||||
|
||||
hexa:str
|
||||
i:int
|
||||
|
||||
return sum([self.__alphabet.index(hexa) << i * 6 for i, hexa in enumerate(code)])
|
||||
return 0
|
||||
|
||||
def to_string(self,
|
||||
code:str|int|list[int]|tuple[int],
|
||||
length:Optional[int] = 1
|
||||
) -> str:
|
||||
|
||||
string:str = ""
|
||||
|
||||
if ErrorsManager.is_string(code):
|
||||
string += code
|
||||
elif ErrorsManager.is_integer(code):
|
||||
while code:
|
||||
string += self.__alphabet[code & 0x3F]
|
||||
code >>= 6
|
||||
elif ErrorsManager.is_array(code):
|
||||
|
||||
hexa:str
|
||||
|
||||
string = "".join([self.__alphabet[hexa] for hexa in code])
|
||||
|
||||
while len(string) < length:
|
||||
string += self.__alphabet[0]
|
||||
|
||||
return string or self.__alphabet[0]
|
||||
|
||||
def has(self,
|
||||
code:int|str|tuple[int]|list[int],
|
||||
bits:Optional[int|list[int]|tuple[int]] = None
|
||||
) -> bool:
|
||||
if not ErrorsManager.is_integer(bits) and not ErrorsManager.is_array(bits):
|
||||
if ErrorsManager.is_string(code):
|
||||
|
||||
hexa:str
|
||||
|
||||
for hexa in code:
|
||||
if self.__alphabet.index(hexa):
|
||||
return True
|
||||
return False
|
||||
if ErrorsManager.is_integer(code):
|
||||
return not not code
|
||||
if ErrorsManager.is_array(code):
|
||||
|
||||
hexa:int
|
||||
|
||||
for hexa in code:
|
||||
if hexa:
|
||||
return True
|
||||
return False
|
||||
|
||||
error:list[int] = self.to_array(code)
|
||||
bit:int
|
||||
|
||||
if ErrorsManager.is_integer(bits):
|
||||
bits = (bits,)
|
||||
|
||||
for bit in bits[:len(error) - 1]:
|
||||
if error[bit]:
|
||||
return True
|
||||
return False
|
||||
|
||||
def to_unknown(self, code:Any|None) -> Any|None:
|
||||
return code
|
||||
|
||||
def compact(self, code:int|str|list[int]|tuple[int]) -> int|str|list[int]:
|
||||
if ErrorsManager.is_string(code):
|
||||
while code and code[-1] == self.__alphabet[0]:
|
||||
code = code[:-1]
|
||||
return code or self.__alphabet[0]
|
||||
if ErrorsManager.is_array(code):
|
||||
code = list(code)
|
||||
while len(code) and not code[-1]:
|
||||
code = code[:-1]
|
||||
return code if len(code) else [0]
|
||||
if ErrorsManager.is_integer(code):
|
||||
return code
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
def type(self, code:int|str|tuple[int]|list[int]):
|
||||
return (
|
||||
"string" if self.is_string(code) else
|
||||
"integer" if self.is_integer(code) else
|
||||
"array" if self.is_array(code) else
|
||||
"unknown")
|
||||
|
||||
def bitwise(self,
|
||||
code:int|str|tuple[int]|list[int],
|
||||
bits:int
|
||||
) -> str|int|list[int]:
|
||||
|
||||
if not bits or not self.has(code):
|
||||
return code
|
||||
|
||||
reverse:bool = bits < 0
|
||||
|
||||
if reverse:
|
||||
bits *= -1
|
||||
|
||||
start:int = int(bits / 6)
|
||||
rest:int = bits % 6
|
||||
type_method:str = "to_" + ErrorsManager.type(code)
|
||||
|
||||
code = self.to_array(code)
|
||||
|
||||
if reverse:
|
||||
|
||||
code = code[start:]
|
||||
|
||||
if rest:
|
||||
if code:
|
||||
|
||||
r:int = 6 - rest
|
||||
block:int = ~-(1 << rest)
|
||||
i:int
|
||||
hexa:int
|
||||
|
||||
code = [(hexa >> rest) | ((code[i + 1] & block) << r) for i, hexa in enumerate(code[:-1])] + [code[-1] >> rest]
|
||||
|
||||
else:
|
||||
code = [0]
|
||||
|
||||
else:
|
||||
|
||||
i:int
|
||||
|
||||
if rest:
|
||||
|
||||
r:int = 6 - rest
|
||||
mask:int = ~-(1 << 6)
|
||||
|
||||
code = [(code[0] << rest) & mask] + [((hexa << rest) & mask) | (code[i - 1] >> r) for i, hexa in enumerate(code[1:])] + [code[-1] >> r]
|
||||
|
||||
for i in range(start):
|
||||
code = [0] + code
|
||||
|
||||
return getattr(self, type_method)(code)
|
||||
|
||||
def set(self,
|
||||
code:int|str|list[int]|tuple[int],
|
||||
error:int|str|list[int]|tuple[int],
|
||||
bit:Optional[int] = 0,
|
||||
length:Optional[int] = 0
|
||||
) -> str:
|
||||
|
||||
code = self.to_array(code)
|
||||
error = self.to_array(error)
|
||||
|
||||
if bit:
|
||||
error = self.bitwise(error, bit)
|
||||
|
||||
i:int = int(bit / 6)
|
||||
|
||||
if length:
|
||||
|
||||
start:int = bit % 6
|
||||
j:int
|
||||
hexa:int
|
||||
end:int = (start + length) % 6
|
||||
|
||||
for j, hexa in enumerate([~-(1 << start)] + [0 for j in range(int((length + start) / 6) - 1)] + ([~-(1 << (6 - end)) << end] if end else [])):
|
||||
code[j + i] &= hexa
|
||||
|
||||
if self.has(error):
|
||||
|
||||
l:int = len(error)
|
||||
|
||||
while len(code) < i:
|
||||
code += [0]
|
||||
|
||||
m:int = len(code)
|
||||
|
||||
while i < l:
|
||||
if i >= m:
|
||||
code += [0]
|
||||
code[i] = (code[i] or 0) | error[i]
|
||||
i += 1
|
||||
|
||||
return self.compact(self.to_string(code))
|
||||
|
||||
def join(self,
|
||||
code:int|str|list[int]|tuple[int],
|
||||
error:int|str|list[int]|tuple[int],
|
||||
bit:Optional[int] = 0,
|
||||
length:Optional[int] = 0
|
||||
) -> str:
|
||||
return self.set(code, error, bit, length)
|
||||
|
||||
def set_blocks(self,
|
||||
code:str|int|tuple[int]|list[int],
|
||||
blocks:list[str|int|tuple[int]|list[int]]|tuple[str|int|tuple[int]|list[int]],
|
||||
bit:Optional[int] = 0,
|
||||
length:Optional[int] = 0
|
||||
) -> str:
|
||||
|
||||
block:int
|
||||
i:int
|
||||
|
||||
if length:
|
||||
code = self.set(code, 0, bit, length)
|
||||
|
||||
for i, block in blocks:
|
||||
if block:
|
||||
code = self.set(code, block, i + bit)
|
||||
|
||||
return code or self.__alphabet[0]
|
||||
|
||||
def slice(self,
|
||||
code:str|int|list[int]|tuple[int],
|
||||
_from:int,
|
||||
_to:Optional[int] = 0
|
||||
) -> str|int|list[int]|None:
|
||||
if self.has(code):
|
||||
return code
|
||||
|
||||
bits:int = self.bits(code)
|
||||
rest:int
|
||||
|
||||
if _from < 0:
|
||||
_from = bits + _from
|
||||
_to = (
|
||||
bits if _to > bits else
|
||||
bits - _to if _to < 0 else
|
||||
_to) - _from
|
||||
rest = _to %6
|
||||
code = self.bitwise(code, -_from)
|
||||
|
||||
return (
|
||||
code[:int(_to / 6)] + (self.__alphabet[self.__alphabet.index(code[-1]) & ~-(1 << rest)] if rest else "") if ErrorsManager.is_string(code) else
|
||||
code[:int(_to / 6)] + ([code[-1] & ~-(1 << rest)] if rest else []) if ErrorsManager.is_array(code) else
|
||||
code & ~-(1 << _to) if ErrorsManager.is_integer(code) else
|
||||
None)
|
||||
|
||||
def has_range(self, code:int|str|list[int]|tuple[int], _from:int, _to:Optional[int] = 0) -> bool:
|
||||
return self.has(self.slice(code, _from, _to))
|
||||
@ -1,65 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from ErrorsManager import ErrorsManager
|
||||
from json import dumps as json_encode
|
||||
|
||||
errors_manager:ErrorsManager = ErrorsManager()
|
||||
|
||||
print(errors_manager.to_string(105))
|
||||
print(errors_manager.to_array(105))
|
||||
print(errors_manager.to_array_binary(105))
|
||||
print(errors_manager.process(105, ["error_message_" + str(i) for i, _ in enumerate("0123456789abcdef")]))
|
||||
|
||||
|
||||
print(errors_manager.bits(105))
|
||||
print(errors_manager.bits("pB"))
|
||||
print(errors_manager.bits([41, 1]))
|
||||
|
||||
options = [-10, -5, -3, -1, 0, 1, 3, 5, 10]
|
||||
filler = ""
|
||||
results = []
|
||||
|
||||
while len(filler) < 30:
|
||||
filler += " "
|
||||
|
||||
def format(value, characters):
|
||||
return " " + (
|
||||
json_encode(value).replace(",", ", ") + filler
|
||||
)[:characters]
|
||||
|
||||
for i, value in enumerate([105, "pB", [41, 1]]):
|
||||
for bits in options:
|
||||
|
||||
new_value = errors_manager.bitwise(value, bits)
|
||||
example = [
|
||||
format(value, 7),
|
||||
format(errors_manager.to_array_binary(value), 20),
|
||||
format(bits, 4),
|
||||
format(new_value, 12),
|
||||
format(errors_manager.to_array_binary(new_value), 30)
|
||||
]
|
||||
|
||||
results += [example]
|
||||
|
||||
print(example)
|
||||
|
||||
print("")
|
||||
|
||||
# [105, "pB", [41, 1]].forEach((value, i) => options.forEach(bits => {
|
||||
|
||||
# const new_value = errors_manager.bitwise(value, bits)
|
||||
|
||||
# results.push([
|
||||
# format(value, 7),
|
||||
# format(errors_manager.to_array_binary(value), 20),
|
||||
# format(bits, 4),
|
||||
# format(new_value, 12),
|
||||
# format(errors_manager.to_array_binary(new_value), 30)
|
||||
# ])
|
||||
# // print(results[results.length - 1])
|
||||
|
||||
# }))
|
||||
|
||||
# -- print(results)
|
||||
# // print(results.map(result => result.join("|")).join("\n"))
|
||||
@ -1,254 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Self, Any, Optional, Sequence
|
||||
from math import log2
|
||||
from re import compile as re_compile, Pattern as REPattern, IGNORECASE as RE_IGNORE_CASE
|
||||
|
||||
class ErrorsManager:
|
||||
|
||||
RE_KEY:REPattern = re_compile(r'^[a-z_][a-z0-9_]*$', RE_IGNORE_CASE)
|
||||
|
||||
def __init__(self:Self, inputs:Optional[str|dict[str, Any|None]|Sequence[Any|None]] = None) -> None:
|
||||
|
||||
alphabet:str|Sequence[str] = self.unique(self.get_value("alphabet", (
|
||||
inputs := {"alphabet" : inputs} if isinstance(inputs, str) else
|
||||
inputs), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="))
|
||||
maximum_base:int = int(log2(len(alphabet)))
|
||||
|
||||
self.__alphabet:tuple[str] = (
|
||||
tuple(alphabet) if isinstance(alphabet, (list, str)) else
|
||||
alphabet)
|
||||
self.__alphabet_map:dict[str, int] = {character : i for i, character in enumerate(self.__alphabet)}
|
||||
self.__base:int = self.get_value("base", inputs, maximum_base)
|
||||
self.__mask:int
|
||||
|
||||
if self.__base > maximum_base:
|
||||
self.__base = maximum_base
|
||||
self.__mask = ~-(1 << self.__base)
|
||||
|
||||
print([self.__mask, self.__base, self.__alphabet])
|
||||
|
||||
def to_string(self:Self, code:str|int|Sequence[int]) -> str:
|
||||
if isinstance(code, str):
|
||||
return code
|
||||
if isinstance(code, (list, tuple)):
|
||||
return "".join([self.__alphabet[hexa] for hexa in code])
|
||||
if isinstance(code, int):
|
||||
|
||||
string:str = ""
|
||||
|
||||
while code:
|
||||
string = self.__alphabet[code & self.__mask] + string
|
||||
code >>= self.__base
|
||||
|
||||
return string
|
||||
return ""
|
||||
|
||||
def to_integer(self:Self, code:str|int|Sequence[int]) -> int:
|
||||
if isinstance(code, str):
|
||||
|
||||
integer:int = 0
|
||||
hexa:str
|
||||
|
||||
for i, hexa in enumerate(reversed(code)):
|
||||
integer |= self.__alphabet_map[hexa] << self.__base * i
|
||||
|
||||
return integer
|
||||
if isinstance(code, (list, tuple)):
|
||||
|
||||
integer:int = 0
|
||||
hexa:int
|
||||
|
||||
for i, hexa in enumerate(reversed(code)):
|
||||
integer |= hexa << self.__base * i
|
||||
|
||||
return integer
|
||||
if isinstance(code, int):
|
||||
return code
|
||||
return 0
|
||||
|
||||
def to_array(self:Self, code:str|int|Sequence[int]) -> list[int]:
|
||||
if isinstance(code, str):
|
||||
return [self.__alphabet_map[hexa] for hexa in code]
|
||||
if isinstance(code, list):
|
||||
return list(code)
|
||||
if isinstance(code, tuple):
|
||||
return code
|
||||
if isinstance(code, int):
|
||||
|
||||
array:list[int] = []
|
||||
|
||||
while code:
|
||||
array.append(code & self.__mask)
|
||||
code >>= self.__base
|
||||
|
||||
return list(reversed(array))
|
||||
return []
|
||||
|
||||
def reset_bak(self:Self, code:int, start:int, length:int) -> int:
|
||||
return code and (code & ~(~-(1 << length) << start))
|
||||
|
||||
def set_bak(self:Self, code:int, map:Sequence[int], codes:Sequence[int], start:int = 0, clean_bits:int = 0) -> int:
|
||||
|
||||
new_code:int = 0
|
||||
shift:int = 0
|
||||
block:int
|
||||
i:int
|
||||
|
||||
code = self.reset(code, start, clean_bits)
|
||||
|
||||
for i, block in enumerate(map):
|
||||
new_code |= (codes[i] or (code >> start + shift & ~-(1 << block))) << shift
|
||||
shift += block
|
||||
|
||||
return (
|
||||
(code & ~-(1 << start)) |
|
||||
(new_code << start) |
|
||||
(code & ~(~-(1 << start + shift)))
|
||||
)
|
||||
|
||||
def has(self:Self, code:str) -> bool:
|
||||
|
||||
errors:tuple[str] = self.__alphabet[1:]
|
||||
character:str
|
||||
|
||||
for character in code:
|
||||
if character in errors:
|
||||
return True
|
||||
return False
|
||||
|
||||
def clean(self:Self, code:str) -> str:
|
||||
|
||||
while code[0] == self.__alphabet[0]:
|
||||
code = code[1:]
|
||||
|
||||
return code
|
||||
|
||||
def reset(self:Self, code:str, start:int = 0, length:int = 0) -> str:
|
||||
|
||||
i:int
|
||||
character:str
|
||||
total:int = start + (length or (length := len(code) * self.__base))
|
||||
_from:int|None = -start // self.__base or None
|
||||
_to:int|None = (-total // self.__base) - (1 if total % self.__base else 0) or None
|
||||
hexas:list[int] = []
|
||||
|
||||
if _from:
|
||||
start += _from * self.__base
|
||||
total += _from * self.__base
|
||||
|
||||
for i, character in enumerate(reversed(code[-_to:_from])):
|
||||
|
||||
bit:int = i * self.__base
|
||||
next_bit:int = bit + self.__base
|
||||
|
||||
if bit >= start and next_bit < total:
|
||||
hexas.append(0)
|
||||
continue
|
||||
|
||||
hexa:int = self.__alphabet_map[character]
|
||||
j:int
|
||||
|
||||
for j in range(self.__base):
|
||||
if bit < start:
|
||||
continue
|
||||
if bit >= total:
|
||||
break
|
||||
hexa &= ~(1 << j)
|
||||
bit += 1
|
||||
|
||||
return (
|
||||
(code[:_to] if _to else "") +
|
||||
"".join(self.__alphabet[hexa] for hexa in hexas)[::-1] +
|
||||
(code[_from:] if _from else "")
|
||||
)
|
||||
|
||||
def set(self:Self, code:str, new_code:int|str, start:int = 0, clean_bits:int = 0) -> str:
|
||||
|
||||
|
||||
|
||||
return code
|
||||
|
||||
def validate(self:Self,
|
||||
code:int,
|
||||
map:Sequence[int],
|
||||
messages:Sequence[Sequence[str]] = []
|
||||
) -> tuple[tuple[int, str]]:
|
||||
|
||||
errors:list[tuple[int, str]] = []
|
||||
|
||||
if code:
|
||||
|
||||
shift:int = 0
|
||||
l:int = len(messages)
|
||||
i:int
|
||||
block:int
|
||||
|
||||
for i, block in enumerate(map):
|
||||
|
||||
subcode:int = (code >> shift) & ~-(1 << block)
|
||||
|
||||
shift += block
|
||||
subcode and errors.append((i, (
|
||||
messages[i][subcode - 1] if i < l and subcode - 1 < len(messages[i]) else
|
||||
"error_" + str(i) + "_" + str(subcode))))
|
||||
|
||||
return len(errors) != 0, tuple(errors)
|
||||
|
||||
@classmethod
|
||||
def get_keys(cls:type[Self], *items:Sequence[Any|None]) -> list[str]:
|
||||
|
||||
keys:list[str] = []
|
||||
item:Any|None
|
||||
|
||||
for item in items:
|
||||
if isinstance(item, str):
|
||||
cls.RE_KEY.match(item) and keys.append(item)
|
||||
elif isinstance(item, (list, tuple)):
|
||||
keys.extend(cls.get_keys(*item))
|
||||
|
||||
return keys
|
||||
|
||||
@classmethod
|
||||
def get_dictionaries(cls:type[Self], *items:Sequence[Any|None]) -> list[dict[str, Any|None]]:
|
||||
|
||||
dictionaries:list[str] = []
|
||||
item:Any|None
|
||||
|
||||
for item in items:
|
||||
if isinstance(item, dict):
|
||||
dictionaries.append(item)
|
||||
elif isinstance(item, (list, tuple)):
|
||||
dictionaries.extend(cls.get_dictionaries(*item))
|
||||
|
||||
return dictionaries
|
||||
|
||||
@classmethod
|
||||
def get_value(cls:type[Self],
|
||||
keys:str|Sequence[str],
|
||||
inputs:dict[str, Any|None]|Sequence[Any|None],
|
||||
default:Optional[Any] = None
|
||||
) -> Any|None:
|
||||
if len(keys := cls.get_keys(keys)):
|
||||
|
||||
subinputs:dict[str, Any|None]
|
||||
|
||||
for subinputs in cls.get_dictionaries(inputs):
|
||||
|
||||
key:str
|
||||
|
||||
for key in keys:
|
||||
if key in subinputs:
|
||||
return subinputs[key]
|
||||
return default
|
||||
|
||||
@staticmethod
|
||||
def unique(items:str|Sequence[Any|None]) -> str|Sequence[Any|None]:
|
||||
if isinstance(items, str):
|
||||
return "".join(character for i, character in enumerate(items) if items.index(character) == i)
|
||||
if isinstance(items, tuple):
|
||||
return tuple(item for i, item in enumerate(items) if items.index(item) == i)
|
||||
if isinstance(items, list):
|
||||
return [item for i, item in enumerate(items) if items.index(item) == i]
|
||||
return items
|
||||
@ -1,48 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from ErrorsManager import ErrorsManager
|
||||
|
||||
class Tests:
|
||||
|
||||
@staticmethod
|
||||
def builder() -> None:
|
||||
print(ErrorsManager())
|
||||
|
||||
@staticmethod
|
||||
def conversions() -> None:
|
||||
|
||||
option:str|int|list[int]
|
||||
errors:ErrorsManager = ErrorsManager()
|
||||
|
||||
for option in (
|
||||
"Hola", 923452, [45, 2, 0, 12]
|
||||
):
|
||||
|
||||
string:str = errors.to_string(option)
|
||||
integer:int = errors.to_integer(option)
|
||||
array:list[int] = errors.to_array(option)
|
||||
|
||||
print(option)
|
||||
print(["T", {
|
||||
"s" : string,
|
||||
"i" : integer,
|
||||
"a" : array
|
||||
}])
|
||||
print(["S", {
|
||||
"s" : errors.to_string(string),
|
||||
"i" : errors.to_integer(string),
|
||||
"a" : errors.to_array(string)
|
||||
}])
|
||||
print(["I", {
|
||||
"s" : errors.to_string(integer),
|
||||
"i" : errors.to_integer(integer),
|
||||
"a" : errors.to_array(integer)
|
||||
}])
|
||||
print(["A", {
|
||||
"s" : errors.to_string(array),
|
||||
"i" : errors.to_integer(array),
|
||||
"a" : errors.to_array(array)
|
||||
}])
|
||||
|
||||
Tests.conversions()
|
||||
621
SQLServer/ErrorsManager.server.sql
Normal file
621
SQLServer/ErrorsManager.server.sql
Normal file
@ -0,0 +1,621 @@
|
||||
if (select top 1 0 from sys.databases where name = 'ErrorsManager') is null create database ErrorsManager collate Latin1_General_CI_AS
|
||||
go
|
||||
use ErrorsManager
|
||||
|
||||
if object_id(N'dbo.tables_drop', N'P') is not null drop procedure dbo.tables_drop
|
||||
go
|
||||
create procedure dbo.tables_drop as begin set nocount on
|
||||
|
||||
-- Level 1.
|
||||
if object_id(N'dbo.Settings', N'U') is not null drop table dbo.Settings
|
||||
|
||||
-- Level 0.
|
||||
if object_id(N'dbo.Sets', N'U') is not null drop table dbo.Sets
|
||||
if object_id(N'dbo.Types', N'U') is not null drop table dbo.Types
|
||||
if object_id(N'dbo.Messages', N'U') is not null drop table dbo.Messages
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.tables_create', N'P') is not null drop procedure dbo.tables_create
|
||||
go
|
||||
create procedure dbo.tables_create as begin set nocount on
|
||||
|
||||
-- Level 0.
|
||||
if object_id(N'dbo.Sets', N'U') is null create table dbo.Sets(
|
||||
id int not null identity(1, 1),
|
||||
[key] varchar(32) not null,
|
||||
alphabet varchar(128) collate Latin1_General_CS_AS not null,
|
||||
base smallint not null,
|
||||
power tinyint not null,
|
||||
in_use bit not null constraint sets_df_in_use default 0,
|
||||
[description] varchar(512),
|
||||
date_in datetime not null constraint sets_df_date_in default getdate(),
|
||||
last_selection datetime,
|
||||
date_out datetime,
|
||||
constraint sets_pk primary key clustered (id),
|
||||
constraint sets_uk_key unique nonclustered ([key]) with (fillfactor = 90),
|
||||
constraint sets_uk_data unique nonclustered (alphabet, base, power) with (fillfactor = 90),
|
||||
constraint sets_ck_key check (
|
||||
[key] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([key]) > 0 and
|
||||
len([key]) <= 32
|
||||
),
|
||||
constraint sets_ck_alphabet check (
|
||||
datalength(alphabet) >= 2 and
|
||||
datalength(alphabet) <= 128
|
||||
),
|
||||
constraint sets_ck_base check (base >= 2 and base <= 128),
|
||||
constraint sets_ck_power check (power > 0 and power < 8)
|
||||
)
|
||||
|
||||
if object_id(N'dbo.Types', N'U') is null create table dbo.Types(
|
||||
id int not null identity(1, 1),
|
||||
[name] varchar(32) not null,
|
||||
[description] varchar(512),
|
||||
date_in datetime not null constraint types_df_date_in default getdate(),
|
||||
date_out datetime,
|
||||
constraint types_pk primary key clustered (id),
|
||||
constraint types_uk_name unique nonclustered ([name]) with (fillfactor = 90),
|
||||
constraint types_ck_name check (
|
||||
[name] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([name]) > 0 and
|
||||
len([name]) <= 32
|
||||
)
|
||||
)
|
||||
|
||||
if object_id(N'dbo.Messages', N'U') is null create table dbo.Messages(
|
||||
id int not null identity(1, 1),
|
||||
[bit] smallint not null,
|
||||
[message] varchar(64) not null,
|
||||
date_in datetime not null constraint messages_df_date_in default getdate(),
|
||||
date_out datetime,
|
||||
constraint messages_pk primary key clustered (id),
|
||||
constraint messages_uk_data unique nonclustered ([bit], [message]) with (fillfactor = 90)
|
||||
)
|
||||
|
||||
-- Level 1.
|
||||
if object_id(N'dbo.Settings', N'U') is null create table dbo.Settings(
|
||||
id int not null identity(1, 1),
|
||||
[type] integer not null,
|
||||
[key] varchar(32) not null,
|
||||
[value] varchar(128),
|
||||
[description] varchar(512),
|
||||
date_in datetime not null constraint settings_df_date_in default getdate(),
|
||||
date_out datetime,
|
||||
constraint settings_pk primary key clustered (id),
|
||||
constraint settings_fk_type foreign key ([type]) references dbo.Types(id)
|
||||
on update no action
|
||||
on delete no action,
|
||||
constraint settings_uk_key unique nonclustered ([key]) with (fillfactor = 90),
|
||||
constraint settings_ck_key check (
|
||||
[key] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([key]) > 0 and
|
||||
len([key]) <= 32
|
||||
)
|
||||
)
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.tables_update', N'P') is not null drop procedure dbo.tables_update
|
||||
go
|
||||
create procedure dbo.tables_update as begin set nocount on
|
||||
|
||||
declare @database varchar(64) = db_name()
|
||||
|
||||
-- Level 0.
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Sets' and column_name = 'key') is null
|
||||
alter table dbo.Sets add
|
||||
[key] varchar(32) not null,
|
||||
constraint sets_uk_key unique nonclustered ([key]) with (fillfactor = 90),
|
||||
constraint sets_ck_key check (
|
||||
[key] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([key]) > 0 and
|
||||
len([key]) <= 32
|
||||
)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Sets' and column_name = 'alphabet') is null
|
||||
alter table dbo.Sets add
|
||||
alphabet varchar(128) not null,
|
||||
constraint sets_ck_alphabet check (
|
||||
datalength(alphabet) >= 2 and
|
||||
datalength(alphabet) <= 128
|
||||
)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Sets' and column_name = 'base') is null
|
||||
alter table dbo.Sets add
|
||||
base smallint not null,
|
||||
constraint sets_ck_base check (base >= 2 and base <= 128)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Sets' and column_name = 'power') is null
|
||||
alter table dbo.Sets add
|
||||
power tinyint not null,
|
||||
constraint sets_ck_power check (power > 0 and power < 8)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Sets' and column_name = 'in_use') is null
|
||||
alter table dbo.Sets add in_use bit not null constraint sets_df_in_use default 0
|
||||
if (select top 1 0 from information_schema.constraints where table_catalog = @database and table_name = 'Sets' and constraint_name = 'sets_uk_data') is null
|
||||
alter table dbo.Sets add constraint sets_uk_data unique nonclustered (alphabet, base, power) with (fillfactor = 90)
|
||||
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Types' and column_name = 'name') is null
|
||||
alter table dbo.Types add
|
||||
[name] varchar(32) not null,
|
||||
constraint types_uk_name unique nonclustered ([name]) with (fillfactor = 90),
|
||||
constraint types_ck_name check (
|
||||
[name] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([name]) > 0 and
|
||||
len([name]) <= 32
|
||||
)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Types' and column_name = 'description') is null
|
||||
alter table dbo.Types add [description] varchar(512)
|
||||
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Messages' and column_name = 'bit') is null
|
||||
alter table dbo.Messages add [bit] smallint not null
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Messages' and column_name = 'message') is null
|
||||
alter table dbo.Messages add [message] varchar(64) not null
|
||||
if (select top 1 0 from information_schema.constraints where table_catalog = @database and table_name = 'Messages' and constraint_name = 'messages_uk_data') is null
|
||||
alter table dbo.Messages add constraint messages_uk_data unique nonclustered ([bit], [message]) with (fillfactor = 90)
|
||||
|
||||
-- Level 1.
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Settings' and column_name = 'key') is null
|
||||
alter table dbo.Settings add
|
||||
[key] varchar(32) not null,
|
||||
constraint settings_uk_key unique nonclustered ([key]) with (fillfactor = 90),
|
||||
constraint settings_ck_key check (
|
||||
[key] not like '%[^a-zA-Z0-9_]%' and
|
||||
len([key]) > 0 and
|
||||
len([key]) <= 32
|
||||
)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Settings' and column_name = 'value') is null
|
||||
alter table dbo.Settings add [value] varchar(128)
|
||||
if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Settings' and column_name = 'description') is null
|
||||
alter table dbo.Settings add [description] varchar(512)
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.tables_fill', N'P') is not null drop procedure dbo.tables_fill
|
||||
go
|
||||
create procedure dbo.tables_fill as begin set nocount on
|
||||
|
||||
if object_id(N'tempdb..#Settings', N'U') is not null drop table #Settings
|
||||
create table #Settings(
|
||||
[type] varchar(32) not null,
|
||||
[key] varchar(32) not null,
|
||||
[value] varchar(128)
|
||||
)
|
||||
if object_id(N'tempdb..#Messages', N'U') is not null drop table #Messages
|
||||
create table #Messages(
|
||||
[bit] smallint not null,
|
||||
[message] varchar(64) not null
|
||||
)
|
||||
|
||||
insert into #Settings([type], [key], [value]) values
|
||||
('string', 'alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='),
|
||||
('integer', 'base', '64')
|
||||
|
||||
insert into #Messages([bit], [message]) values
|
||||
(0, 'invalid_alphabet'),
|
||||
(1, 'invalid_base'),
|
||||
(2, 'invalid_alphabet_type'),
|
||||
(3, 'too_short_alphabet'),
|
||||
(4, 'repeated_characters_in_alphabet'),
|
||||
(5, 'too_long_alphabet'),
|
||||
(6, 'base_lower_than_2'),
|
||||
(7, 'base_greater_than_128'),
|
||||
(8, 'base_greater_than_alphabet')
|
||||
|
||||
insert into dbo.Types([name]) select distinct [type] from #Settings where [type] not in (
|
||||
select [name] from dbo.Types
|
||||
)
|
||||
insert into dbo.Settings([type], [key], [value]) select
|
||||
types.id,
|
||||
settings.[key],
|
||||
settings.[value]
|
||||
from #Settings as settings
|
||||
inner join dbo.Types as types on types.name = settings.type
|
||||
where settings.[key] not in (
|
||||
select [key] from dbo.Settings
|
||||
)
|
||||
|
||||
insert into dbo.Messages([bit], [message]) select
|
||||
[bit],
|
||||
[message]
|
||||
from #Messages where ([bit], [message]) not in (
|
||||
select [bit], [message] from dbo.Messages
|
||||
)
|
||||
|
||||
if (select count(1) from dbo.Sets) = 0 begin
|
||||
end
|
||||
|
||||
drop table #Settings
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.settings_get', N'FN') is not null drop function dbo.settings_get
|
||||
go
|
||||
create function dbo.settings_get(
|
||||
@key varchar(32),
|
||||
@default varchar(128)
|
||||
) returns varchar(128) as begin
|
||||
return isnull((
|
||||
select top 1 [value] from dbo.Settings where [key] = @key and date_out is null order by date_in desc
|
||||
), @default)
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.settings_get_int', N'FN') is not null drop function dbo.settings_get_int
|
||||
go
|
||||
create function dbo.settings_get_int(
|
||||
@key varchar(32),
|
||||
@default integer
|
||||
) returns integer as begin
|
||||
return convert(integer, dbo.settings_get(@key, @default))
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.shift', N'FN') is not null drop function dbo.shift
|
||||
go
|
||||
create function dbo.shift(
|
||||
@value integer,
|
||||
@bits smallint
|
||||
) returns integer as begin
|
||||
return (case
|
||||
when @bits is null or @value is null then 0
|
||||
when @bits > 0 then @value * power(2, @bits)
|
||||
when @bits < 0 then @value / power(2, -@bits)
|
||||
else @value end)
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.unique', N'FN') is not null drop function dbo.unique
|
||||
go
|
||||
create function dbo.unique(
|
||||
@string varchar(max)
|
||||
) returns varchar(128) as begin
|
||||
|
||||
declare @i integer = 1
|
||||
declare @l integer = datalength(@string)
|
||||
declare @unique varchar(128) = ''
|
||||
|
||||
while @i <= @l begin
|
||||
|
||||
declare @character char(1) = substring(@string, @i, 1)
|
||||
|
||||
if charindex(@character, @unique) = 0
|
||||
set @unique += @character
|
||||
set @i += 1
|
||||
|
||||
end
|
||||
|
||||
return @unique
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.log2', N'FN') is not null drop function dbo.log2
|
||||
go
|
||||
create function dbo.log2(
|
||||
@value float
|
||||
) returns float as begin
|
||||
return log(@value) / log(2)
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.set_alphabet', N'P') is not null drop procedure dbo.set_alphabet
|
||||
go
|
||||
create procedure dbo.set_alphabet(
|
||||
@alphabet varchar(max),
|
||||
@base smallint,
|
||||
@id integer output,
|
||||
@error integer output
|
||||
) begin set nocount on
|
||||
|
||||
declare @original_length integer
|
||||
declare @power tinyint
|
||||
|
||||
set @error = 0
|
||||
|
||||
if @alphabet is null set @alphabet = dbo.settings_get('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=')
|
||||
if @base is null set @base = dbo.settings_get_int('base', 64)
|
||||
|
||||
set @original_length = datalength(@alphabet)
|
||||
|
||||
set @alphabet = dbo.unique(@alphabet)
|
||||
|
||||
if datalength(@alphabet) > 2 begin
|
||||
set @error = @error | dbo.shift(1, 3)
|
||||
set @alphabet = dbo.settings_get('alphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=')
|
||||
end
|
||||
if datalength(@alphabet) != original_length
|
||||
set @error = @error | dbo.shift(1, 4)
|
||||
if datalength(@alphabet) > 128
|
||||
set @error = @error | dbo.shift(1, 5)
|
||||
|
||||
set @error = @error | dbo.shift(1, case
|
||||
when @base < 2 then 6
|
||||
when @base > 128 then 7
|
||||
when @base >= datalength(@alphabet) then 8
|
||||
else null end)
|
||||
|
||||
if dbo.shift(error, -6) = 0 set @alphabet = substring(@alphabet, 1, @base)
|
||||
|
||||
set @power = cast(dbo.log2(datalength(substring(@alphabet, 1, 128))) as integer)
|
||||
set @base = power(2, @power)
|
||||
|
||||
insert into dbo.Sets(alphabet, base, power) values
|
||||
(substring(@alphabet, 1, @base), @base, @power)
|
||||
set @id = scope_identity()
|
||||
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.change', N'P') is not null drop procedure dbo.change
|
||||
go
|
||||
create procedure dbo.change(
|
||||
@key varchar(32)
|
||||
) begin set nocount on
|
||||
if (select top 1 0 from dbo.Sets where [key] = @key and in_use = 1) = 0 begin
|
||||
update dbo.Sets set in_use = 0 where in_use = 1
|
||||
update dbo.Sets set
|
||||
in_use = 1,
|
||||
last_selection = getdate()
|
||||
where [key] = @key
|
||||
end
|
||||
end
|
||||
|
||||
if object_id(N'dbo.get_alphabet', N'FN') is not null drop function dbo.get_alphabet
|
||||
go
|
||||
create function dbo.get_alphabet() returns varchar(128) as begin
|
||||
return (select top 1 alphabet from dbo.Sets where in_use = 1)
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.to_string', N'FN') is not null drop function dbo.to_string
|
||||
go
|
||||
create function dbo.to_string(
|
||||
@code integer
|
||||
) returns varchar(512) as begin
|
||||
|
||||
declare @hexas varchar(512) = ''
|
||||
declare @alphabet varchar(128)
|
||||
declare @base smallint
|
||||
|
||||
select top 1 @alphabet = alphabet, @base = base from dbo.Sets where in_use = 1
|
||||
|
||||
while @code != 0 begin
|
||||
|
||||
declare @module integer = @code % @base
|
||||
|
||||
set @hexas = @hexas + substring(@alphabet, @module + 1, 1)
|
||||
set @code = @code / @base
|
||||
|
||||
end
|
||||
|
||||
return @hexas
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.to_integer', N'FN') is not null drop function dbo.to_integer
|
||||
go
|
||||
create function dbo.to_integer(
|
||||
@code varchar(512)
|
||||
) returns integer as begin
|
||||
|
||||
declare @integer integer = 0
|
||||
declare @alphabet varchar(128)
|
||||
declare @base smallint
|
||||
declare @i integer = datalength(@code)
|
||||
|
||||
select top 1 @alphabet = alphabet, @base = base from dbo.Sets where in_use = 1
|
||||
|
||||
while @i > 0 begin
|
||||
|
||||
declare @value integer = charindex(substring(@code, @i, 1), @alphabet)
|
||||
|
||||
if @value != 0 set @integer = @integer * @base + @value - 1
|
||||
set @i = @i - 1
|
||||
|
||||
end
|
||||
|
||||
return @integer
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.to_binary', N'FN') is not null drop function dbo.to_binary
|
||||
go
|
||||
create function dbo.to_binary(
|
||||
@integer integer
|
||||
) returns varchar(32) as begin
|
||||
|
||||
declare @binary varchar(32) = ''
|
||||
|
||||
while @integer != 0 begin
|
||||
set @binary = convert(char(1), @integer % 2) + @binary
|
||||
set @integer = @integer / 2
|
||||
end
|
||||
|
||||
return @binary
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.to_string_binary', N'FN') is not null drop function dbo.to_string_binary
|
||||
go
|
||||
create function dbo.to_string_binary(
|
||||
@string varchar(512),
|
||||
@integer integer
|
||||
) returns varchar(3584) as begin
|
||||
|
||||
declare @binary varchar(3584) = ''
|
||||
declare @alphabet varchar(128)
|
||||
declare @base smallint
|
||||
declare @power tinyint
|
||||
|
||||
select top 1 @alphabet = alphabet, @base = base, @power = power from dbo.Sets where in_use = 1
|
||||
|
||||
if @string is not null begin
|
||||
|
||||
declare @i integer = datalength(@string)
|
||||
|
||||
while @i > 0 begin
|
||||
|
||||
declare @value integer = charindex(substring(@string, @i, 1), @alphabet)
|
||||
|
||||
if @value != 0 set @binary = right('0000000' + dbo.to_binary(@value - 1), @power) + @binary
|
||||
set @i = @i - 1
|
||||
|
||||
end
|
||||
|
||||
end else if @integer is not null begin
|
||||
set @binary = dbo.to_binary(@integer)
|
||||
while len(@binary) % @power != 0
|
||||
set @binary = '0' + @binary
|
||||
end
|
||||
|
||||
return @binary
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.get_bits', N'FN') is not null drop function dbo.get_bits
|
||||
go
|
||||
create function dbo.get_bits(
|
||||
@string varchar(512),
|
||||
@integer integer
|
||||
) returns smallint as begin
|
||||
if @string is not null begin
|
||||
|
||||
declare @alphabet varchar(128)
|
||||
declare @base smallint
|
||||
declare @power tinyint
|
||||
|
||||
select top 1 @alphabet = alphabet, @base = base, @power = power from dbo.Sets where in_use = 1
|
||||
|
||||
return (case
|
||||
when datalength(@string) = 0 then 0
|
||||
else (datalength(@string) - 1) * @power + ceiling(dbo.log2(charindex(substring(@string, datalength(@string), 1), @alphabet))) end)
|
||||
end
|
||||
if @integer is not null
|
||||
return ceiling(dbo.log2(@integer))
|
||||
return 0
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.get_from_bits', N'P') is not null drop procedure dbo.get_from_bits
|
||||
go
|
||||
create procedure dbo.get_from_bits(
|
||||
@string varchar(512),
|
||||
@integer integer,
|
||||
@from smallint output,
|
||||
@bits smallint output
|
||||
) begin set nocount on
|
||||
if @from < 0 begin
|
||||
set @from = dbo.get_bits(@string, @integer) + @from
|
||||
if @from < 0 set @from = 0
|
||||
end
|
||||
if @bits < 0 begin
|
||||
set @from = @from + @bits
|
||||
set @bits = -@bits
|
||||
if @from < 0 begin
|
||||
set @bits = @bits + @from
|
||||
set @from = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.clean', N'P') is not null drop procedure dbo.clean
|
||||
go
|
||||
create procedure dbo.clean(
|
||||
@string varchar(512) output,
|
||||
@integer integer output
|
||||
) as begin set nocount on
|
||||
if @string is not null begin
|
||||
|
||||
declare @character_0 char(1)
|
||||
|
||||
select top 1 @character_0 = substring(alphabet, 1, 1) from dbo.Sets where in_use = 1
|
||||
|
||||
while datalength(@string) != 0 and right(@string, 1) = @character_0
|
||||
set @string = left(@string, datalength(@string) - 1)
|
||||
if @string = '' set @string = @character_0
|
||||
|
||||
end
|
||||
end
|
||||
go
|
||||
|
||||
if object_id(N'dbo.get_range', N'P') is not null drop procedure dbo.get_range
|
||||
go
|
||||
create procedure dbo.get_range(
|
||||
@string varchar(512) output,
|
||||
@integer integer output,
|
||||
@from smallint,
|
||||
@bits smallint
|
||||
) begin set nocount on
|
||||
if @string is not null begin
|
||||
|
||||
execute dbo.get_from_bits @string, @integer, @from output, @bits output
|
||||
|
||||
if bits = 0 set bits = dbo.get_bits(@string, @integer) - @from
|
||||
if bits > 0 begin
|
||||
|
||||
declare @alphabet varchar(128)
|
||||
declare @base smallint
|
||||
declare @power tinyint
|
||||
|
||||
select top 1 @alphabet = alphabet, @base = base, @power = power from dbo.Sets where in_use = 1
|
||||
|
||||
if @from > 0 begin
|
||||
|
||||
declare shift smallint = @from % @power
|
||||
declare mask smallint = ~-@base
|
||||
|
||||
set @string = substring(@string, @from / @power + 1, datalength(@string))
|
||||
if @shift != 0 and datalength(@string) > 0 begin
|
||||
|
||||
declare l integer = datalength(@string)
|
||||
declare i integer = 1
|
||||
|
||||
while i < l begin
|
||||
set @string = (
|
||||
left(@string, i - 1) +
|
||||
substring(@alphabet, ((
|
||||
dbo.shift(charindex(@alphabet, substring(@string, i, 1)), @shift) |
|
||||
dbo.shift(charindex(@alphabet, substring(@string, i + 1, 1)), @power - @shift)
|
||||
) & @mask) + 1, 1) +
|
||||
right(@string, l - i)
|
||||
)
|
||||
set i = i + 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
if @bits > 0 begin
|
||||
|
||||
declare @shift smallint = @bits % @power
|
||||
|
||||
set @string = left(@string, ceiling((@from + @bits) / @power))
|
||||
if @shift != 0 and datalength(@string) > 0 begin
|
||||
set @string = (
|
||||
substring(@string, 1, datalength(@string) - 1) +
|
||||
substring(@alphabet, (
|
||||
dbo.shift(charindex(@alphabet, right(@string, 1)), -@shift) & ~-power(2, @power - @shift)
|
||||
) + 1, 1)
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
execute dbo.clean @string output, @integer output
|
||||
|
||||
end else if @integer is not null begin
|
||||
|
||||
execute dbo.get_from_bits null, @integer, @from output, @bits output
|
||||
|
||||
if @from > 0 set @code = dbo.shift(@code, @from) & (dbo.shift(1, 31 - @from) - 1)
|
||||
if @bits > 0 || @bits < 31 begin
|
||||
if @from + @bits > 31 set @bits = 31 - @from
|
||||
set @integer = @integer & (dbo.shift(1, @bits) - 1)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
go
|
||||
@ -270,7 +270,8 @@ Namespace ErrorsManager
|
||||
|
||||
End If
|
||||
|
||||
ElseIf bits > 0 Then
|
||||
End If
|
||||
If bits > 0 Then
|
||||
|
||||
Dim shift As Byte = CByte(bits Mod power)
|
||||
|
||||
@ -375,7 +376,7 @@ Namespace ErrorsManager
|
||||
Dim last_hexa As Byte = hexas.Last() << shift
|
||||
|
||||
For i As Integer = hexas.Count() - 1 To 1 Step -1
|
||||
hexas(i) = CByte(((hexas(i) << shift) And mask) Or (hexas(i - 1) >> (power - shift)))
|
||||
hexas(i) = CByte(((hexas(i) << shift) Or (hexas(i - 1) >> (power - shift))) And mask)
|
||||
Next
|
||||
hexas(0) = CByte((hexas(0) << shift) And mask)
|
||||
|
||||
@ -427,7 +428,7 @@ Namespace ErrorsManager
|
||||
Next
|
||||
|
||||
If hexa_from = hexa_to Then
|
||||
hexas(hexa_to) = CByte(hexas(hexa_to) And (from_mask And to_mask))
|
||||
hexas(hexa_to) = CByte(hexas(hexa_to) And (from_mask Or to_mask))
|
||||
Else
|
||||
hexas(hexa_from) = CByte(hexas(hexa_from) And from_mask)
|
||||
If hexa_to < hexas.Count() Then hexas(hexa_to) = CByte(hexas(hexa_to) And to_mask)
|
||||
|
||||
@ -98,7 +98,7 @@ Namespace ErrorsManager
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << seed.Next(0, 16)))
|
||||
Dim shifted As New FullError(errors, 0)
|
||||
Dim unshifted As New FullError(errors, 0)
|
||||
Dim bitwise As Integer = 10 - seed.Next(0, 20)
|
||||
Dim bitwise As Integer = seed.Next(-10, 10)
|
||||
|
||||
Console.WriteLine($"BITWISE: {bitwise}")
|
||||
|
||||
@ -283,7 +283,7 @@ Namespace ErrorsManager
|
||||
set_string.array = errors.set([error].array, value._string, [from], bits)
|
||||
set_array.array = errors.set([error].array, value.array, [from], bits)
|
||||
|
||||
Console.WriteLine($"SET: from {[from]} bits {bits} to {value}")
|
||||
Console.WriteLine($"SET: from {[from]} bits {bits} to [{value._string}, {FullError.print(value.array)}, {value._integer}]")
|
||||
Console.WriteLine($"ERROR: {errors.to_string_binary([error]._integer)} - VALUE: {errors.to_string_binary(value._integer)}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(set_integer._integer)} - {errors.to_string_binary(set_integer._string)} - {errors.to_string_binary(set_integer.array)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(set_string._integer)} - {errors.to_string_binary(set_string._string)} - {errors.to_string_binary(set_string.array)}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user