From 629b7b17f047baf9a78cd4077c9ecb9c4be91b6b Mon Sep 17 00:00:00 2001 From: BoynChen Date: Fri, 27 Jan 2023 16:19:49 +0800 Subject: [PATCH] Optimized strings prefix judge: use startsWith instead of substring --- src.ts/abi/fragments.ts | 2 +- src.ts/abi/typed.ts | 2 +- src.ts/address/address.ts | 2 +- src.ts/contract/factory.ts | 2 +- src.ts/utils/data.ts | 2 +- src.ts/utils/maths.ts | 2 +- src.ts/wallet/json-keystore.ts | 2 +- src.ts/wallet/utils.ts | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src.ts/abi/fragments.ts b/src.ts/abi/fragments.ts index cecf5017bc..a2a220c364 100644 --- a/src.ts/abi/fragments.ts +++ b/src.ts/abi/fragments.ts @@ -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 diff --git a/src.ts/abi/typed.ts b/src.ts/abi/typed.ts index bdd46f9625..7c3f0b5ac5 100644 --- a/src.ts/abi/typed.ts +++ b/src.ts/abi/typed.ts @@ -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 { diff --git a/src.ts/address/address.ts b/src.ts/address/address.ts index 089de113e5..08af8da7ad 100644 --- a/src.ts/address/address.ts +++ b/src.ts/address/address.ts @@ -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); diff --git a/src.ts/contract/factory.ts b/src.ts/contract/factory.ts index 4b5da3398f..039223f23d 100644 --- a/src.ts/contract/factory.ts +++ b/src.ts/contract/factory.ts @@ -33,7 +33,7 @@ export class ContractFactory = Array, 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)); } diff --git a/src.ts/utils/data.ts b/src.ts/utils/data.ts index dd7b8d7de6..89c0c4c5d0 100644 --- a/src.ts/utils/data.ts +++ b/src.ts/utils/data.ts @@ -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; } diff --git a/src.ts/utils/maths.ts b/src.ts/utils/maths.ts index 18bff7fbe6..31c51b98bf 100644 --- a/src.ts/utils/maths.ts +++ b/src.ts/utils/maths.ts @@ -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; } diff --git a/src.ts/wallet/json-keystore.ts b/src.ts/wallet/json-keystore.ts index 80fe87026e..b9aea27446 100644 --- a/src.ts/wallet/json-keystore.ts +++ b/src.ts/wallet/json-keystore.ts @@ -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; } assertArgument(getAddress(check) === address, "keystore address/privateKey mismatch", "address", data.address); } diff --git a/src.ts/wallet/utils.ts b/src.ts/wallet/utils.ts index f718c7eee8..53b28e22b3 100644 --- a/src.ts/wallet/utils.ts +++ b/src.ts/wallet/utils.ts @@ -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);