Skip to content

Commit 9bc920c

Browse files
committed
♻️ tidy decode duplicate handling and dot-decoding hot path
1 parent d3e4c27 commit 9bc920c

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

lib/src/extensions/decode.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,19 @@ extension _$Decode on QS {
191191

192192
// Duplicate key policy: combine/first/last (default: combine).
193193
final bool existing = obj.containsKey(key);
194-
if (existing && options.duplicates == Duplicates.combine) {
195-
obj[key] = Utils.combine(obj[key], val, listLimit: options.listLimit);
196-
} else if (!existing || options.duplicates == Duplicates.last) {
197-
obj[key] = val;
194+
switch ((existing, options.duplicates)) {
195+
case (true, Duplicates.combine):
196+
// Existing key + `combine` policy: merge old/new values.
197+
obj[key] = Utils.combine(obj[key], val, listLimit: options.listLimit);
198+
break;
199+
case (false, _):
200+
case (true, Duplicates.last):
201+
// New key, or `last` policy: store the current value.
202+
obj[key] = val;
203+
break;
204+
case (true, Duplicates.first):
205+
// Existing key + `first` policy: keep the original value.
206+
break;
198207
}
199208
}
200209

@@ -285,7 +294,7 @@ extension _$Decode on QS {
285294
final bool wasBracketed = root.startsWith('[') && root.endsWith(']');
286295
final String cleanRoot =
287296
wasBracketed ? root.slice(1, root.length - 1) : root;
288-
String decodedRoot = options.decodeDotInKeys
297+
String decodedRoot = options.decodeDotInKeys && cleanRoot.contains('%2')
289298
? cleanRoot.replaceAll('%2E', '.').replaceAll('%2e', '.')
290299
: cleanRoot;
291300

@@ -301,8 +310,8 @@ extension _$Decode on QS {
301310
int opens = 0, closes = 0;
302311
for (int k = 0; k < decodedRoot.length; k++) {
303312
final cu = decodedRoot.codeUnitAt(k);
304-
if (cu == 0x5B) opens++;
305-
if (cu == 0x5D) closes++;
313+
if (cu == 0x5B) opens++; // '['
314+
if (cu == 0x5D) closes++; // ']'
306315
}
307316
if (opens > closes) {
308317
decodedRoot = decodedRoot.substring(0, decodedRoot.length - 1);
@@ -404,9 +413,9 @@ extension _$Decode on QS {
404413
// Balance nested '[' and ']' within this group.
405414
while (i < n) {
406415
final int cu = key.codeUnitAt(i);
407-
if (cu == 0x5B) {
416+
if (cu == 0x5B /* '[' */) {
408417
level++;
409-
} else if (cu == 0x5D) {
418+
} else if (cu == 0x5D /* ']' */) {
410419
level--;
411420
if (level == 0) {
412421
close = i;

0 commit comments

Comments
 (0)