@slnknrr/str-im 1.1.0 → 1.2.0

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.
Files changed (3) hide show
  1. package/README.md +39 -6
  2. package/package.json +10 -7
  3. package/src/str-im.js +44 -18
package/README.md CHANGED
@@ -1,4 +1,12 @@
1
- # str-im
1
+ # @slnknrr/str-im
2
+
3
+ [![npm](https://img.shields.io/npm/v/@slnknrr/str-im?logo=npm&label=npm)](https://www.npmjs.com/package/@slnknrr/str-im)
4
+ [![tests](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/actions/workflows/test.yml/badge.svg?branch=str-im)](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/actions/workflows/test.yml?query=branch%3Astr-im)
5
+ [![license: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/blob/str-im@1.2.0/LICENSE.md)
6
+ [![stars](https://img.shields.io/github/stars/slnknrr-dev/str-lib-in-javascript-for-pm_npm?style=flat&logo=github&label=stars)](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/stargazers)
7
+ [![GitHub](https://img.shields.io/badge/GitHub-slnknrr--dev-181717?logo=github)](https://github.com/slnknrr-dev)
8
+ [![Ko-fi](https://img.shields.io/badge/Ko--fi-slnknrr-FF5E5B?logo=ko-fi&logoColor=white)](https://ko-fi.com/slnknrr)
9
+ [![PayPal](https://img.shields.io/badge/PayPal-slnknrrdev-00457C?logo=paypal&logoColor=white)](https://paypal.me/slnknrrdev)
2
10
 
3
11
  **A zero-allocation string engine. Numbers in the core, strings only at the boundary.**
4
12
 
@@ -207,7 +215,7 @@ All take `(str, max=∞)`.
207
215
  | `find(str, target, {all}?)` | lazy match indices; `target` = string or many (one Aho-Corasick pass) |
208
216
  | `lfind` / `ifind` | right-to-left / case-insensitive |
209
217
  | `near(a, b, max=∞, {damerau}?)` | edit distance, or `Infinity` if it exceeds `max` |
210
- | `com(a, b)` / `comb(a, b)` | length of common prefix / suffix (`b` = one string or many) |
218
+ | `com(a, b)` / `comb(a, b)` | length of common prefix / suffix in code points (`b` = one string or many) |
211
219
 
212
220
  ### 10 · Segmentation & parsing
213
221
  | Method | Returns |
@@ -337,17 +345,42 @@ Every internal call dispatches through `this`, so replacing one method replaces
337
345
 
338
346
  ## Links
339
347
 
340
- - Source: [https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm/src/branch/main](https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm/src/branch/main)
348
+ - Source: [github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-im](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-im)
341
349
  - npm: [https://www.npmjs.com/package/@slnknrr/str-im](https://www.npmjs.com/package/@slnknrr/str-im)
342
350
  - Friendly wrapper — familiar names, booleans and arrays over the same engine: [@slnknrr/str-ez](https://www.npmjs.com/package/@slnknrr/str-ez)
343
351
 
352
+ ## Repository layout
353
+
354
+ Every `@slnknrr` library family lives in one GitHub repository, and **every npm package is a branch** of it — with its own README, tests and releases. A release is a version bump: when a push to the package's branch carries a `package.json` version that is not on npm yet, CI tests it, publishes it to npm and tags it `<package>@<version>` (`v<version>` on a `main` branch).
355
+
356
+ This package is the `str-im` branch of [`slnknrr-dev/str-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm); its releases are tagged `str-im@<version>`.
357
+
358
+ | Branch | npm package | Role |
359
+ |---|---|---|
360
+ | [`str-im`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-im) | [`@slnknrr/str-im`](https://www.npmjs.com/package/@slnknrr/str-im) | **this package** — the engine |
361
+ | [`str-ez`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-ez) | [`@slnknrr/str-ez`](https://www.npmjs.com/package/@slnknrr/str-ez) | the friendly wrapper over str-im |
362
+
363
+ All families:
364
+
365
+ | Family | Repository | Branches → packages |
366
+ |---|---|---|
367
+ | arrays | [`arr-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/arr-lib-in-javascript-for-pm_npm) | [`arr-im`](https://github.com/slnknrr-dev/arr-lib-in-javascript-for-pm_npm/tree/arr-im) → [`@slnknrr/arr-im`](https://www.npmjs.com/package/@slnknrr/arr-im) |
368
+ | bytes | [`buf-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/buf-lib-in-javascript-for-pm_npm) | [`buf-im`](https://github.com/slnknrr-dev/buf-lib-in-javascript-for-pm_npm/tree/buf-im) → [`@slnknrr/buf-im`](https://www.npmjs.com/package/@slnknrr/buf-im) |
369
+ | identifiers | [`id-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/id-lib-in-javascript-for-pm_npm) | [`main`](https://github.com/slnknrr-dev/id-lib-in-javascript-for-pm_npm/tree/main) → [`@slnknrr/id`](https://www.npmjs.com/package/@slnknrr/id) |
370
+ | numbers as text | [`num-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/num-lib-in-javascript-for-pm_npm) | [`num-im`](https://github.com/slnknrr-dev/num-lib-in-javascript-for-pm_npm/tree/num-im) → [`@slnknrr/num-im`](https://www.npmjs.com/package/@slnknrr/num-im) · [`num-ez`](https://github.com/slnknrr-dev/num-lib-in-javascript-for-pm_npm/tree/num-ez) → [`@slnknrr/num-ez`](https://www.npmjs.com/package/@slnknrr/num-ez) |
371
+ | random data | [`rnd-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/rnd-lib-in-javascript-for-pm_npm) | [`rnd-im`](https://github.com/slnknrr-dev/rnd-lib-in-javascript-for-pm_npm/tree/rnd-im) → [`@slnknrr/rnd-im`](https://www.npmjs.com/package/@slnknrr/rnd-im) · [`rnd-ez`](https://github.com/slnknrr-dev/rnd-lib-in-javascript-for-pm_npm/tree/rnd-ez) → [`@slnknrr/rnd-ez`](https://www.npmjs.com/package/@slnknrr/rnd-ez) |
372
+ | strings | [`str-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm) | [`str-im`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-im) → [`@slnknrr/str-im`](https://www.npmjs.com/package/@slnknrr/str-im) · [`str-ez`](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-ez) → [`@slnknrr/str-ez`](https://www.npmjs.com/package/@slnknrr/str-ez) |
373
+ | types | [`type-lib-in-javascript-for-pm_npm`](https://github.com/slnknrr-dev/type-lib-in-javascript-for-pm_npm) | [`type-im`](https://github.com/slnknrr-dev/type-lib-in-javascript-for-pm_npm/tree/type-im) → [`@slnknrr/type-im`](https://www.npmjs.com/package/@slnknrr/type-im) · [`type-ez`](https://github.com/slnknrr-dev/type-lib-in-javascript-for-pm_npm/tree/type-ez) → [`@slnknrr/type-ez`](https://www.npmjs.com/package/@slnknrr/type-ez) |
374
+
344
375
  ## Author
345
376
 
346
377
  Yury Slinkin (Юрий Слинкин)
347
378
 
348
- - Email: slnknrr.dev@gmail.com
349
- - Codeberg: https://codeberg.org/slnknrr
379
+ - Email: [slnknrr.dev@gmail.com](mailto:slnknrr.dev@gmail.com)
380
+ - GitHub: [github.com/slnknrr-dev](https://github.com/slnknrr-dev)
381
+ - Ko-fi: [ko-fi.com/slnknrr](https://ko-fi.com/slnknrr)
382
+ - PayPal: [paypal.me/slnknrrdev](https://paypal.me/slnknrrdev)
350
383
 
351
384
  ## License
352
385
 
353
- MIT. See [LICENSE.md](https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm/src/branch/main/LICENSE.md).
386
+ MIT © 2026 Yury Slinkin <slnknrr.dev@gmail.com>. See [LICENSE.md](https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/blob/str-im@1.2.0/LICENSE.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slnknrr/str-im",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Zero-allocation string engine: unique-character iterators, UTF-8 buffer inspection without decoding, run detection, Unicode classification. Numbers in the core, strings only at the boundary.",
5
5
  "keywords": [
6
6
  "string",
@@ -20,16 +20,19 @@
20
20
  "aho-corasick"
21
21
  ],
22
22
  "license": "MIT",
23
- "author": "Yury Slinkin <slnknrr.dev@gmail.com>",
23
+ "author": "Yury Slinkin <slnknrr.dev@gmail.com> (https://github.com/slnknrr-dev)",
24
24
  "repository": {
25
25
  "type": "git",
26
- "url": "git+https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm.git"
26
+ "url": "git+https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm.git"
27
27
  },
28
28
  "bugs": {
29
- "url": "https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm/issues"
29
+ "url": "https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/issues"
30
+ },
31
+ "homepage": "https://github.com/slnknrr-dev/str-lib-in-javascript-for-pm_npm/tree/str-im#readme",
32
+ "funding": {
33
+ "type": "ko-fi",
34
+ "url": "https://ko-fi.com/slnknrr"
30
35
  },
31
- "homepage": "https://codeberg.org/slnknrr/strim_lib.js-for-pm_npm",
32
- "funding": "https://slnknrr.codeberg.page/funding#?npmjs=@slnknrr/str-im",
33
36
  "type": "module",
34
37
  "exports": {
35
38
  ".": {
@@ -55,7 +58,7 @@
55
58
  "bench": "node bench/index.js",
56
59
  "types": "tsc -p tsconfig.json",
57
60
  "tables": "node tools/build-tables.js",
58
- "prepublishOnly": "npm run tables && npm run types && npm test"
61
+ "prepublishOnly": "npm run types && npm test"
59
62
  },
60
63
  "publishConfig": {
61
64
  "access": "public"
package/src/str-im.js CHANGED
@@ -577,6 +577,37 @@ class strim {
577
577
  return this._find(this._class('WIDE'), cp, 2) >= 0 ? 2 : 1;
578
578
  }
579
579
 
580
+ //width of one grapheme cluster from its first code point. Emoji presentation widens a
581
+ //narrow pictograph (☺ + VS16 U+FE0F → 2), and a regional-indicator pair is one flag (2).
582
+ _clusterw(base, cps) {
583
+ const w = this._width(base);
584
+ if (w !== 1) return w;
585
+ if (cps > 1 && (this._gcb(base) & 15) === GCB.RI) return 2;
586
+ return w;
587
+ }
588
+ //display width of a run of code points, cluster by cluster (UAX #29 boundaries, as _graph).
589
+ //One state machine for dlen and both wraps, so a ZWJ sequence is one glyph everywhere.
590
+ _cpsw(cps) {
591
+ let w = 0, prev = -1, ri = 0, pict = false, base = -1, n = 0, vs = false;
592
+ const flush = () => {
593
+ if (base < 0) return;
594
+ const cw = this._clusterw(base, n);
595
+ w += cw === 1 && vs && (this._gcb(base) & 16) ? 2 : cw;
596
+ };
597
+ for (const cp of cps) {
598
+ const raw = this._gcb(cp), cls = raw & 15;
599
+ if (prev < 0 || this._gbreak(prev, cls, ri, pict, (raw & 16) !== 0)) { flush(); base = cp; n = 0; vs = false; }
600
+ if (cp === 0xfe0f) vs = true;
601
+ n++;
602
+ ri = cls === GCB.RI ? ri + 1 : 0;
603
+ if (raw & 16) pict = true;
604
+ else if (cls !== GCB.EXTEND && cls !== GCB.ZWJ) pict = false;
605
+ prev = cls;
606
+ }
607
+ flush();
608
+ return w;
609
+ }
610
+
580
611
  //case folding (simple case folding). By table, not via toLowerCase: the latter needs
581
612
  //a string. Needed by ichar, ifind and skel.
582
613
  _fold(cp) {
@@ -671,7 +702,7 @@ class strim {
671
702
  if (first) {
672
703
  yield 0;
673
704
  first = false;
674
- } else if (this._gbreak(prev, cls, ri, pict)) {
705
+ } else if (this._gbreak(prev, cls, ri, pict, (raw & 16) !== 0)) {
675
706
  yield i;
676
707
  }
677
708
  //state for GB12/13: length of the current chain of regional indicators
@@ -686,7 +717,8 @@ class strim {
686
717
 
687
718
  //whether to break between class a and class b. Extracted from _graph as a separate method
688
719
  //so the rules can be overridden (point 11) — e.g. to glue emoji tags together.
689
- _gbreak(a, b, ri, pict) {
720
+ //pict: the cluster so far is pictograph Extend* ZWJ; bpict: b is Extended_Pictographic.
721
+ _gbreak(a, b, ri, pict, bpict) {
690
722
  if (a === GCB.CR && b === GCB.LF) return false; //GB3
691
723
  if (a === GCB.CTRL || a === GCB.CR || a === GCB.LF) return true; //GB4
692
724
  if (b === GCB.CTRL || b === GCB.CR || b === GCB.LF) return true; //GB5
@@ -696,7 +728,7 @@ class strim {
696
728
  if (b === GCB.EXTEND || b === GCB.ZWJ) return false; //GB9
697
729
  if (b === GCB.SPACING) return false; //GB9a
698
730
  if (a === GCB.PREPEND) return false; //GB9b
699
- if (a === GCB.ZWJ && pict) return false; //GB11 — decision made from the pict flag
731
+ if (a === GCB.ZWJ && pict && bpict) return false; //GB11 — ExtPict Extend* ZWJ × ExtPict, nothing else
700
732
  if (a === GCB.RI && b === GCB.RI && ri % 2 === 1) return false; //GB12/13
701
733
  return true; //GB999
702
734
  }
@@ -1028,15 +1060,10 @@ class strim {
1028
1060
  return c === 0 ? 0 : c - 1;
1029
1061
  }
1030
1062
  //visual width in columns: width is taken for the cluster as a whole (by its base
1031
- //code point); combining marks inside give 0 and don't enter the sum.
1063
+ //code point); combining marks inside give 0 and don't enter the sum; VS16 and flag
1064
+ //pairs widen a cluster to 2 (see _cpsw).
1032
1065
  dlen(buf) {
1033
- const adp = this._adapter(buf);
1034
- let w = 0, prev = -1;
1035
- for (const b of this._graph(buf, adp)) {
1036
- if (prev >= 0) w += this._width(this._at(buf, adp, prev));
1037
- prev = b;
1038
- }
1039
- return w;
1066
+ return this._cpsw(this._cps(buf, this._adapter(buf)));
1040
1067
  }
1041
1068
 
1042
1069
  //=========================================================================
@@ -1890,13 +1917,12 @@ class strim {
1890
1917
  //visual width of a string in columns, stripping ANSI-CSI (ESC '[' … final byte @-~).
1891
1918
  _vwidth(s) {
1892
1919
  const adp = this._adapter(s);
1893
- const cps = [...this._cps(s, adp)];
1894
- let w = 0;
1920
+ const cps = [...this._cps(s, adp)], out = [];
1895
1921
  for (let i = 0; i < cps.length; i++) {
1896
1922
  if (cps[i] === 0x1b) { i++; if (cps[i] === 0x5b) { i++; while (i < cps.length && !(cps[i] >= 0x40 && cps[i] <= 0x7e)) i++; } continue; }
1897
- w += this._width(cps[i]);
1923
+ out.push(cps[i]);
1898
1924
  }
1899
- return w;
1925
+ return this._cpsw(out);
1900
1926
  }
1901
1927
 
1902
1928
 
@@ -2147,7 +2173,7 @@ class Mut {
2147
2173
  //display width of [a, b) — the engine's _vwidth over a range, ANSI-CSI excluded
2148
2174
  _vwidth(a, b) {
2149
2175
  const e = this.engine, adp = e._adapter(this);
2150
- let w = 0;
2176
+ const out = [];
2151
2177
  for (let i = a; i < b;) {
2152
2178
  const cp = e._at(this, adp, i);
2153
2179
  i = this._next(i);
@@ -2159,9 +2185,9 @@ class Mut {
2159
2185
  }
2160
2186
  continue;
2161
2187
  }
2162
- w += e._width(cp);
2188
+ out.push(cp);
2163
2189
  }
2164
- return w;
2190
+ return e._cpsw(out);
2165
2191
  }
2166
2192
  //simple case folding in place, by the engine's table. Pass one proves every folded code
2167
2193
  //point fits the slot of the original (utf-16 never changes width; in utf-8 a few do — Ⱥ