Skip to content

Commit

Permalink
Optimize code size for text-utils.ts (#18262)
Browse files Browse the repository at this point in the history
* Optimize code size for text-utils.ts

* Fix text-utils.ts
  • Loading branch information
dumganhar authored Feb 5, 2025
1 parent 6d189bd commit bc0a2b4
Showing 1 changed file with 27 additions and 24 deletions.
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,7 +58,7 @@ export function getBaselineOffset (): number {
const MAX_CACHE_SIZE = 100;

interface ICacheNode {
key: string | null;
key: string;
value: number,
prev: ICacheNode | null,
next: ICacheNode | null
Expand All @@ -78,14 +78,14 @@ export class LRUCache {
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;

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 @@ export class LRUCache {
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 @@ export class LRUCache {
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 @@ export class LRUCache {
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 measureCache = new LRUCache(MAX_CACHE_SIZE);
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 @@ export function getSymbolCodeAt (str: string, index: number): string {
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 @@ function getSymbolStartIndex (targetString, index): number {
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 @@ function getSymbolEndIndex (targetString, index): number {
// _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 @@ function _safeSubstring (targetString, startIndex, endIndex?): string {
newEndIndex += 1;
}
}
return targetString.substring(newStartIndex, newEndIndex) as string;
return targetString.substring(newStartIndex, newEndIndex);
}

/**
Expand Down

0 comments on commit bc0a2b4

Please sign in to comment.