0

I have next code :

struct Data
{
    uint id;
    
    string name;
    string Surname
    string moreInfo;
  
}
mapping(address => Data[]) private ownerauto;
address[] private owners
string[] private linksStorage;

function setdata(address _acc, string[] memory _data) external 
{
    requireData(_data)==false,"Data is empty!!!");
    Data memory autom;
    autom.id = ownerauto[_acc].length;
    autom.name = _data[0];
    autom.Surname = _data[1];
    autom.moreInfo = _data[2];
    linksStorage.push(_data[3]);

   /*here other code*/
    owners.push(_acc);
}

function getAllData() external view  returns(string[] memory)
{     
    uint index = 0;
    uint countDiag = 0;

    for(uint i=0;i<docs.length;i++)
    {
        address own = owners[i];
        countDiag +=ownerauto[own ].length;
    }

    string[] memory data = new string[](countDiag+2);
    for(uint i =0;i<owners.length;i++)
    {
        address own = owners[i];
        for(uint j=0;j<doctor_diagnosis[own].length;j++)
        {    
            data[index] = _getDiagnos(ownerauto[own ][j]);
            assembly
            {
                index:=add(index,0x1)
            }
            
        }
    }
    data[index] = _getLinksIPFS();
    return data;
}
function _getDiagnos(Data memory _own) private pure returns(string memory)
{
    string memory id = _own.id.uintTostring();
    
    string memory names = _own.name;
    string memory Surname = _own.Surname;
    string memory infoDiag = _own.moreInfo;
  
    string memory all = "";
    all = string(abi.encodePacked("id:" , id , "/name:" , name , "/surname :" ,
                Surname , "/i:", infoDiag ,";"));

    return all;
}
 function _getLinksIPFS() private view returns(string memory)
{
    /*code get string, it's ok */
}


function uintTostring(uint _number) internal pure returns (string memory _uintAsString) 
{
    bytes memory curNumber;
    if(_number==0)
        return "0";
    assembly
    {
        function calculateLenBytes(_num) -> len 
        {
            let num := _num
            for { } gt(num,0) {}
            {
                len:=add(len,0x1)
                num:= div(num,0xA)
            }
        }
        
        let length := calculateLenBytes(_number)
        let indexByte := length
        let temp
        curNumber := mload(length)
        for { } gt(_number,0) {}
        { 
            temp := add(  0x30, sub(_number, mul(div(_number,0xA),0xA)))

            mstore(add(curNumber,indexByte),temp)
            indexByte := sub(indexByte,0x1)
            _number:= div(_number,0xA)
        }
        mstore(curNumber,length)
    }

    return string(curNumber);
}

And i have problem, my function uintTostring using near 25000 gas, and increases slightly with large numbers, but don't work with my contract,it eats up all the gas with an array of length greater than one. But , if using it function without assembly, that working,but eat near 30000 gas and up on the 1000 and more (# example 99977799999 - 24625, vs 39752) Help me figure it out, I know that the problem is in the unitTostring function, but I want to use a little gas, how to translate it into assembly correctly then, or in this case it is impossible?

1
  • uintTostring in library
    – Kilo Arms
    Nov 27, 2022 at 20:55

1 Answer 1

0

Right function:

function uintTostring(uint _number) internal pure returns (string memory _uintAsString) 
{
    bytes memory curNumber = new bytes(32);
    if(_number==0)
        return "0";
    assembly
    {
        function calculateLenBytes(_num) -> len 
        {
            let num := _num
            for { } gt(num,0) {}
            {
                len:=add(len,0x1)
                num:= div(num,0xA)
            }
        }
        
        let length := calculateLenBytes(_number)
        let indexByte := length
        let temp
        //curNumber := mload(length)
        for { } gt(_number,0) {}
        { 
            temp := add(  0x30, sub(_number, mul(div(_number,0xA),0xA)))

            mstore(add(curNumber,indexByte),temp)
            indexByte := sub(indexByte,0x1)
            _number:= div(_number,0xA)
        }
        mstore(curNumber,length)
    }

    return string(curNumber);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.