204 lines
5.1 KiB
Rust
204 lines
5.1 KiB
Rust
pub fn make_string_arr(s: &str) -> Vec<String> {
|
|
s.trim_matches(&['[', ']'][..])
|
|
.split(',')
|
|
.map(|s| s.trim_matches(&['"',' ']).to_string())
|
|
.collect()
|
|
}
|
|
pub fn make_arr(s: &str) -> Vec<i32> {
|
|
s.trim_matches(&['[', ']'][..])
|
|
.split(',')
|
|
.map(|x| {
|
|
let x = x.trim();
|
|
if x == "null" {
|
|
-1
|
|
} else {
|
|
x.parse::<i32>().unwrap()
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
|
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<i32>> = 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<i32> = row_str
|
|
.split(',')
|
|
.map(|x| {
|
|
let x = x.trim();
|
|
if x == "null" || x.is_empty() {
|
|
-1
|
|
} else {
|
|
x.parse::<i32>().unwrap()
|
|
}
|
|
})
|
|
.collect();
|
|
rows.push(row_vec);
|
|
}
|
|
|
|
rows
|
|
}
|
|
|
|
pub fn make_char_matrix(s: &str) -> Vec<Vec<char>> {
|
|
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<char>> = 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<char> = 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<Vec<String>> {
|
|
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<String>> = 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<String> = 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
|
|
} |