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.
@@ -1071,9 +1071,54 @@ class BufferCursor {
1071
1071
  }
1072
1072
  }
1073
1073
 
1074
+ // Code units from U+D800 up. Only these can make the two orders differ.
1075
+ const NEEDS_CODE_POINT_ORDER = /[\ud800-￿]/;
1076
+
1077
+ // Is the code unit at index i a surrogate with no partner? Such a unit is not valid Unicode.
1078
+ // A high surrogate needs a low one after it. A low surrogate needs a high one before it.
1079
+ function isLoneSurrogate(s, i, unit) {
1080
+ if (unit < 0xd800 || unit > 0xdfff) return false
1081
+ if (unit < 0xdc00) {
1082
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
1083
+ return next < 0xdc00 || next > 0xdfff
1084
+ }
1085
+ const prev = i > 0 ? s.charCodeAt(i - 1) : 0;
1086
+ return prev < 0xd800 || prev > 0xdbff
1087
+ }
1088
+
1089
+ /**
1090
+ * Compare two strings by code point. This gives the same order as the UTF-8 bytes of the
1091
+ * strings, and git sorts paths in that order. The `<` operator compares UTF-16 code units,
1092
+ * so it puts a character above U+FFFF (a surrogate pair) before the characters from U+E000
1093
+ * to U+FFFF. Git puts it after them. A lone surrogate counts as U+FFFD, because the UTF-8
1094
+ * encoding of the path writes it that way.
1095
+ *
1096
+ * @param {string} a
1097
+ * @param {string} b
1098
+ * @returns {-1 | 0 | 1}
1099
+ */
1074
1100
  function compareStrings(a, b) {
1075
- // https://stackoverflow.com/a/40355107/2168416
1076
- return -(a < b) || +(a > b)
1101
+ if (!NEEDS_CODE_POINT_ORDER.test(a) && !NEEDS_CODE_POINT_ORDER.test(b)) {
1102
+ // Both orders are the same, and the `<` operator is the fastest way.
1103
+ return -(a < b) || +(a > b)
1104
+ }
1105
+ const length = Math.min(a.length, b.length);
1106
+ for (let i = 0; i < length; i++) {
1107
+ let x = a.charCodeAt(i);
1108
+ let y = b.charCodeAt(i);
1109
+ if (x !== y) {
1110
+ if (isLoneSurrogate(a, i, x)) x = 0xfffd;
1111
+ if (isLoneSurrogate(b, i, y)) y = 0xfffd;
1112
+ if (x === y) continue
1113
+ // Move the surrogates (U+D800 to U+DFFF) above U+FFFF, where their code points are.
1114
+ if (x >= 0xd800 && y >= 0xd800) {
1115
+ x += x >= 0xe000 ? -0x800 : 0x2000;
1116
+ y += y >= 0xe000 ? -0x800 : 0x2000;
1117
+ }
1118
+ return x < y ? -1 : 1
1119
+ }
1120
+ }
1121
+ return a.length < b.length ? -1 : a.length > b.length ? 1 : 0
1077
1122
  }
1078
1123
 
1079
1124
  function comparePath(a, b) {
package/managers/index.js CHANGED
@@ -1064,9 +1064,54 @@ class BufferCursor {
1064
1064
  }
1065
1065
  }
1066
1066
 
1067
+ // Code units from U+D800 up. Only these can make the two orders differ.
1068
+ const NEEDS_CODE_POINT_ORDER = /[\ud800-￿]/;
1069
+
1070
+ // Is the code unit at index i a surrogate with no partner? Such a unit is not valid Unicode.
1071
+ // A high surrogate needs a low one after it. A low surrogate needs a high one before it.
1072
+ function isLoneSurrogate(s, i, unit) {
1073
+ if (unit < 0xd800 || unit > 0xdfff) return false
1074
+ if (unit < 0xdc00) {
1075
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
1076
+ return next < 0xdc00 || next > 0xdfff
1077
+ }
1078
+ const prev = i > 0 ? s.charCodeAt(i - 1) : 0;
1079
+ return prev < 0xd800 || prev > 0xdbff
1080
+ }
1081
+
1082
+ /**
1083
+ * Compare two strings by code point. This gives the same order as the UTF-8 bytes of the
1084
+ * strings, and git sorts paths in that order. The `<` operator compares UTF-16 code units,
1085
+ * so it puts a character above U+FFFF (a surrogate pair) before the characters from U+E000
1086
+ * to U+FFFF. Git puts it after them. A lone surrogate counts as U+FFFD, because the UTF-8
1087
+ * encoding of the path writes it that way.
1088
+ *
1089
+ * @param {string} a
1090
+ * @param {string} b
1091
+ * @returns {-1 | 0 | 1}
1092
+ */
1067
1093
  function compareStrings(a, b) {
1068
- // https://stackoverflow.com/a/40355107/2168416
1069
- return -(a < b) || +(a > b)
1094
+ if (!NEEDS_CODE_POINT_ORDER.test(a) && !NEEDS_CODE_POINT_ORDER.test(b)) {
1095
+ // Both orders are the same, and the `<` operator is the fastest way.
1096
+ return -(a < b) || +(a > b)
1097
+ }
1098
+ const length = Math.min(a.length, b.length);
1099
+ for (let i = 0; i < length; i++) {
1100
+ let x = a.charCodeAt(i);
1101
+ let y = b.charCodeAt(i);
1102
+ if (x !== y) {
1103
+ if (isLoneSurrogate(a, i, x)) x = 0xfffd;
1104
+ if (isLoneSurrogate(b, i, y)) y = 0xfffd;
1105
+ if (x === y) continue
1106
+ // Move the surrogates (U+D800 to U+DFFF) above U+FFFF, where their code points are.
1107
+ if (x >= 0xd800 && y >= 0xd800) {
1108
+ x += x >= 0xe000 ? -0x800 : 0x2000;
1109
+ y += y >= 0xe000 ? -0x800 : 0x2000;
1110
+ }
1111
+ return x < y ? -1 : 1
1112
+ }
1113
+ }
1114
+ return a.length < b.length ? -1 : a.length > b.length ? 1 : 0
1070
1115
  }
1071
1116
 
1072
1117
  function comparePath(a, b) {