-
Notifications
You must be signed in to change notification settings - Fork 2
format #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
format #26
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc96706
format
thedavidmeister d18cd62
lint pragma
thedavidmeister 3468862
lint
thedavidmeister 13ab1cd
Update test/src/lib/format/LibFormatDecimalFloat.t.sol
thedavidmeister 072396a
ignore deps
thedavidmeister f48e553
Merge branch '2025-04-07-format' of github.com:rainlanguage/rain.math…
thedavidmeister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule rain.math.fixedpoint
added at
6f8f7f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "detectors_to_exclude": "unused-imports,solc-version,dead-code,different-pragma-directives-are-used,assembly-usage,similar-names,naming-convention", | ||
| "filter_paths": "test/,lib/forge-std" | ||
| "filter_paths": "test/,lib/forge-std,lib/rain.math.fixedpoint,openzeppelin-contracts" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // SPDX-License-Identifier: LicenseRef-DCL-1.0 | ||
| // SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister | ||
| pragma solidity ^0.8.25; | ||
|
|
||
| import {LibDecimalFloat, Float} from "../LibDecimalFloat.sol"; | ||
|
|
||
| import {LibFixedPointDecimalFormat} from "rain.math.fixedpoint/lib/format/LibFixedPointDecimalFormat.sol"; | ||
|
|
||
| library LibFormatDecimalFloat { | ||
| /// Format a decimal float as a string. | ||
| /// Currently is a thin wrapper around converting to a fixed point decimal | ||
| /// and then formatting that as a string. | ||
| /// In the future this may be extended to support a wider range of possible | ||
| /// values. | ||
| /// @param signedCoefficient The signed coefficient of the decimal float. | ||
| /// @param exponent The exponent of the decimal float. | ||
| /// @return The string representation of the decimal float. | ||
| function toDecimalString(int256 signedCoefficient, int256 exponent) internal pure returns (string memory) { | ||
| uint256 decimal18Value = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 18); | ||
| return LibFixedPointDecimalFormat.fixedPointToDecimalString(decimal18Value); | ||
| } | ||
|
|
||
| function toDecimalString(Float memory float) internal pure returns (string memory) { | ||
| return toDecimalString(float.signedCoefficient, float.exponent); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // SPDX-License-Identifier: LicenseRef-DCL-1.0 | ||
| // SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister | ||
| pragma solidity =0.8.25; | ||
|
|
||
| import {Test} from "forge-std/Test.sol"; | ||
| import {Float, LibDecimalFloat} from "src/lib/LibDecimalFloat.sol"; | ||
| import {LibFormatDecimalFloat} from "src/lib/format/LibFormatDecimalFloat.sol"; | ||
| import {LibParseDecimalFloat} from "src/lib/parse/LibParseDecimalFloat.sol"; | ||
|
|
||
| /// @title LibFormatDecimalFloatTest | ||
| /// @notice Test contract for verifying the functionality of LibFormatDecimalFloat | ||
| /// @dev Tests both the stack and memory versions of formatting functions and round-trip conversions | ||
| contract LibFormatDecimalFloatTest is Test { | ||
| using LibDecimalFloat for Float; | ||
|
|
||
| function toDecimalStringExternal(int256 signedCoefficient, int256 exponent) external pure returns (string memory) { | ||
| return LibFormatDecimalFloat.toDecimalString(signedCoefficient, exponent); | ||
| } | ||
|
|
||
| function toString(Float memory float) external pure returns (string memory) { | ||
| return LibFormatDecimalFloat.toDecimalString(float); | ||
| } | ||
|
|
||
| /// Check that the memory version matches the stack version. | ||
| function testFormatMem(Float memory float) external { | ||
| try this.toDecimalStringExternal(float.signedCoefficient, float.exponent) returns (string memory formatted) { | ||
| string memory actual = this.toString(float); | ||
| assertEq(formatted, actual, "Formatted value mismatch"); | ||
| } catch (bytes memory err) { | ||
| vm.expectRevert(err); | ||
| LibFormatDecimalFloat.toDecimalString(float); | ||
| } | ||
| } | ||
|
|
||
| /// Test round tripping a value through parse and format. | ||
| function testRoundTrip(uint256 value) external pure { | ||
| // Dividing by 10 here keeps us clearly within the range of lossless | ||
| // conversions. | ||
| value = bound(value, 0, type(uint256).max / 10); | ||
| Float memory float = LibDecimalFloat.fromFixedDecimalLosslessMem(value, 18); | ||
| string memory formatted = LibFormatDecimalFloat.toDecimalString(float); | ||
| (bytes4 errorCode, Float memory parsed) = LibParseDecimalFloat.parseDecimalFloat(formatted); | ||
| assertEq(errorCode, 0, "Parse error"); | ||
| assertTrue(float.eq(parsed), "Round trip failed"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.