(zelf: &Py<Self>, (pystr, char_idx): Self::Args, vm: &VirtualMachine)
| 632 | impl Callable for JsonScanner { |
| 633 | type Args = (PyStrRef, isize); |
| 634 | fn call(zelf: &Py<Self>, (pystr, char_idx): Self::Args, vm: &VirtualMachine) -> PyResult { |
| 635 | if char_idx < 0 { |
| 636 | return Err(vm.new_value_error("idx cannot be negative")); |
| 637 | } |
| 638 | let char_idx = char_idx as usize; |
| 639 | let pystr = pystr.try_into_utf8(vm)?; |
| 640 | let s = pystr.as_str(); |
| 641 | |
| 642 | // Calculate byte index from char index (O(char_idx) but only at entry point) |
| 643 | let byte_idx = if char_idx == 0 { |
| 644 | 0 |
| 645 | } else { |
| 646 | match s.char_indices().nth(char_idx) { |
| 647 | Some((byte_i, _)) => byte_i, |
| 648 | None => { |
| 649 | // char_idx is beyond the string length |
| 650 | return PyIterReturn::StopIteration(Some(vm.ctx.new_int(char_idx).into())) |
| 651 | .to_pyresult(vm); |
| 652 | } |
| 653 | } |
| 654 | }; |
| 655 | |
| 656 | zelf.parse(pystr, char_idx, byte_idx, zelf.to_owned().into(), vm) |
| 657 | .and_then(|x| x.to_pyresult(vm)) |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | fn encode_string(s: &str, ascii_only: bool) -> String { |
no test coverage detected