returns str, str.char_len() (it might not be cached in the str yet but we calculate it anyway in this method)
(
&mut self,
n: usize,
vm: &VirtualMachine,
)
| 3928 | /// returns str, str.char_len() (it might not be cached in the str yet but we calculate it |
| 3929 | /// anyway in this method) |
| 3930 | fn get_decoded_chars( |
| 3931 | &mut self, |
| 3932 | n: usize, |
| 3933 | vm: &VirtualMachine, |
| 3934 | ) -> Option<(PyStrRef, usize)> { |
| 3935 | if n == 0 { |
| 3936 | return None; |
| 3937 | } |
| 3938 | let decoded_chars = self.decoded_chars.as_ref()?; |
| 3939 | let avail = &decoded_chars.as_wtf8()[self.decoded_chars_used.bytes..]; |
| 3940 | if avail.is_empty() { |
| 3941 | return None; |
| 3942 | } |
| 3943 | let avail_chars = decoded_chars.char_len() - self.decoded_chars_used.chars; |
| 3944 | let (chars, chars_used) = if n >= avail_chars { |
| 3945 | if self.decoded_chars_used.bytes == 0 { |
| 3946 | (decoded_chars.clone(), avail_chars) |
| 3947 | } else { |
| 3948 | (PyStr::from(avail).into_ref(&vm.ctx), avail_chars) |
| 3949 | } |
| 3950 | } else { |
| 3951 | let s = crate::common::str::get_codepoints(avail, 0..n); |
| 3952 | (PyStr::from(s).into_ref(&vm.ctx), n) |
| 3953 | }; |
| 3954 | self.decoded_chars_used += Utf8size { |
| 3955 | bytes: chars.byte_len(), |
| 3956 | chars: chars_used, |
| 3957 | }; |
| 3958 | Some((chars, chars_used)) |
| 3959 | } |
| 3960 | |
| 3961 | fn set_decoded_chars(&mut self, s: Option<PyStrRef>) { |
| 3962 | self.decoded_chars = s; |