aboutsummaryrefslogtreecommitdiff
path: root/src/str.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-02-19 17:16:54 +0100
committerHampusM <hampus@hampusmat.com>2023-02-19 18:24:57 +0100
commit6b6f25357e25a07d234ae683c1b58b1929b8483e (patch)
tree5ff873b64aa21c0961d32c6aee056aca32481e20 /src/str.rs
parentb3fc15dc216794419c7d97d9f4d7f596c26e9182 (diff)
feat: add project & for_each_opengl_command macro
Diffstat (limited to 'src/str.rs')
-rw-r--r--src/str.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/str.rs b/src/str.rs
new file mode 100644
index 0000000..90f3600
--- /dev/null
+++ b/src/str.rs
@@ -0,0 +1,61 @@
+use once_cell::sync::Lazy;
+use regex::{Captures, Regex};
+
+static C_PTR_RE: Lazy<Regex> =
+ Lazy::new(|| Regex::new(r"(const )?([a-zA-Z0-9_]+)\s?\*(.*)").unwrap());
+
+static REST_C_PTR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(const)?\s?\*").unwrap());
+
+pub fn c_ptr_to_rust_ptr(c_ptr: &str) -> String
+{
+ C_PTR_RE
+ .replace(c_ptr, |captures: &Captures| {
+ let const_or_mut = captures.get(1).map_or_else(|| "mut", |_| "const");
+
+ let type_name = captures.get(2).unwrap().as_str();
+
+ let rest = captures.get(3).unwrap();
+
+ let rest_ptr_matches = REST_C_PTR_RE.captures_iter(rest.as_str());
+
+ let rest_ptrs = rest_ptr_matches
+ .map(|capts| {
+ let const_or_mut = capts.get(1).map_or_else(|| "mut", |_| "const");
+
+ format!("*{const_or_mut} ")
+ })
+ .collect::<Vec<_>>();
+
+ format!("{}*{const_or_mut} {type_name}", rest_ptrs.join(" "))
+ })
+ .to_string()
+}
+
+#[cfg(test)]
+mod tests
+{
+ use pretty_assertions::assert_str_eq;
+
+ use super::*;
+
+ #[test]
+ fn c_ptr_to_rust_ptr_works()
+ {
+ assert_str_eq!(c_ptr_to_rust_ptr("void *"), "*mut void");
+ assert_str_eq!(c_ptr_to_rust_ptr("const void*"), "*const void");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLint *"), "*mut GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint*"), "*const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint *"), "*const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint **"), "*mut *const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("const GLint * *"), "*mut *const GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLint **"), "*mut *mut GLint");
+ assert_str_eq!(c_ptr_to_rust_ptr("GLuint * const*"), "*const *mut GLuint");
+
+ assert_str_eq!(
+ c_ptr_to_rust_ptr("const GLuint * const*"),
+ "*const *const GLuint"
+ );
+
+ assert_str_eq!(c_ptr_to_rust_ptr("GLchar"), "GLchar");
+ }
+}