calculatePixelWidth can be optimized as bytes(text).length can be very large and we have only one "It is further assumed that there is no other semicolon in text
".
function calculatePixelWidth(string memory text, bool largeFont) internal pure returns (uint256 width) {
uint256 length = bytes(text).length;
if (length == 0) {
return 0;
}
unchecked {
uint256 charWidth = largeFont ? 16 : 13;
uint256 semicolonIndex;
for (uint256 i = 0; i < length; ++i) {
if (bytes(text)[i] == ";") {
semicolonIndex = i;
}
width += charWidth;
}
// Account for escaped characters (such as ≥).
@>> width -= charWidth * semicolonIndex;
}
}
code is optimized as we are not going through the whole array.
function calculatePixelWidth(string memory text, bool largeFont) internal pure returns (uint256 width) {
uint256 length = bytes(text).length;
if (length == 0) {
return 0;
}
unchecked {
uint256 charWidth = largeFont ? 16 : 13;
uint256 semicolonIndex;
for (uint256 i = 0; i < length; ++i) {
if (bytes(text)[i] == ";") {
semicolonIndex = i;
break;
}
}
// Account for escaped characters (such as ≥).
width =(length-i)*charWidth;
}
}
https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.