From 44ee64a821f867c327cc6d91958fc27658841fab Mon Sep 17 00:00:00 2001 From: steve donovan Date: Mon, 17 Apr 2017 17:09:15 +0200 Subject: [PATCH] errors starting to bubble up --- Cargo.toml | 1 - examples/iter.rs | 9 +++++++++ src/lib.rs | 28 ++++++++++++++++++---------- src/lua-str.c | 14 ++++++++------ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab6544f..461953e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ links = "foo" build = "build.rs" [dependencies] -libc="0.2.0" [build-dependencies] gcc="0.3" diff --git a/examples/iter.rs b/examples/iter.rs index acf6126..ac66220 100644 --- a/examples/iter.rs +++ b/examples/iter.rs @@ -3,6 +3,12 @@ extern crate lua_patterns as lp; fn main() { + + let mut m = lp::LuaPattern::new("hello%"); + m.matches("hello"); + println!("ok"); + + ///* let mut m = lp::LuaPattern::new("(%a+)"); let mut iter = m.gmatch("one two three"); assert_eq!(iter.next(), Some("one")); @@ -31,6 +37,9 @@ fn main() { |cc| cc.get(1).to_uppercase() ); assert_eq!(res,"hello DOLLY you're so FINE"); + //*/ + + } diff --git a/src/lib.rs b/src/lib.rs index b91d65d..3af8157 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ -extern crate libc; -use libc::{size_t,c_int}; use std::ptr; use std::ops; +use std::os::raw::{c_int,c_char,c_uint}; +use std::ffi::CStr; #[repr(C)] #[derive(PartialEq,Eq,Debug)] @@ -12,13 +12,13 @@ struct LuaMatch { static LUA_MAXCAPTURES: usize = 32; -// int str_match (const char *s, size_t ls, const char *p, size_t lp, char **err_msg, LuaMatch *mm) #[link(name = "lua-str", kind="static")] extern { - fn str_match (s: *const u8, ls: size_t, p: *const u8, lp: size_t, - //err_msg: *mut *const u8, - err_msg: *const u8, - mm: *mut LuaMatch) -> c_int; + fn str_match ( + s: *const u8, ls: c_uint, p: *const u8, lp: c_uint, + err_msg: *mut *mut c_char, + mm: *mut LuaMatch + ) -> c_int; } pub struct LuaPattern<'a> { @@ -39,13 +39,21 @@ impl <'a> LuaPattern<'a> { } pub fn matches_bytes(&mut self, s: &[u8]) -> bool { - let err_msg: *const u8 = ptr::null(); + let c_ptr: *mut c_char = ptr::null_mut(); + let pvoid = Box::into_raw(Box::new(c_ptr)); + let err_msg : *mut *mut c_char = pvoid; unsafe { - self.n_match = str_match(s.as_ptr(),s.len() as size_t, - self.patt.as_ptr(),self.patt.len() as size_t, + self.n_match = str_match(s.as_ptr(),s.len() as c_uint, + self.patt.as_ptr(),self.patt.len() as c_uint, err_msg, self.matches.as_mut_ptr()) as usize; + let ep = *err_msg; + if ! ep.is_null() { + let slice = CStr::from_ptr(ep); + println!("{:?}", slice); + } } + self.n_match > 0 } diff --git a/src/lua-str.c b/src/lua-str.c index 34c3786..51dc66c 100644 --- a/src/lua-str.c +++ b/src/lua-str.c @@ -82,6 +82,7 @@ static int capture_to_close (MatchState *ms) { static const char *classend (MatchState *ms, const char *p) { + switch (*p++) { case L_ESC: { if (p == ms->p_end) @@ -384,13 +385,19 @@ static int push_captures (MatchState *ms, const char *s, const char *e, LuaMatch } -int str_match (const char *s, size_t ls, const char *p, size_t lp, char **err_msg, LuaMatch *mm) { +int str_match (const char *s, unsigned int ls, const char *p, unsigned int lp, char **err_msg, LuaMatch *mm) { const char *s1 = s; MatchState ms; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ } + + if (setjmp(s_jmp_buf) != 0) { + if (err_msg != NULL) *err_msg = s_msg_buff; + return 0; + } + ms.matchdepth = MAXCCALLS; ms.src_init = s; ms.src_end = s + ls; @@ -405,11 +412,6 @@ int str_match (const char *s, size_t ls, const char *p, size_t lp, char **err_ms } } while (s1++ < ms.src_end && !anchor); - if (setjmp(s_jmp_buf) != 0) { - if (err_msg != NULL) *err_msg = s_msg_buff; - } else { - if (err_msg != NULL) *err_msg = NULL; - } return 0; }