MCPcopy Index your code
hub / github.com/RustPython/RustPython / next

Method next

crates/stdlib/src/csv.rs:935–1049  ·  view source on GitHub ↗
(zelf: &Py<Self>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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;

Callers 10

getnameinfoFunction · 0.45
get_addrFunction · 0.45
prodFunction · 0.45
sumprodFunction · 0.45
try_parse_crlMethod · 0.45
txt2objFunction · 0.45
PEM_cert_to_DER_certFunction · 0.45
repr_strMethod · 0.45
scanstringFunction · 0.45
decode_unicodeFunction · 0.45

Calls 15

new_csv_errorFunction · 0.85
resize_bufFunction · 0.85
starts_withMethod · 0.80
new_listMethod · 0.80
to_vecMethod · 0.80
allMethod · 0.80
to_stringMethod · 0.80
ErrClass · 0.50
as_bytesMethod · 0.45
is_emptyMethod · 0.45
lockMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected