Skip to content

Commit

Permalink
use toTrimmedString inside of ResourceId.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudeZsb committed Dec 11, 2023
1 parent 4a0100e commit 9b4f570
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/world/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"file": "test/World.t.sol",
"test": "testRegisterRootFunctionSelector",
"name": "Register a root function selector",
"gasUsed": 80433
"gasUsed": 80428
},
{
"file": "test/World.t.sol",
Expand Down Expand Up @@ -172,5 +172,11 @@
"test": "testGetType",
"name": "get type from a resource ID",
"gasUsed": 4
},
{
"file": "test/WorldResourceId.t.sol",
"test": "testToString",
"name": "convert resource ID to string",
"gasUsed": 3287
}
]
4 changes: 2 additions & 2 deletions packages/world/src/WorldResourceId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ library WorldResourceIdInstance {
abi.encodePacked(
resourceType,
":",
resourceNamespace == bytes14("") ? bytes14("<root>") : resourceNamespace,
resourceNamespace == bytes14("") ? "<root>" : WorldResourceIdLib.toTrimmedString(resourceNamespace),
":",
resourceName == bytes16("") ? bytes16("<root>") : resourceName
resourceName == bytes16("") ? "<root>" : WorldResourceIdLib.toTrimmedString(resourceName)
)
);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/world/test/WorldResourceId.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ contract WorldResourceIdTest is Test, GasReporter {
assertEq(ResourceId.unwrap(namespaceId), ResourceId.unwrap(WorldResourceIdLib.encodeNamespace("namespace")));
}

function testToString() public {
ResourceId resourceId = WorldResourceIdLib.encode({
typeId: RESOURCE_SYSTEM,
namespace: "namespace",
name: "name"
});

startGasReport("convert resource ID to string");
string memory resourceIdString = resourceId.toString();
endGasReport();

assertEq(resourceIdString, "sy:namespace:name");
}

function testGetType() public {
ResourceId resourceId = WorldResourceIdLib.encode({
typeId: RESOURCE_SYSTEM,
Expand Down

0 comments on commit 9b4f570

Please sign in to comment.