rusticl: extract tokenize() and fix UTF-8 handling in compile options

Extract the tokenization logic from CompileOptions::new() into a
separate tokenize() method.

Also change to_str().unwrap() to to_str().map_err(|_| err)? so that
non-UTF-8 option strings return the appropriate error code instead
of panicking.

Signed-off-by: jiajia Qian <jiajia.qian@nxp.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/41980>
This commit is contained in:
jiajia Qian 2026-06-11 09:45:02 +08:00 committed by Marge Bot
parent 36db44b2ad
commit d889250541

View file

@ -317,18 +317,9 @@ pub struct CompileOptions {
}
impl CompileOptions {
pub fn new(options: &CStr, err: cl_int) -> CLResult<Self> {
let mut parsed_options = ParsedCompileOptions::from_option_str(options);
if options.is_empty() {
return Ok(CompileOptions {
parsed: parsed_options,
clang_args: Vec::new(),
});
}
let options = options.to_str().unwrap();
/// Tokenizes an options string, splitting on spaces but respecting double-quoted strings.
fn tokenize(options: &str) -> Vec<&str> {
let mut res = Vec::new();
// we seperate on a ' ' unless we hit a "
let mut sep = ' ';
let mut old = 0;
@ -351,6 +342,20 @@ impl CompileOptions {
}
// add end of the string
res.push(&options[old..]);
res
}
pub fn new(options: &CStr, err: cl_int) -> CLResult<Self> {
let mut parsed_options = ParsedCompileOptions::from_option_str(options);
if options.is_empty() {
return Ok(CompileOptions {
parsed: parsed_options,
clang_args: Vec::new(),
});
}
let options = options.to_str().map_err(|_| err)?;
let res = Self::tokenize(options);
let mut strings = Vec::new();
for a in res.into_iter() {