Skip to content
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

ref: added functionality to escapeJSONstrings (ref: #5251) #5508

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ library Strings {

bytes16 private constant HEX_DIGITS = "0123456789abcdef";
uint8 private constant ADDRESS_LENGTH = 20;
uint256 constant SPECIAL_CHARS_LOOKUP =
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
(1 << 8) | // backspace
(1 << 9) | // tab
(1 << 10) | // newline
(1 << 12) | // form feed
(1 << 13) | // carriage return
(1 << 34) | // double quote
(1 << 47) | // forward slash
(1 << 92); // backslash
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @dev The `value` string doesn't fit in the specified `length`.
Expand Down Expand Up @@ -438,4 +447,42 @@ library Strings {
value := mload(add(buffer, add(0x20, offset)))
}
}

function _escapeJsonString(string memory input) private pure returns (string memory) {
bytes memory buffer = bytes(input);
bytes memory output = new bytes(buffer.length * 2); // Allocate max possible space
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried this allocated a lot of memory that is not freed. This is quite expensive. Alternatives are:

  • count number of "extra chars" in a loop, and allocate the right size directly
  • resize the output buffer without a copy, and move the free memory ptr, using assembly

uint256 outputLength = 0;

for (uint256 i; i < buffer.length; ) {
bytes1 char = buffer[i];

if ((SPECIAL_CHARS_LOOKUP & (1 << uint8(char))) != 0) {
output[outputLength++] = '\\';

if (char == 0x08) output[outputLength++] = 'b';
else if (char == 0x09) output[outputLength++] = 't';
else if (char == 0x0A) output[outputLength++] = 'n';
else if (char == 0x0C) output[outputLength++] = 'f';
else if (char == 0x0D) output[outputLength++] = 'r';
else if (char == 0x22) output[outputLength++] = '"';
else if (char == 0x2F) output[outputLength++] = '/';
else if (char == 0x5C) output[outputLength++] = '\\';
} else {
output[outputLength++] = char;
}

unchecked { ++i; }
DarkLord017 marked this conversation as resolved.
Show resolved Hide resolved
}

// Trim the output to the correct length
bytes memory finalOutput = new bytes(outputLength);
for (uint256 i = 0; i < outputLength; i++) {
finalOutput[i] = output[i];
}

return string(finalOutput);
}



}
Loading