isomorphic-git 1.42.4 → 1.42.5

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.
package/index.cjs CHANGED
@@ -507,9 +507,54 @@ class BufferCursor {
507
507
  }
508
508
  }
509
509
 
510
+ // Code units from U+D800 up. Only these can make the two orders differ.
511
+ const NEEDS_CODE_POINT_ORDER = /[\ud800-￿]/;
512
+
513
+ // Is the code unit at index i a surrogate with no partner? Such a unit is not valid Unicode.
514
+ // A high surrogate needs a low one after it. A low surrogate needs a high one before it.
515
+ function isLoneSurrogate(s, i, unit) {
516
+ if (unit < 0xd800 || unit > 0xdfff) return false
517
+ if (unit < 0xdc00) {
518
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
519
+ return next < 0xdc00 || next > 0xdfff
520
+ }
521
+ const prev = i > 0 ? s.charCodeAt(i - 1) : 0;
522
+ return prev < 0xd800 || prev > 0xdbff
523
+ }
524
+
525
+ /**
526
+ * Compare two strings by code point. This gives the same order as the UTF-8 bytes of the
527
+ * strings, and git sorts paths in that order. The `<` operator compares UTF-16 code units,
528
+ * so it puts a character above U+FFFF (a surrogate pair) before the characters from U+E000
529
+ * to U+FFFF. Git puts it after them. A lone surrogate counts as U+FFFD, because the UTF-8
530
+ * encoding of the path writes it that way.
531
+ *
532
+ * @param {string} a
533
+ * @param {string} b
534
+ * @returns {-1 | 0 | 1}
535
+ */
510
536
  function compareStrings(a, b) {
511
- // https://stackoverflow.com/a/40355107/2168416
512
- return -(a < b) || +(a > b)
537
+ if (!NEEDS_CODE_POINT_ORDER.test(a) && !NEEDS_CODE_POINT_ORDER.test(b)) {
538
+ // Both orders are the same, and the `<` operator is the fastest way.
539
+ return -(a < b) || +(a > b)
540
+ }
541
+ const length = Math.min(a.length, b.length);
542
+ for (let i = 0; i < length; i++) {
543
+ let x = a.charCodeAt(i);
544
+ let y = b.charCodeAt(i);
545
+ if (x !== y) {
546
+ if (isLoneSurrogate(a, i, x)) x = 0xfffd;
547
+ if (isLoneSurrogate(b, i, y)) y = 0xfffd;
548
+ if (x === y) continue
549
+ // Move the surrogates (U+D800 to U+DFFF) above U+FFFF, where their code points are.
550
+ if (x >= 0xd800 && y >= 0xd800) {
551
+ x += x >= 0xe000 ? -0x800 : 0x2000;
552
+ y += y >= 0xe000 ? -0x800 : 0x2000;
553
+ }
554
+ return x < y ? -1 : 1
555
+ }
556
+ }
557
+ return a.length < b.length ? -1 : a.length > b.length ? 1 : 0
513
558
  }
514
559
 
515
560
  function comparePath(a, b) {
@@ -9806,8 +9851,8 @@ function filterCapabilities(server, client) {
9806
9851
 
9807
9852
  const pkg = {
9808
9853
  name: 'isomorphic-git',
9809
- version: '1.42.4',
9810
- agent: 'git/isomorphic-git@1.42.4',
9854
+ version: '1.42.5',
9855
+ agent: 'git/isomorphic-git@1.42.5',
9811
9856
  };
9812
9857
 
9813
9858
  class FIFO {
package/index.js CHANGED
@@ -500,9 +500,54 @@ class BufferCursor {
500
500
  }
501
501
  }
502
502
 
503
+ // Code units from U+D800 up. Only these can make the two orders differ.
504
+ const NEEDS_CODE_POINT_ORDER = /[\ud800-￿]/;
505
+
506
+ // Is the code unit at index i a surrogate with no partner? Such a unit is not valid Unicode.
507
+ // A high surrogate needs a low one after it. A low surrogate needs a high one before it.
508
+ function isLoneSurrogate(s, i, unit) {
509
+ if (unit < 0xd800 || unit > 0xdfff) return false
510
+ if (unit < 0xdc00) {
511
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
512
+ return next < 0xdc00 || next > 0xdfff
513
+ }
514
+ const prev = i > 0 ? s.charCodeAt(i - 1) : 0;
515
+ return prev < 0xd800 || prev > 0xdbff
516
+ }
517
+
518
+ /**
519
+ * Compare two strings by code point. This gives the same order as the UTF-8 bytes of the
520
+ * strings, and git sorts paths in that order. The `<` operator compares UTF-16 code units,
521
+ * so it puts a character above U+FFFF (a surrogate pair) before the characters from U+E000
522
+ * to U+FFFF. Git puts it after them. A lone surrogate counts as U+FFFD, because the UTF-8
523
+ * encoding of the path writes it that way.
524
+ *
525
+ * @param {string} a
526
+ * @param {string} b
527
+ * @returns {-1 | 0 | 1}
528
+ */
503
529
  function compareStrings(a, b) {
504
- // https://stackoverflow.com/a/40355107/2168416
505
- return -(a < b) || +(a > b)
530
+ if (!NEEDS_CODE_POINT_ORDER.test(a) && !NEEDS_CODE_POINT_ORDER.test(b)) {
531
+ // Both orders are the same, and the `<` operator is the fastest way.
532
+ return -(a < b) || +(a > b)
533
+ }
534
+ const length = Math.min(a.length, b.length);
535
+ for (let i = 0; i < length; i++) {
536
+ let x = a.charCodeAt(i);
537
+ let y = b.charCodeAt(i);
538
+ if (x !== y) {
539
+ if (isLoneSurrogate(a, i, x)) x = 0xfffd;
540
+ if (isLoneSurrogate(b, i, y)) y = 0xfffd;
541
+ if (x === y) continue
542
+ // Move the surrogates (U+D800 to U+DFFF) above U+FFFF, where their code points are.
543
+ if (x >= 0xd800 && y >= 0xd800) {
544
+ x += x >= 0xe000 ? -0x800 : 0x2000;
545
+ y += y >= 0xe000 ? -0x800 : 0x2000;
546
+ }
547
+ return x < y ? -1 : 1
548
+ }
549
+ }
550
+ return a.length < b.length ? -1 : a.length > b.length ? 1 : 0
506
551
  }
507
552
 
508
553
  function comparePath(a, b) {
@@ -9793,8 +9838,8 @@ function filterCapabilities(server, client) {
9793
9838
 
9794
9839
  const pkg = {
9795
9840
  name: 'isomorphic-git',
9796
- version: '1.42.4',
9797
- agent: 'git/isomorphic-git@1.42.4',
9841
+ version: '1.42.5',
9842
+ agent: 'git/isomorphic-git@1.42.5',
9798
9843
  };
9799
9844
 
9800
9845
  class FIFO {