AddSub

Description:

Smart contract deployed on Ethereum with Factory features.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "contracts/AddSub.sol": {
      "content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract AddSub {
    /// @notice Adds two unsigned integers, returns (sum, error).
    function adder(uint _a, uint _b) public pure returns (uint sum, bool error) {
        unchecked {
            sum = _a + _b;
        }
        // Overflow occurred if result is less than one operand
        if (sum < _a) {
            return (0, true);
        }
        return (sum, false);
    }

    /// @notice Subtracts two unsigned integers, returns (difference, error).
    function subtractor(uint _a, uint _b) public pure returns (uint difference, bool error) {
        if (_a < _b) {
            return (0, true);
        }
        unchecked {
            difference = _a - _b;
        }
        return (difference, false);
    }

    /// @notice Parse two uints from a string (extracts the first two digit sequences).
    /// @dev Accepts any non-negative integer sequences; ignores non-digit characters.
    function _parseTwoUint(string calldata s) internal pure returns (bool ok, uint a, uint b) {
        bytes calldata bs = bytes(s);
        uint len = bs.length;
        uint num = 0;
        uint found = 0;
        bool inNum = false;

        for (uint i = 0; i < len; i++) {
            bytes1 c = bs[i];
            if (c >= 0x30 && c <= 0x39) { // '0'..'9'
                uint digit = uint8(c) - 48;
                // check overflow for num = num * 10 + digit
                if (num > (type(uint).max - digit) / 10) {
                    return (false, 0, 0); // parse overflow
                }
                num = num * 10 + digit;
                inNum = true;
            } else {
                if (inNum) {
                    if (found == 0) {
                        a = num;
                    } else if (found == 1) {
                        b = num;
                    }
                    found++;
                    num = 0;
                    inNum = false;
                    if (found == 2) {
                        return (true, a, b);
                    }
                }
            }
        }

        // If ended while in a number, push that number
        if (inNum) {
            if (found == 0) {
                a = num;
                found++;
            } else if (found == 1) {
                b = num;
                found++;
            }
        }

        if (found == 2) {
            return (true, a, b);
        } else {
            return (false, 0, 0);
        }
    }

    /// @notice Converts a uint to its decimal string representation.
    function _uintToString(uint v) internal pure returns (string memory) {
        if (v == 0) {
            return "0";
        }
        uint temp = v;
        uint digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (v != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + (v % 10)));
            v /= 10;
        }
        return string(buffer);
    }

    /// @notice Public function: pass a string containing two numbers; get a friendly result message.
    /// @dev Returns: "sum = <number|overflow> and diff = <number|underflow>, have a nice day"
    function resultsFromString(string calldata input) external pure returns (string memory) {
        (bool parsed, uint a, uint b) = _parseTwoUint(input);
        if (!parsed) {
            return "Invalid input. Provide two non-negative integers in the string.";
        }

        (uint sum, bool sumErr) = adder(a, b);
        (uint diff, bool diffErr) = subtractor(a, b);

        string memory sumStr = sumErr ? "overflow" : _uintToString(sum);
        string memory diffStr = diffErr ? "underflow" : _uintToString(diff);

        return string(
            abi.encodePacked("sum = ", sumStr, " and diff = ", diffStr, ", have a nice day")
        );
    }
}
"
    }
  },
  "settings": {
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "remappings": [],
    "evmVersion": "cancun"
  }
}}

Tags:
Factory|addr:0xc85cac9c73bcaf2f012de9112ed1c070ec1ae45c|verified:true|block:23547279|tx:0x68cd502eb689c02036c5b47cfdee45a6ac9a2ec10d4a1d6d2da58cad54b00fb5|first_check:1760099505

Submitted on: 2025-10-10 14:31:45

Comments

Log in to comment.

No comments yet.