Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions datafusion/functions/src/unicode/strpos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,37 @@ where
)
}
} else {
// The `find` method returns the byte index of the substring.
// We count the number of chars up to that byte index.
T::Native::from_usize(
string
.find(substring)
.map(|x| string[..x].chars().count() + 1)
.unwrap_or(0),
)
// For non-ASCII, use a single-pass search that tracks both
// byte position and character position simultaneously
if substring.is_empty() {
return T::Native::from_usize(1);
}

let substring_bytes = substring.as_bytes();
let string_bytes = string.as_bytes();

if substring_bytes.len() > string_bytes.len() {
return T::Native::from_usize(0);
}

// Single pass: find substring while counting characters
let mut char_pos = 0;
for (byte_idx, _) in string.char_indices() {
char_pos += 1;
if byte_idx + substring_bytes.len() <= string_bytes.len() {
// SAFETY: We just checked that byte_idx + substring_bytes.len() <= string_bytes.len()
let slice = unsafe {
string_bytes.get_unchecked(
byte_idx..byte_idx + substring_bytes.len(),
)
};
if slice == substring_bytes {
return T::Native::from_usize(char_pos);
}
}
}

T::Native::from_usize(0)
}
}
_ => None,
Expand Down