Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: github/CopilotForXcode
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: AICREATEIT/CopilotForXcode
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Jan 27, 2025

  1. main

    // SPDX-License-Identifier: MIT
    pragma solidity >=0.8.0;
    
    /// @notice Efficient library for creating string representations of integers.
    /// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/LibString.sol)
    /// @author Modified from Solady (https://github.com/Vectorized/solady/blob/main/src/utils/LibString.sol)
    library LibString {
        function toString(int256 value) internal pure returns (string memory str) {
            if (value >= 0) return toString(uint256(value));
    
            unchecked {
                str = toString(uint256(-value));
    
                /// @solidity memory-safe-assembly
                assembly {
                    // Note: This is only safe because we over-allocate memory
                    // and write the string from right to left in toString(uint256),
                    // and thus can be sure that sub(str, 1) is an unused memory location.
    
                    let length := mload(str) // Load the string length.
                    // Put the - character at the start of the string contents.
                    mstore(str, 45) // 45 is the ASCII code for the - character.
                    str := sub(str, 1) // Move back the string pointer by a byte.
                    mstore(str, add(length, 1)) // Update the string length.
                }
            }
        }
    
        function toString(uint256 value) internal pure returns (string memory str) {
            /// @solidity memory-safe-assembly
            assembly {
                // The maximum value of a uint256 contains 78 digits (1 byte per digit), but we allocate 160 bytes
                // to keep the free memory pointer word aligned. We'll need 1 word for the length, 1 word for the
                // trailing zeros padding, and 3 other words for a max of 78 digits. In total: 5 * 32 = 160 bytes.
                let newFreeMemoryPointer := add(mload(0x40), 160)
    
                // Update the free memory pointer to avoid overriding our string.
                mstore(0x40, newFreeMemoryPointer)
    
                // Assign str to the end of the zone of newly allocated memory.
                str := sub(newFreeMemoryPointer, 32)
    
                // Clean the last word of memory it may not be overwritten.
                mstore(str, 0)
    
                // Cache the end of the memory to calculate the length later.
                let end := str
    
                // We write the string from rightmost digit to leftmost digit.
                // The following is essentially a do-while loop that also handles the zero case.
                // prettier-ignore
                for { let temp := value } 1 {} {
                    // Move the pointer 1 byte to the left.
                    str := sub(str, 1)
    
                    // Write the character to the pointer.
                    // The ASCII index of the '0' character is 48.
                    mstore8(str, add(48, mod(temp, 10)))
    
                    // Keep dividing temp until zero.
                    temp := div(temp, 10)
    
                     // prettier-ignore
                    if iszero(temp) { break }
                }
    
                // Compute and cache the final total length of the string.
                let length := sub(end, str)
    
                // Move the pointer 32 bytes leftwards to make room for the length.
                str := sub(str, 32)
    
                // Store the string's length at the start of memory allocated for our string.
                mstore(str, length)
            }
        }
    }
    AICREATEIT authored Jan 27, 2025
    Configuration menu
    Copy the full SHA
    7961644 View commit details
    Browse the repository at this point in the history
Loading