solidity学习-17-library

在我这里看来library的本质和rust中的crate差不多,python中的库,都是差不多的东西。
这玩意可以下载吗?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

library Strings {
    
    
    bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) public pure returns (string memory) {
    
    
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
    
    
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
    
    
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
    
    
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) public pure returns (string memory) {
    
    
        if (value == 0) {
    
    
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
    
    
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) public pure returns (string memory) {
    
    
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
    
    
            buffer[i] = _HEX_SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }
}


contract uselibrary{
    
    
    // Strings这个library需要显示的copy过来,也就是类似于手动抄一个,这点不同于正常的编程语言
    using Strings for uint256;

    function getstring(uint256 _number) public pure returns (string memory){
    
    
        return _number.toHexString();
    }

    function getstring2(uint256 _number) public pure returns (string memory){
    
    
        return Strings.toHexString(_number);
    }
}

remix无法提示自动补全啊?

猜你喜欢

转载自blog.csdn.net/qq_54384621/article/details/142208865