Skip to content

Commit

Permalink
Merge pull request #304 from rainlanguage/2024-12-30-update-string
Browse files Browse the repository at this point in the history
update rain.string
  • Loading branch information
thedavidmeister authored Dec 30, 2024
2 parents 8c39203 + d2cb0d8 commit 2090d6a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 125 deletions.
75 changes: 0 additions & 75 deletions test/lib/literal/LibLiteralString.sol

This file was deleted.

16 changes: 8 additions & 8 deletions test/src/lib/parse/LibParse.literalString.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {LibParse} from "src/lib/parse/LibParse.sol";
import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol";
import {IntOrAString, LibIntOrAString} from "rain.intorastring/src/lib/LibIntOrAString.sol";
import {StringTooLong, UnclosedStringLiteral} from "src/error/ErrParse.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {LibParseState, ParseState} from "src/lib/parse/LibParseState.sol";

/// @title LibParseLiteralStringTest
Expand Down Expand Up @@ -58,7 +58,7 @@ contract LibParseLiteralStringTest is Test {
/// forge-config: default.fuzz.runs = 100
function testParseStringLiteralShortASCII(string memory str) external view {
vm.assume(bytes(str).length < 0x20);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);

(bytes memory bytecode, uint256[] memory constants) =
LibMetaFixture.newState(string.concat("_: \"", str, "\";")).parse();
Expand All @@ -79,9 +79,9 @@ contract LibParseLiteralStringTest is Test {
/// forge-config: default.fuzz.runs = 100
function testParseStringLiteralTwo(string memory strA, string memory strB) external view {
vm.assume(bytes(strA).length < 0x20);
LibLiteralString.conformValidPrintableStringContent(strA);
LibConformString.conformValidPrintableStringContent(strA);
vm.assume(bytes(strB).length < 0x20);
LibLiteralString.conformValidPrintableStringContent(strB);
LibConformString.conformValidPrintableStringContent(strB);
vm.assume(keccak256(bytes(strA)) != keccak256(bytes(strB)));

(bytes memory bytecode, uint256[] memory constants) =
Expand All @@ -104,7 +104,7 @@ contract LibParseLiteralStringTest is Test {
/// forge-config: default.fuzz.runs = 100
function testParseStringLiteralLongASCII(string memory str) external {
vm.assume(bytes(str).length >= 0x20);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);

vm.expectRevert(abi.encodeWithSelector(StringTooLong.selector, 3));
(bytes memory bytecode, uint256[] memory constants) = this.externalParse(string.concat("_: \"", str, "\";"));
Expand All @@ -116,7 +116,7 @@ contract LibParseLiteralStringTest is Test {
/// forge-config: default.fuzz.runs = 100
function testParseStringLiteralInvalidCharAfter(string memory strA, string memory strB) external {
vm.assume(bytes(strA).length >= 0x20);
LibLiteralString.conformValidPrintableStringContent(strA);
LibConformString.conformValidPrintableStringContent(strA);
assembly ("memory-safe") {
// Force truncate the string to 32 bytes.
mstore(strA, 0x20)
Expand All @@ -133,10 +133,10 @@ contract LibParseLiteralStringTest is Test {
/// forge-config: default.fuzz.runs = 100
function testParseStringLiteralInvalidCharWithin(string memory str, uint256 badIndex) external {
vm.assume(bytes(str).length > 0);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);
badIndex = bound(badIndex, 0, (bytes(str).length > 0x1F ? 0x1F : bytes(str).length) - 1);

LibLiteralString.corruptSingleChar(str, badIndex);
LibConformString.corruptSingleChar(str, badIndex);

vm.expectRevert(abi.encodeWithSelector(UnclosedStringLiteral.selector, 4 + badIndex));
(bytes memory bytecode, uint256[] memory constants) = this.externalParse(string.concat("_: \"", str, "\";"));
Expand Down
30 changes: 15 additions & 15 deletions test/src/lib/parse/LibParseOperand.parseOperand.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ParseState} from "src/lib/parse/LibParseState.sol";
import {LibBytes, Pointer} from "rain.solmem/lib/LibBytes.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {LibMetaFixture} from "test/lib/parse/LibMetaFixture.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {OperandValuesOverflow, UnclosedOperand} from "src/error/ErrParse.sol";
import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol";
import {LibDecimalFloat, LibDecimalFloatImplementation} from "rain.math.float/lib/LibDecimalFloat.sol";
Expand Down Expand Up @@ -72,8 +72,8 @@ contract LibParseOperandParseOperandTest is Test {
string memory maybeWhitespaceB,
string memory suffix
) external pure {
LibLiteralString.conformStringToWhitespace(maybeWhitespaceA);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceB);
LibConformString.conformStringToWhitespace(maybeWhitespaceA);
LibConformString.conformStringToWhitespace(maybeWhitespaceB);

value = bound(value, 0, SIGNED_NORMALIZED_MAX);

Expand Down Expand Up @@ -108,9 +108,9 @@ contract LibParseOperandParseOperandTest is Test {
valueA = bound(valueA, 0, SIGNED_NORMALIZED_MAX);
valueB = bound(valueB, 0, SIGNED_NORMALIZED_MAX);

LibLiteralString.conformStringToWhitespace(maybeWhitespaceA);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceB);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceC);
LibConformString.conformStringToWhitespace(maybeWhitespaceA);
LibConformString.conformStringToWhitespace(maybeWhitespaceB);
LibConformString.conformStringToWhitespace(maybeWhitespaceC);

string memory valueAString = asHexA ? uint256(valueA).toHexString() : valueA.toString();
string memory valueBString = asHexB ? uint256(valueB).toHexString() : valueB.toString();
Expand Down Expand Up @@ -156,10 +156,10 @@ contract LibParseOperandParseOperandTest is Test {
valueB = bound(valueB, 0, SIGNED_NORMALIZED_MAX);
valueC = bound(valueC, 0, SIGNED_NORMALIZED_MAX);

LibLiteralString.conformStringToWhitespace(maybeWhitespaceA);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceB);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceC);
LibLiteralString.conformStringToWhitespace(maybeWhitespaceD);
LibConformString.conformStringToWhitespace(maybeWhitespaceA);
LibConformString.conformStringToWhitespace(maybeWhitespaceB);
LibConformString.conformStringToWhitespace(maybeWhitespaceC);
LibConformString.conformStringToWhitespace(maybeWhitespaceD);

string memory s;
uint256 expectedLength;
Expand Down Expand Up @@ -207,11 +207,11 @@ contract LibParseOperandParseOperandTest is Test {
vm.assume(bytes(maybeWhitespace[1]).length > 0);
vm.assume(bytes(maybeWhitespace[2]).length > 0);
vm.assume(bytes(maybeWhitespace[3]).length > 0);
LibLiteralString.conformStringToWhitespace(maybeWhitespace[0]);
LibLiteralString.conformStringToWhitespace(maybeWhitespace[1]);
LibLiteralString.conformStringToWhitespace(maybeWhitespace[2]);
LibLiteralString.conformStringToWhitespace(maybeWhitespace[3]);
LibLiteralString.conformStringToWhitespace(maybeWhitespace[4]);
LibConformString.conformStringToWhitespace(maybeWhitespace[0]);
LibConformString.conformStringToWhitespace(maybeWhitespace[1]);
LibConformString.conformStringToWhitespace(maybeWhitespace[2]);
LibConformString.conformStringToWhitespace(maybeWhitespace[3]);
LibConformString.conformStringToWhitespace(maybeWhitespace[4]);
}

for (uint256 i = 0; i < 4; i++) {
Expand Down
18 changes: 9 additions & 9 deletions test/src/lib/parse/LibParsePragma.keyword.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
CMASK_INTERSTITIAL_HEAD,
CMASK_HEX
} from "rain.string/lib/parse/LibParseCMask.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {NoWhitespaceAfterUsingWordsFrom} from "src/error/ErrParse.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol";
Expand Down Expand Up @@ -85,7 +85,7 @@ contract LibParsePragmaKeywordTest is Test {
/// error.
/// forge-config: default.fuzz.runs = 100
function testPragmaKeywordNoWhitespace(uint256 seed, string memory str) external {
bytes1 notWhitespace = LibLiteralString.charFromMask(seed, ~CMASK_WHITESPACE);
bytes1 notWhitespace = LibConformString.charFromMask(seed, ~CMASK_WHITESPACE);
string memory fullString =
string.concat(string(PRAGMA_KEYWORD_BYTES), string(abi.encodePacked(notWhitespace)), str);
vm.expectRevert(abi.encodeWithSelector(NoWhitespaceAfterUsingWordsFrom.selector, PRAGMA_KEYWORD_BYTES_LENGTH));
Expand All @@ -98,8 +98,8 @@ contract LibParsePragmaKeywordTest is Test {
/// forge-config: default.fuzz.runs = 100
function testPragmaKeywordWhitespaceNoHex(uint256 seed, string calldata calldataStr) external pure {
seed = bound(seed, 0, type(uint256).max - 1);
bytes1 whitespace = LibLiteralString.charFromMask(seed, CMASK_WHITESPACE);
bytes1 notInterstitialHead = LibLiteralString.charFromMask(seed + 1, ~CMASK_INTERSTITIAL_HEAD);
bytes1 whitespace = LibConformString.charFromMask(seed, CMASK_WHITESPACE);
bytes1 notInterstitialHead = LibConformString.charFromMask(seed + 1, ~CMASK_INTERSTITIAL_HEAD);
if (bytes(calldataStr).length > 2) {
vm.assume(
keccak256(bytes(calldataStr[0:2]))
Expand Down Expand Up @@ -128,8 +128,8 @@ contract LibParsePragmaKeywordTest is Test {
string calldata suffix
) external pure {
vm.assume(bytes(whitespace).length > 0);
bytes1 notHexData = LibLiteralString.charFromMask(seed, ~CMASK_HEX);
LibLiteralString.conformStringToMask(whitespace, CMASK_WHITESPACE, 0x80);
bytes1 notHexData = LibConformString.charFromMask(seed, ~CMASK_HEX);
LibConformString.conformStringToMask(whitespace, CMASK_WHITESPACE, 0x80);
string memory str = string.concat(
string(PRAGMA_KEYWORD_BYTES),
whitespace,
Expand Down Expand Up @@ -169,10 +169,10 @@ contract LibParsePragmaKeywordTest is Test {
vm.assume(bytes(whitespace0).length > 0);
vm.assume(bytes(whitespace1).length > 0);

bytes1 notHexData = LibLiteralString.charFromMask(seed, ~CMASK_HEX);
bytes1 notHexData = LibConformString.charFromMask(seed, ~CMASK_HEX);

LibLiteralString.conformStringToMask(whitespace0, CMASK_WHITESPACE, 0x80);
LibLiteralString.conformStringToMask(whitespace1, CMASK_WHITESPACE, 0x80);
LibConformString.conformStringToMask(whitespace0, CMASK_WHITESPACE, 0x80);
LibConformString.conformStringToMask(whitespace1, CMASK_WHITESPACE, 0x80);

string memory str = string.concat(
string.concat(
Expand Down
6 changes: 3 additions & 3 deletions test/src/lib/parse/literal/LibParseLiteralHex.boundHex.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity =0.8.25;
import {ParseLiteralTest} from "test/abstract/ParseLiteralTest.sol";
import {ParseState} from "src/lib/parse/LibParseState.sol";
import {LibParseLiteral} from "src/lib/parse/literal/LibParseLiteral.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {CMASK_HEX} from "rain.string/lib/parse/LibParseCMask.sol";
import {LibParseLiteralHex} from "src/lib/parse/literal/LibParseLiteralHex.sol";

Expand Down Expand Up @@ -34,9 +34,9 @@ contract LibParseLiteralBoundLiteralHexTest is ParseLiteralTest {
external
pure
{
LibLiteralString.conformStringToHexDigits(str);
LibConformString.conformStringToHexDigits(str);
string memory delimString = string(abi.encodePacked(delimByte));
LibLiteralString.conformStringToMask(delimString, ~CMASK_HEX, 0x100);
LibConformString.conformStringToMask(delimString, ~CMASK_HEX, 0x100);
checkHexBounds(
bytes(string.concat("0x", str, delimString, anyOtherString)),
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity =0.8.25;
import {Test} from "forge-std/Test.sol";
import {LibBytes, Pointer} from "rain.solmem/lib/LibBytes.sol";
import {LibParseLiteralString} from "src/lib/parse/literal/LibParseLiteralString.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {StringTooLong, UnclosedStringLiteral, ParserOutOfBounds} from "src/error/ErrParse.sol";
import {LibParseState, ParseState} from "src/lib/parse/LibParseState.sol";
import {LibAllStandardOpsNP} from "src/lib/op/LibAllStandardOpsNP.sol";
Expand Down Expand Up @@ -59,15 +59,15 @@ contract LibParseLiteralStringBoundTest is Test {
/// of their quotes and the inner start and end at their data bounds.
function testParseStringLiteralBounds(string memory str) external view {
vm.assume(bytes(str).length < 0x20);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);

checkStringBounds(string.concat("\"", str, "\""), 1, bytes(str).length + 1, bytes(str).length + 2);
}

/// Valid but too long strings should error.
function testParseStringLiteralBoundsTooLong(string memory str) external {
vm.assume(bytes(str).length >= 0x20);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);

vm.expectRevert(abi.encodeWithSelector(StringTooLong.selector, 0));
checkStringBounds(string.concat("\"", str, "\""), 0, 0, 0);
Expand All @@ -76,9 +76,9 @@ contract LibParseLiteralStringBoundTest is Test {
/// Invalid chars in the first 31 bytes should error.
function testParseStringLiteralBoundsInvalidCharBefore(string memory str, uint256 badIndex) external {
vm.assume(bytes(str).length > 0);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);
badIndex = bound(badIndex, 0, (bytes(str).length > 0x1F ? 0x1F : bytes(str).length) - 1);
LibLiteralString.corruptSingleChar(str, badIndex);
LibConformString.corruptSingleChar(str, badIndex);

vm.expectRevert(abi.encodeWithSelector(UnclosedStringLiteral.selector, 1 + badIndex));
checkStringBounds(string.concat("\"", str, "\""), 0, 0, 0);
Expand All @@ -88,7 +88,7 @@ contract LibParseLiteralStringBoundTest is Test {
/// an unclosed string.
function testParseStringLiteralBoundsParserOutOfBounds(string memory str, uint256 length) external {
vm.assume(bytes(str).length < 0x20);
LibLiteralString.conformValidPrintableStringContent(str);
LibConformString.conformValidPrintableStringContent(str);
str = string.concat("\"", str, "\"");
length = bound(length, 1, bytes(str).length - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {IntOrAString, LibIntOrAString} from "rain.intorastring/src/lib/LibIntOrA
import {LibParseState, ParseState} from "src/lib/parse/LibParseState.sol";
import {LibAllStandardOpsNP} from "src/lib/op/LibAllStandardOpsNP.sol";
import {CMASK_STRING_LITERAL_TAIL} from "rain.string/lib/parse/LibParseCMask.sol";
import {LibLiteralString} from "test/lib/literal/LibLiteralString.sol";
import {LibConformString} from "rain.string/lib/mut/LibConformString.sol";
import {UnclosedStringLiteral} from "src/error/ErrParse.sol";

/// @title LibParseLiteralStringTest
Expand All @@ -30,7 +30,7 @@ contract LibParseLiteralStringTest is Test {

/// The parser will only accept strings that are valid according to the mask.
function testParseStringLiteralAny(bytes memory data) external pure {
LibLiteralString.conformStringToMask(string(data), CMASK_STRING_LITERAL_TAIL, 0x80);
LibConformString.conformStringToMask(string(data), CMASK_STRING_LITERAL_TAIL, 0x80);
vm.assume(data.length < 32);
ParseState memory state = LibParseState.newState(bytes(string.concat("\"", string(data), "\"")), "", "", "");

Expand All @@ -45,10 +45,10 @@ contract LibParseLiteralStringTest is Test {
/// If any character in the string is not in the mask, the parser will error.
function testParseStringLiteralCorrupt(bytes memory data, uint256 corruptIndex) external {
vm.assume(data.length > 0);
LibLiteralString.conformStringToMask(string(data), CMASK_STRING_LITERAL_TAIL, 0x80);
LibConformString.conformStringToMask(string(data), CMASK_STRING_LITERAL_TAIL, 0x80);
vm.assume(data.length < 32);
corruptIndex = bound(corruptIndex, 0, data.length - 1);
LibLiteralString.corruptSingleChar(string(data), corruptIndex);
LibConformString.corruptSingleChar(string(data), corruptIndex);

ParseState memory state = LibParseState.newState(bytes(string.concat("\"", string(data), "\"")), "", "", "");

Expand Down
Loading

0 comments on commit 2090d6a

Please sign in to comment.