DataStructure added

This commit is contained in:
Abdussamed 2024-10-26 19:13:22 +03:00
parent 385d40fe87
commit 3a5924d46c
1 changed files with 86 additions and 16 deletions

100
memory.js
View File

@ -1,7 +1,24 @@
class ProcessableData {
/**
* @param {Uint8Array} data
* @param {number} from
* @param {number} to
*/
constructor(data, from = 0, to){
if(data){
this.data = data;
if(data)
{
if(data instanceof ArrayBuffer)
{
this.data = new Uint8Array(data);
}else if(data instanceof Uint8Array)
{
this.data = data;
}else if(typeof data == "number")
{
this.data = new Uint8Array(data);
}else{
throw new Error("data is not type ArrayBuffer or Uint8Array");
}
this.init(from, to);
}
}
@ -256,7 +273,7 @@ class ConstructedData extends ProcessableData {
is4Byte,
is8Byte,
issigned,
255
0
);
if(isInt == false)
@ -483,6 +500,12 @@ class DataAnchor extends ConstructedData {
}
class DataStructure {
constructor(structure){
if(typeof structure == "string")
{
this.setHeadSigner(structure);
}
}
/** @type {boolean} */
sign = true;
/** @type {string} */
@ -494,6 +517,13 @@ class DataStructure {
/** @type {any} */
value = null;
size = 1;
reset(){
this.size = 1;
this.fixedLength = true;
this.list = true;
this.type = "uint8";
this.sign = true;
}
/** @param {DataAnchor} da */
setData(da){
let dataCost = da.getProcessor(this.type);
@ -534,11 +564,11 @@ class DataStructure {
}
/** @param {DataAnchor} da */
getData(da){
let result = null, sign;
let result = null;
if(this.sign == true)
{
// Verinin içine veri türüni işliyor
sign = da.readUInt8();
this.type = da.readUInt8();
}
let dataCost = da.getProcessor(this.type);
if(this.fixedLength == false && this.list == true)
@ -551,7 +581,7 @@ class DataStructure {
if(this.list == true)
{
result = [];
for (const data of this.value)
for (let t = 0;t < this.size; t++)
{
result.push(dataCost.read());
}
@ -562,13 +592,13 @@ class DataStructure {
if(this.list == true)
{
result = [];
for (const data of this.value)
for (let t = 0;t < this.size; t++)
{
let length = da.readVarNumber(data.length);
let length = da.readVarNumber();
result.push(dataCost.read(length));
}
}else{
let length = da.readVarNumber(data.length);
let length = da.readVarNumber();
result = dataCost.read(length);
}
}
@ -576,23 +606,63 @@ class DataStructure {
}
/** @param {string} dataSigner */
setHeadSigner(dataSigner){
let regex = /^(\w+)(\[\d*\])?$/.exec(dataSigner);
let regex = /^(\w+)(\?)(\[\d*\])?$/.exec(dataSigner);
if(regex[1] == 'any')
if(regex[1] != 'any')
{
this.type = regex[1];
}
if(regex[2] == '?')
{
this.sign = true;
}else{
this.sign = false;
this.type = regex[1];
}
if(regex[2])
if(regex[3])
{
this.list = true;
this.fixedLength = regex[2] != '[]'
this.size = regex[2].slice(1,-1) | 0;
this.fixedLength = regex[3] != '[]'
this.size = regex[3].slice(1,-1) | 0;
}else{
this.list = false;
}
}
getHeadSigner(){
let t = [];
if(this.sign)
{
t.push('any')
}else{
t.push(this.type)
}
if(this.list)
{
if(this.fixedLength == false)
{
t.push('[]')
}else{
t.push('[', this.size,']')
}
}
return t.join('')
}
static ds = new DataStructure();
/**
* @param {string} sign
*/
static settle(da, sign, value, size)
{
this.ds.setHeadSigner(sign);
this.ds.value = value;
typeof size == "number" && (this.ds.size = size);
this.ds.setData(da);
this.ds.reset();
}
static gettle(da, sign)
{
this.ds.setHeadSigner(sign);
return this.ds.getData(da);
}
}