Substrings based on character index # Arguments `array` - The input string array `start` - The start index of all substrings. If `start >= 0`, then count from the start of the string, otherwise count from the end of the string. `length`(option) - The length of all substrings. If `length` is `None`, then the substring is from `start` to the end of the string. Attention: Both `start` and `length
(
array: &GenericStringArray<OffsetSize>,
start: i64,
length: Option<u64>,
)
| 190 | /// assert_eq!(result, StringArray::from(vec![Some("rrow"), None, Some(" ⊢x:")])); |
| 191 | /// ``` |
| 192 | pub fn substring_by_char<OffsetSize: OffsetSizeTrait>( |
| 193 | array: &GenericStringArray<OffsetSize>, |
| 194 | start: i64, |
| 195 | length: Option<u64>, |
| 196 | ) -> Result<GenericStringArray<OffsetSize>, ArrowError> { |
| 197 | let mut vals = BufferBuilder::<u8>::new({ |
| 198 | let offsets = array.value_offsets(); |
| 199 | (offsets[array.len()] - offsets[0]).to_usize().unwrap() |
| 200 | }); |
| 201 | let mut new_offsets = BufferBuilder::<OffsetSize>::new(array.len() + 1); |
| 202 | new_offsets.append(OffsetSize::zero()); |
| 203 | let length = length.map(|len| len.to_usize().unwrap()); |
| 204 | |
| 205 | array.iter().for_each(|val| { |
| 206 | if let Some(val) = val { |
| 207 | let char_count = val.chars().count(); |
| 208 | let start = if start >= 0 { |
| 209 | start.to_usize().unwrap() |
| 210 | } else { |
| 211 | char_count - (-start).to_usize().unwrap().min(char_count) |
| 212 | }; |
| 213 | let (start_offset, end_offset) = get_start_end_offset(val, start, length); |
| 214 | vals.append_slice(&val.as_bytes()[start_offset..end_offset]); |
| 215 | } |
| 216 | new_offsets.append(OffsetSize::from_usize(vals.len()).unwrap()); |
| 217 | }); |
| 218 | let offsets = OffsetBuffer::new(new_offsets.finish().into()); |
| 219 | let values = vals.finish(); |
| 220 | let nulls = array |
| 221 | .nulls() |
| 222 | .map(|n| n.inner().sliced()) |
| 223 | .and_then(|b| NullBuffer::from_unsliced_buffer(b, array.len())); |
| 224 | Ok(GenericStringArray::<OffsetSize>::new( |
| 225 | offsets, values, nulls, |
| 226 | )) |
| 227 | } |
| 228 | |
| 229 | /// * `val` - string |
| 230 | /// * `start` - the start char index of the substring |
no test coverage detected