Skip to content

Optimize code size for text-utils.ts - #18262

Merged
minggo merged 2 commits into
cocos:v3.8.6from
dumganhar:386-opt-size-text-utils
Feb 5, 2025
Merged

Optimize code size for text-utils.ts#18262
minggo merged 2 commits into
cocos:v3.8.6from
dumganhar:386-opt-size-text-utils

Conversation

@dumganhar

@dumganhar dumganhar commented Jan 29, 2025

Copy link
Copy Markdown
Contributor

Re: #18222

截屏2025-01-29 23 08 22
function regexEqual(regex1, regex2) {
    return regex1.source === regex2.source && regex1.flags === regex2.flags;
}


LAST_WORD_REG = /([a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłęćąŻŹŚÓŃŁĘĆĄ-яА-ЯЁёáàãăâéèêếiíìĩóòõôơúùũưýđÁÀÃĂÂÉÈÊIÍÌĨÓÒÕÔƠÚÙŨƯÝĐ]+|\S)$/;
LAST_ENGLISH_REG = /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłęćąŻŹŚÓŃŁĘĆĄ-яА-ЯЁёáàãăâéèêếiíìĩóòõôơúùũưýđÁÀÃĂÂÉÈÊIÍÌĨÓÒÕÔƠÚÙŨƯÝĐ]+$/;
FIRST_ENGLISH_REG = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłęćąŻŹŚÓŃŁĘĆĄ-яА-ЯЁёáàãăâéèêếiíìĩóòõôơúùũưýđÁÀÃĂÂÉÈÊIÍÌĨÓÒÕÔƠÚÙŨƯÝĐ]/;

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


regexEqual(LAST_WORD_REG, LAST_WORD_REG2);
regexEqual(LAST_ENGLISH_REG, LAST_ENGLISH_REG2);
regexEqual(FIRST_ENGLISH_REG, FIRST_ENGLISH_REG2);

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@github-actions

github-actions Bot commented Jan 29, 2025

Copy link
Copy Markdown

👍 Package size ⤵ -1062 bytes, old: 5225108, new: 5224046

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -3959,16 +3959,16 @@
     export function fragmentText(stringToken: string, allWidth: number, maxWidth: number, measureText: (string: string) => number): string[];
     export const BASELINE_RATIO = 0.26;
     export const MIDDLE_RATIO: number;
     export class LRUCache {
-        constructor(size: any);
-        moveToHead(node: any): void;
-        put(key: any, value: any): void;
-        remove(node: any): void;
-        get(key: any): number | null;
+        constructor(size: number);
+        moveToHead(node: __private._cocos_2d_utils_text_utils__ICacheNode): void;
+        put(key: string, value: number): void;
+        remove(node: __private._cocos_2d_utils_text_utils__ICacheNode): void;
+        get(key: string): number | null;
         clear(): void;
-        has(key: any): boolean;
-        delete(key: any): void;
+        has(key: string): boolean;
+        delete(key: string): void;
     }
     /**
      * @en The dynamic atlas manager which manages all runtime dynamic packed atlas texture for UI rendering.
      * It generates a maximum of [[maxAtlasCount]] atlas texture, all atlas texture have the size of [[textureSize]].
@@ -61932,8 +61932,14 @@
              * @param {Number} y
              */
             drawTextureAt(image: ImageAsset, x: number, y: number): void;
         }
+        export interface _cocos_2d_utils_text_utils__ICacheNode {
+            key: string;
+            value: number;
+            prev: _cocos_2d_utils_text_utils__ICacheNode | null;
+            next: _cocos_2d_utils_text_utils__ICacheNode | null;
+        }
         /**
          * @en The instance of [[MorphRendering]] for dedicated control in the mesh renderer.
          * The root [[MorphRendering]] is owned by [[Mesh]] asset, each [[MeshRenderer]] can have its own morph rendering instance.
          * @zh 用于网格渲染器中独立控制 [[MorphRendering]] 的实例。原始 [[MorphRendering]] 被 [[Mesh]] 资源持有,每个 [[MeshRenderer]] 都持有自己的形变网格渲染实例。

@dumganhar
dumganhar requested a review from minggo February 5, 2025 05:59
private declare head;
private declare tail;
private head: ICacheNode | null = null;
private tail: ICacheNode | null = null;

Copy link
Copy Markdown
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?

@dumganhar dumganhar Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

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.

@minggo
minggo merged commit bc0a2b4 into cocos:v3.8.6 Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants