(zelf: &Py<Self>, vm: &VirtualMachine)
| 933 | impl SelfIter for Reader {} |
| 934 | impl IterNext for Reader { |
| 935 | fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> { |
| 936 | let string = raise_if_stop!(zelf.iter.next(vm)?); |
| 937 | let string = string.downcast::<PyStr>().map_err(|obj| { |
| 938 | new_csv_error( |
| 939 | vm, |
| 940 | format!( |
| 941 | "iterator should return strings, not {} (the file should be opened in text mode)", |
| 942 | obj.class().name() |
| 943 | ), |
| 944 | ) |
| 945 | })?; |
| 946 | let input = string.as_bytes(); |
| 947 | if input.is_empty() || input.starts_with(b"\n") { |
| 948 | return Ok(PyIterReturn::Return(vm.ctx.new_list(vec![]).into())); |
| 949 | } |
| 950 | let mut state = zelf.state.lock(); |
| 951 | let ReadState { |
| 952 | buffer, |
| 953 | output_ends, |
| 954 | reader, |
| 955 | skipinitialspace, |
| 956 | delimiter, |
| 957 | line_num, |
| 958 | } = &mut *state; |
| 959 | |
| 960 | let mut input_offset = 0; |
| 961 | let mut output_offset = 0; |
| 962 | let mut output_ends_offset = 0; |
| 963 | let field_limit = GLOBAL_FIELD_LIMIT.lock().to_owned(); |
| 964 | #[inline] |
| 965 | fn trim_spaces(input: &[u8]) -> &[u8] { |
| 966 | let trimmed_start = input.iter().position(|&x| x != b' ').unwrap_or(input.len()); |
| 967 | let trimmed_end = input |
| 968 | .iter() |
| 969 | .rposition(|&x| x != b' ') |
| 970 | .map(|i| i + 1) |
| 971 | .unwrap_or(0); |
| 972 | &input[trimmed_start..trimmed_end] |
| 973 | } |
| 974 | let input = if *skipinitialspace { |
| 975 | let t = input.split(|x| x == delimiter); |
| 976 | t.map(|x| { |
| 977 | let trimmed = trim_spaces(x); |
| 978 | String::from_utf8(trimmed.to_vec()).unwrap() |
| 979 | }) |
| 980 | .join(format!("{}", *delimiter as char).as_str()) |
| 981 | } else { |
| 982 | String::from_utf8(input.to_vec()).unwrap() |
| 983 | }; |
| 984 | loop { |
| 985 | let (res, n_read, n_written, n_ends) = reader.read_record( |
| 986 | &input.as_bytes()[input_offset..], |
| 987 | &mut buffer[output_offset..], |
| 988 | &mut output_ends[output_ends_offset..], |
| 989 | ); |
| 990 | input_offset += n_read; |
| 991 | output_offset += n_written; |
| 992 | output_ends_offset += n_ends; |
no test coverage detected