Skip to content

Instantly share code, notes, and snippets.

@Alexintosh
Forked from frangio/ShortString.sol
Created February 21, 2022 19:01

Revisions

  1. @frangio frangio revised this gist Dec 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ShortString.sol
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    pragma solidity ^0.8.9;

    // Unlike the string type, ShortString is a value type that can be made immutable.
    // It supports strings of at most 32 bytes and assumes they are null-terminated.
    // It supports strings of at most 32 bytes and assumes they don't contain null bytes.

    type ShortString is bytes32;

  2. @frangio frangio revised this gist Dec 29, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ShortString.sol
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;

    // Unlike the string type, ShortString is a value type that can be made immutable.
    // It supports strings of at most 32 bytes and assumes they are null-terminated.

    type ShortString is bytes32;

    error StringTooLong(string s);
  3. @frangio frangio revised this gist Dec 29, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ShortString.sol
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;

    error StringTooLong(string s);

    type ShortString is bytes32;

    error StringTooLong(string s);

    function toShortString(string memory s) pure returns (ShortString) {
    bytes memory b = bytes(s);
    if (b.length > 32) {
  4. @frangio frangio created this gist Dec 29, 2021.
    36 changes: 36 additions & 0 deletions ShortString.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;

    error StringTooLong(string s);

    type ShortString is bytes32;

    function toShortString(string memory s) pure returns (ShortString) {
    bytes memory b = bytes(s);
    if (b.length > 32) {
    revert StringTooLong(s);
    }
    return ShortString.wrap(bytes32(b));
    }

    function shortStringLength(ShortString s) pure returns (uint) {
    uint x = uint(ShortString.unwrap(s));
    uint len = 0;
    for (uint m = 0xff << 248; m != 0; m >>= 8) {
    if (x & m == 0) {
    break;
    }
    len += 1;
    }
    return len;
    }

    function toString(ShortString s) pure returns (string memory) {
    bytes memory b = new bytes(32);
    uint len = shortStringLength(s);
    assembly {
    mstore(b, len)
    mstore(add(b, 32), s)
    }
    return string(b);
    }
    16 changes: 16 additions & 0 deletions TestShortString.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.9;

    import './ShortString.sol';

    contract TestShortString {
    ShortString immutable $name;

    constructor(string memory _name) {
    $name = toShortString(_name);
    }

    function name() public view returns (string memory) {
    return toString($name);
    }
    }