go 导入c的 C.cre2_options_t,暂时没跑完

package main

/*
#cgo LDFLAGS: -lre2 -lcre2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cre2.h>
*/
import (
	"C"
	"fmt"
	"unsafe"
)

func main() {
	var (
		cre2Opt  *C.cre2_options_t
		set2     *C.cre2_set
		pattern0 = "hello"
		pattern1 = "world"
		pattern2 = "po.*ar"
		pattern3 = "invalid"
		match    [3]C.int
		count    C.size_t
	)
	cre2Opt = (*C.cre2_options_t)(unsafe.Pointer(C.cre2_opt_new()))
	set2 = C.cre2_set_new(unsafe.Pointer(cre2Opt), C.CRE2_UNANCHORED)
	C.cre2_set_add_simple(set2, C.CString(pattern0))
	C.cre2_set_add_simple(set2, C.CString(pattern1))
	C.cre2_set_add_simple(set2, C.CString(pattern2))
	C.cre2_set_add_simple(set2, C.CString(pattern3))
	C.cre2_set_compile(set2)

	// Test second match
	text := "blabla hello polar bear!"
	count = C.cre2_set_match(set2, C.CString(text), C.ulong(len(text)), (*C.int)(unsafe.Pointer(&match)), C.size_t(len(match)))
	if count != 2 || match[0] != 0 || match[1] != 2 {
		fmt.Printf("Failed to match: %s", text)
	} else {
		fmt.Printf("Correctly matched: %s\n", text)
	}

	C.cre2_opt_delete(unsafe.Pointer(cre2Opt))
	C.cre2_set_delete(set2)
}
posted @ 2023-03-04 23:54  ty1539  阅读(14)  评论(0编辑  收藏  举报