pub fn make_string_arr(s: &str) -> Vec { s.trim_matches(&['[', ']'][..]) .split(',') .map(|s| s.trim_matches(&['"',' ']).to_string()) .collect() } pub fn make_arr(s: &str) -> Vec { s.trim_matches(&['[', ']'][..]) .split(',') .map(|x| { let x = x.trim(); if x == "null" { -1 } else { x.parse::().unwrap() } }) .collect() } pub fn make_matrix(s: &str) -> Vec> { let s = s.trim(); // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { &s[1..s.len() - 1] } else { s } .trim(); if inner.is_empty() { return Vec::new(); } let bytes = inner.as_bytes(); let mut i = 0usize; let mut rows: Vec> = Vec::new(); while i < bytes.len() { // find next '[' while i < bytes.len() && bytes[i] != b'[' { i += 1; } if i >= bytes.len() { break; } let start = i + 1; // byte index after '[' i = start; // find matching ']' while i < bytes.len() && bytes[i] != b']' { i += 1; } let end = i; // exclusive // move i past ']' for next iteration (if in bounds) if i < bytes.len() { i += 1; } if end <= start { // empty row like [] rows.push(Vec::new()); continue; } let row_str = &inner[start..end]; let row_vec: Vec = row_str .split(',') .map(|x| { let x = x.trim(); if x == "null" || x.is_empty() { -1 } else { x.parse::().unwrap() } }) .collect(); rows.push(row_vec); } rows } pub fn make_char_matrix(s: &str) -> Vec> { let s = s.trim(); // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { &s[1..s.len() - 1] } else { s } .trim(); if inner.is_empty() { return Vec::new(); } let bytes = inner.as_bytes(); let mut i = 0usize; let mut rows: Vec> = Vec::new(); while i < bytes.len() { // find next '[' while i < bytes.len() && bytes[i] != b'[' { i += 1; } if i >= bytes.len() { break; } let start = i + 1; // byte index after '[' i = start; // find matching ']' while i < bytes.len() && bytes[i] != b']' { i += 1; } let end = i; // exclusive // move i past ']' for next iteration (if in bounds) if i < bytes.len() { i += 1; } if end <= start { // empty row like [] rows.push(Vec::new()); continue; } let row_str = &inner[start..end]; let row_vec: Vec = row_str .split(',') .map(|x| { let x = x.trim().trim_matches('"'); if x == "null" || x.is_empty() { ' ' } else { x.to_string().chars().nth(0).unwrap() } }) .collect(); rows.push(row_vec); } rows } pub fn make_string_matrix(s: &str) -> Vec> { let s = s.trim(); // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { &s[1..s.len() - 1] } else { s } .trim(); if inner.is_empty() { return Vec::new(); } let bytes = inner.as_bytes(); let mut i = 0usize; let mut rows: Vec> = Vec::new(); while i < bytes.len() { // find next '[' while i < bytes.len() && bytes[i] != b'[' { i += 1; } if i >= bytes.len() { break; } let start = i + 1; // byte index after '[' i = start; // find matching ']' while i < bytes.len() && bytes[i] != b']' { i += 1; } let end = i; // exclusive // move i past ']' for next iteration (if in bounds) if i < bytes.len() { i += 1; } if end <= start { // empty row like [] rows.push(Vec::new()); continue; } let row_str = &inner[start..end]; let row_vec: Vec = row_str .split(',') .map(|x| { let x = x.trim().trim_matches('"'); if x == "null" || x.is_empty() { "".to_string() } else { x.to_string() } }) .collect(); rows.push(row_vec); } rows }