Browse Source

Wrote Oniguruma test

master
Macoy Madson 8 months ago
parent
commit
105866f5cd
  1. 58
      src/Oniguruma.cake

58
src/Oniguruma.cake

@ -6,11 +6,69 @@
(add-c-search-directory-module "cakelisp_cache/OnigurumaInstallDir/include")
(c-import "oniguruma.h"))
(var-global c-onig-free-self-and-contents (const int) 1)
(comptime-cond
('auto-test
(c-import "<string.h>" "<stdio.h>")
(defun test--oniguruma (&return int)
;; Engine setup
(var encodings ([] OnigEncoding) (array ONIG_ENCODING_UTF8))
(onig_initialize encodings (array-size encodings))
;; Set up regex
(var pattern (* (const char)) "(.)[abc](.)")
(var regex (* regex_t))
(var error-info OnigErrorInfo)
(var regex-compile-result int
(onig_new (addr regex) (type-cast pattern (* (const OnigUChar)))
(type-cast (+ pattern (strlen pattern)) (* (const OnigUChar)))
ONIG_OPTION_DEFAULT ONIG_ENCODING_UTF8 ONIG_SYNTAX_DEFAULT
(addr error-info)))
(unless (= regex-compile-result ONIG_NORMAL)
(var error-message ([] ONIG_MAX_ERROR_MESSAGE_LEN char))
(onig_error_code_to_str (type-cast error-message (* UChar))
regex-compile-result (addr error-info))
(fprintf stderr "error: %s\n" error-message)
(return 1))
(var search-string (* (const char)) "The quick brown fox jumps over the lazy dog")
(var start (* (const char)) search-string)
(var end (* (const char)) (+ search-string (strlen search-string)))
(var range (* (const char)) end) ;; Provided to work with reverse searches
(var region (* OnigRegion) (onig_region_new))
(var result int
(onig_search regex (type-cast search-string (* UChar))
(type-cast end (* UChar))
(type-cast start (* UChar))
(type-cast range (* UChar))
region
ONIG_OPTION_NONE))
(cond
((>= result 0)
(each-in-range (path region > num_regs) i
(fprintf stderr "%d: (%d-%d)\n" i (at i (path region > beg))
(at i (path region > end)))))
((= result ONIG_MISMATCH)
(fprintf stderr "Expected to find results, found none\n")
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 1))
(true ;; Result < 0
(var error-message ([] ONIG_MAX_ERROR_MESSAGE_LEN char))
(onig_error_code_to_str (type-cast error-message (* UChar))
result)
(fprintf stderr "error: %s\n" error-message)
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 1)))
;; Clean up
(onig_region_free region c-onig-free-self-and-contents)
(onig_free regex)
(onig_end)
(return 0))))

Loading…
Cancel
Save