Str.fromUtf8Range: take list argument as owned; make sure it's decremented in the small string case

This commit is contained in:
Folkert 2022-07-12 22:54:10 +02:00
parent 32207beedf
commit bc24841921
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 11 additions and 2 deletions

View file

@ -1670,14 +1670,23 @@ pub fn fromUtf8Range(arg: RocList, start: usize, count: usize, update_mode: Upda
.problem_code = Utf8ByteProblem.InvalidStartByte,
};
} else {
// turn the bytes into a small string
const string = RocStr.init(@ptrCast([*]const u8, bytes), count);
// decref the list
utils.decref(arg.bytes, arg.len(), 1);
return FromUtf8Result{
.is_ok = true,
.string = RocStr.init(@ptrCast([*]const u8, bytes), count),
.string = string,
.byte_index = 0,
.problem_code = Utf8ByteProblem.InvalidStartByte,
};
}
} else {
// decref the list
utils.decref(arg.bytes, arg.len(), 1);
const temp = errorToProblem(@ptrCast([*]u8, arg.bytes), arg.length);
return FromUtf8Result{ .is_ok = false, .string = RocStr.empty(), .byte_index = temp.index, .problem_code = temp.problem };
}