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

Optimized strings prefix judge: use startsWith instead of substring #3691

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src.ts/abi/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ export class ParamType {
return new ParamType(_guard, name || "", type, "array", indexed, null, arrayLength, arrayChildren);
}

if (type === "tuple" || type.substring(0, 5) === "tuple(" || type[0] === "(") {
if (type === "tuple" || type.startsWith("tuple(") || type[0] === "(") {
const comps = (obj.components != null) ? obj.components.map((c: any) => ParamType.from(c)): null;
const tuple = new ParamType(_guard, name || "", type, "tuple", indexed, comps, null, null);
// @TODO: use lexer to validate and normalize type
Expand Down
2 changes: 1 addition & 1 deletion src.ts/abi/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Typed {
}

isData(): this is TypedData {
return (this.type.substring(0, 5) === "bytes");
return (this.type.startsWith("bytes"));
}

isString(): this is TypedString {
Expand Down
2 changes: 1 addition & 1 deletion src.ts/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function getAddress(address: string): string {
if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {

// Missing the 0x prefix
if (address.substring(0, 2) !== "0x") { address = "0x" + address; }
if (!address.startsWith("0x")) { address = "0x" + address; }

const result = getChecksumAddress(address);

Expand Down
2 changes: 1 addition & 1 deletion src.ts/contract/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ContractFactory<A extends Array<any> = Array<any>, I = BaseContract
bytecode = hexlify(getBytes(bytecode));
} else {
if (typeof(bytecode) === "object") { bytecode = bytecode.object; }
if (bytecode.substring(0, 2) !== "0x") { bytecode = "0x" + bytecode; }
if (!bytecode.startsWith("0x")) { bytecode = "0x" + bytecode; }
bytecode = hexlify(getBytes(bytecode));
}

Expand Down
2 changes: 1 addition & 1 deletion src.ts/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function dataSlice(data: BytesLike, start?: number, end?: number): string
*/
export function stripZerosLeft(data: BytesLike): string {
let bytes = hexlify(data).substring(2);
while (bytes.substring(0, 2) == "00") { bytes = bytes.substring(2); }
while (bytes.startsWith("00")) { bytes = bytes.substring(2); }
return "0x" + bytes;
}

Expand Down
2 changes: 1 addition & 1 deletion src.ts/utils/maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function toBeArray(_value: BigNumberish): Uint8Array {
*/
export function toQuantity(value: BytesLike | BigNumberish): string {
let result = hexlify(isBytesLike(value) ? value: toBeArray(value)).substring(2);
while (result.substring(0, 1) === "0") { result = result.substring(1); }
while (result.startsWith("0")) { result = result.substring(1); }
if (result === "") { result = "0"; }
return "0x" + result;
}
2 changes: 1 addition & 1 deletion src.ts/wallet/json-keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function getAccount(data: any, _key: string): KeystoreAccount {
const address = computeAddress(privateKey);
if (data.address) {
let check = data.address.toLowerCase();
if (check.substring(0, 2) !== "0x") { check = "0x" + check; }
if (check.startsWith("0x")) { check = "0x" + check; }
Copy link
Member

Choose a reason for hiding this comment

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

This should be prefixed with !. But no worries, I caught it during merging. Just adding here for future observers. ;)

Thanks! :)


assertArgument(getAddress(check) === address, "keystore address/privateKey mismatch", "address", data.address);
}
Expand Down
2 changes: 1 addition & 1 deletion src.ts/wallet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../utils/index.js";

export function looseArrayify(hexString: string): Uint8Array {
if (typeof(hexString) === 'string' && hexString.substring(0, 2) !== '0x') {
if (typeof(hexString) === 'string' && !hexString.startsWith("0x")) {
hexString = '0x' + hexString;
}
return getBytesCopy(hexString);
Expand Down