@cjser/string-width 8.2.2-cjser.2 → 8.3.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  ));
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
 
29
- // packages/@cjser/string-width.tmp-26-1783614088733/index.js
29
+ // packages/@cjser/string-width.tmp-26-1790266896938/index.js
30
30
  var index_exports = {};
31
31
  __export(index_exports, {
32
32
  default: () => stringWidth
@@ -35,8 +35,7 @@ module.exports = __toCommonJS(index_exports);
35
35
  var import_strip_ansi = __toESM(require("@cjser/strip-ansi"), 1);
36
36
  var import_get_east_asian_width = require("@cjser/get-east-asian-width");
37
37
  var segmenter = new Intl.Segmenter();
38
- var zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Format}|\\p{Nonspacing_Mark}|\\p{Enclosing_Mark}|\\p{Surrogate})+$", "v");
39
- var leadingNonPrintingRegex = new RegExp("^[\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Nonspacing_Mark}\\p{Enclosing_Mark}\\p{Surrogate}]+", "v");
38
+ var visibleCharacterRegex = new RegExp("[^\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Nonspacing_Mark}\\p{Enclosing_Mark}\\p{Surrogate}]", "v");
40
39
  var spacingMarkRegex = new RegExp("\\p{Spacing_Mark}", "v");
41
40
  var rgiEmojiRegex = new RegExp("^\\p{RGI_Emoji}$", "v");
42
41
  var unqualifiedKeycapRegex = /^[\d#*]\u20E3$/;
@@ -55,10 +54,8 @@ function isDoubleWidthNonRgiEmojiSequence(segment) {
55
54
  return false;
56
55
  }
57
56
  function baseVisible(segment) {
58
- return segment.replace(leadingNonPrintingRegex, "");
59
- }
60
- function isZeroWidthCluster(segment) {
61
- return zeroWidthClusterRegex.test(segment);
57
+ const index = segment.search(visibleCharacterRegex);
58
+ return index === -1 ? void 0 : segment.slice(index);
62
59
  }
63
60
  function isHangulLeadingJamo(codePoint) {
64
61
  return codePoint >= 4352 && codePoint <= 4447 || codePoint >= 43360 && codePoint <= 43388;
@@ -75,7 +72,7 @@ function isHangulJamo(codePoint) {
75
72
  function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
76
73
  const codePoints = [];
77
74
  for (const character of visibleSegment) {
78
- if (zeroWidthClusterRegex.test(character)) {
75
+ if (!visibleCharacterRegex.test(character)) {
79
76
  continue;
80
77
  }
81
78
  codePoints.push(character.codePointAt(0));
@@ -139,14 +136,14 @@ function stringWidth(input, options = {}) {
139
136
  let width = 0;
140
137
  const eastAsianWidthOptions = { ambiguousAsWide: !ambiguousIsNarrow };
141
138
  for (const { segment } of segmenter.segment(string)) {
142
- if (isZeroWidthCluster(segment)) {
139
+ const visibleSegment = baseVisible(segment);
140
+ if (visibleSegment === void 0) {
143
141
  continue;
144
142
  }
145
143
  if (rgiEmojiRegex.test(segment) || isDoubleWidthNonRgiEmojiSequence(segment)) {
146
144
  width += 2;
147
145
  continue;
148
146
  }
149
- const visibleSegment = baseVisible(segment);
150
147
  const hangulWidth = hangulClusterWidth(visibleSegment, eastAsianWidthOptions);
151
148
  if (hangulWidth !== void 0) {
152
149
  width += hangulWidth;
package/index.js CHANGED
@@ -15,11 +15,9 @@ Logic:
15
15
 
16
16
  const segmenter = new Intl.Segmenter();
17
17
 
18
- // Whole-cluster zero-width
19
- const zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Nonspacing_Mark}|\p{Enclosing_Mark}|\p{Surrogate})+$/v;
20
-
21
- // Pick the base scalar if the cluster starts with Prepend/Format/Marks
22
- const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]+/v;
18
+ // Matches a visible (printing) character. Used to find the base scalar of a cluster (skipping leading Prepend/Format/Marks) and to detect whole-cluster zero-width.
19
+ // Intentionally a single negated class without a quantifier: a repeated pattern like `^(?:…|…)+$` backtracks exponentially on overlapping properties, and even `^[…]+$` overflows V8's backtrack stack (RangeError) on clusters with millions of marks.
20
+ const visibleCharacterRegex = /[^\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]/v;
23
21
  const spacingMarkRegex = /\p{Spacing_Mark}/v;
24
22
 
25
23
  // RGI emoji sequences
@@ -48,12 +46,10 @@ function isDoubleWidthNonRgiEmojiSequence(segment) {
48
46
  return false;
49
47
  }
50
48
 
49
+ // Returns the cluster starting at its first visible character, or `undefined` if the whole cluster is zero-width.
51
50
  function baseVisible(segment) {
52
- return segment.replace(leadingNonPrintingRegex, '');
53
- }
54
-
55
- function isZeroWidthCluster(segment) {
56
- return zeroWidthClusterRegex.test(segment);
51
+ const index = segment.search(visibleCharacterRegex);
52
+ return index === -1 ? undefined : segment.slice(index);
57
53
  }
58
54
 
59
55
  function isHangulLeadingJamo(codePoint) {
@@ -81,7 +77,7 @@ function hangulClusterWidth(visibleSegment, eastAsianWidthOptions) {
81
77
  const codePoints = [];
82
78
 
83
79
  for (const character of visibleSegment) {
84
- if (zeroWidthClusterRegex.test(character)) {
80
+ if (!visibleCharacterRegex.test(character)) {
85
81
  continue;
86
82
  }
87
83
 
@@ -178,7 +174,8 @@ export default function stringWidth(input, options = {}) {
178
174
 
179
175
  for (const {segment} of segmenter.segment(string)) {
180
176
  // Zero-width / non-printing clusters
181
- if (isZeroWidthCluster(segment)) {
177
+ const visibleSegment = baseVisible(segment);
178
+ if (visibleSegment === undefined) {
182
179
  continue;
183
180
  }
184
181
 
@@ -188,7 +185,6 @@ export default function stringWidth(input, options = {}) {
188
185
  continue;
189
186
  }
190
187
 
191
- const visibleSegment = baseVisible(segment);
192
188
  const hangulWidth = hangulClusterWidth(visibleSegment, eastAsianWidthOptions);
193
189
  if (hangulWidth !== undefined) {
194
190
  width += hangulWidth;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cjser/string-width",
3
- "version": "8.2.2-cjser.2",
3
+ "version": "8.3.0-cjser.2",
4
4
  "description": "Get the visual width of a string - the number of columns required to display it",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -59,7 +59,7 @@
59
59
  "east-asian-width"
60
60
  ],
61
61
  "dependencies": {
62
- "@cjser/get-east-asian-width": "1.6.0-cjser.2",
62
+ "@cjser/get-east-asian-width": "1.7.0-cjser.2",
63
63
  "@cjser/strip-ansi": "7.2.0-cjser.2"
64
64
  },
65
65
  "devDependencies": {
@@ -70,11 +70,11 @@
70
70
  "types": "./index.d.ts",
71
71
  "main": "./dist-cjser/index.cjs",
72
72
  "cjser": {
73
- "sourceVersion": "8.2.2",
73
+ "sourceVersion": "8.3.0",
74
74
  "cjserVersion": 2,
75
75
  "original": {
76
76
  "name": "string-width",
77
- "version": "8.2.2",
77
+ "version": "8.3.0",
78
78
  "exports": {
79
79
  "types": "./index.d.ts",
80
80
  "default": "./index.js"