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

Optimize code size for text-utils.ts #18262

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
51 changes: 27 additions & 24 deletions cocos/2d/utils/text-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
const MAX_CACHE_SIZE = 100;

interface ICacheNode {
key: string | null;
key: string;
value: number,
prev: ICacheNode | null,
next: ICacheNode | null
}

const pool = new js.Pool<ICacheNode>(2);
pool.get = function (): ICacheNode {

Check warning on line 68 in cocos/2d/utils/text-utils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
return this._get() || {
key: '',
value: 0,
Expand All @@ -78,14 +78,14 @@
private count = 0;
private limit = 0;
private datas: Record<string, ICacheNode> = {};
private declare head;
private declare tail;
private head: ICacheNode | null = null;
private tail: ICacheNode | null = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use declare?

Copy link
Contributor Author

@dumganhar dumganhar Feb 5, 2025

Choose a reason for hiding this comment

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

Because head and tail are not assigned in constructor, so we need to initialize them to null while definition.


constructor (size) {
constructor (size: number) {
this.limit = size;
}

public moveToHead (node): void {
public moveToHead (node: ICacheNode): void {
node.next = this.head;
node.prev = null;
if (this.head) this.head.prev = node;
Expand All @@ -95,25 +95,25 @@
this.datas[node.key] = node;
}

public put (key, value): void {
public put (key: string, value: number): void {
const node = pool.get();
node!.key = key;
node!.value = value;

if (this.count >= this.limit) {
const discard = this.tail;
delete this.datas[discard.key];
delete this.datas[discard!.key];
this.count--;
this.tail = discard.prev;
this.tail.next = null;
discard.prev = null;
discard.next = null;
pool.put(discard);
this.tail = discard!.prev;
this.tail!.next = null;
discard!.prev = null;
discard!.next = null;
pool.put(discard!);
}
this.moveToHead(node);
this.moveToHead(node!);
}

public remove (node): void {
public remove (node: ICacheNode): void {
if (node.prev) {
node.prev.next = node.next;
} else {
Expand All @@ -128,7 +128,7 @@
this.count--;
}

public get (key): number | null {
public get (key: string): number | null {
const node = this.datas[key];
if (node) {
this.remove(node);
Expand All @@ -145,11 +145,11 @@
this.tail = null;
}

public has (key): boolean {
public has (key: string): boolean {
return !!this.datas[key];
}

public delete (key): void {
public delete (key: string): void {
const node = this.datas[key];
this.remove(node);
}
Expand All @@ -160,9 +160,12 @@
const WORD_REG = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûа-яА-ЯЁё]+|\S)/;
// eslint-disable-next-line no-useless-escape
const SYMBOL_REG = /^[!,.:;'}\]%\?>、‘“》?。,!]/;
const LAST_WORD_REG = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłę湯ŹŚÓŃŁĘĆĄ-яА-ЯЁёáàảạãăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệiíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢẠÃĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆIÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ]+|\S)$/;
const LAST_ENGLISH_REG = /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłę湯ŹŚÓŃŁĘĆĄ-яА-ЯЁёáàảạãăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệiíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢẠÃĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆIÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ]+$/;
const FIRST_ENGLISH_REG = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłę湯ŹŚÓŃŁĘĆĄ-яА-ЯЁёáàảạãăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệiíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢẠÃĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆIÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ]/;

const CHAR_SET = '[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłę湯ŹŚÓŃŁĘĆĄ-яА-ЯЁёáàảạãăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệiíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢẠÃĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆIÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ]';

Check warning on line 164 in cocos/2d/utils/text-utils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 269. Maximum allowed is 150
const LAST_WORD_REG = new RegExp(`(${CHAR_SET}+|\\S)$`);
const LAST_ENGLISH_REG = new RegExp(`${CHAR_SET}+$`);
const FIRST_ENGLISH_REG = new RegExp(`^${CHAR_SET}`);

const WRAP_INSPECTION = true;
// The unicode standard will never assign a character from code point 0xD800 to 0xDFFF
// high surrogate (0xD800-0xDBFF) and low surrogate(0xDC00-0xDFFF) combines to a character on the Supplementary Multilingual Plane
Expand Down Expand Up @@ -299,7 +302,7 @@
return `${charCodes}`;
}

function getSymbolStartIndex (targetString, index): number {
function getSymbolStartIndex (targetString: string, index: number): number {
if (index >= targetString.length) {
return targetString.length;
}
Expand Down Expand Up @@ -332,7 +335,7 @@
return startCheckIndex;
}

function getSymbolEndIndex (targetString, index): number {
function getSymbolEndIndex (targetString: string, index: number): number {
let newEndIndex = index;
let endCheckIndex = index;
let endChar = targetString[endCheckIndex];
Expand Down Expand Up @@ -379,7 +382,7 @@
// _safeSubstring(a, 0, 4) === '😉🚗'
// _safeSubstring(a, 0, 1) === _safeSubstring(a, 0, 2) === '😉'
// _safeSubstring(a, 2, 3) === _safeSubstring(a, 2, 4) === '🚗'
function _safeSubstring (targetString, startIndex, endIndex?): string {
function _safeSubstring (targetString: string, startIndex: number, endIndex?: number): string {
let newStartIndex = getSymbolStartIndex(targetString, startIndex);
if (newStartIndex < startIndex) {
newStartIndex = getSymbolEndIndex(targetString, startIndex) + 1;
Expand All @@ -396,7 +399,7 @@
newEndIndex += 1;
}
}
return targetString.substring(newStartIndex, newEndIndex) as string;
return targetString.substring(newStartIndex, newEndIndex);
}

/**
Expand Down