From 3d40bc8416e31e56f9908f36982fdafb609410b3 Mon Sep 17 00:00:00 2001 From: ICHINOSE Shogo Date: Tue, 16 Jan 2024 00:26:16 +0900 Subject: [PATCH] Refactor string handling in lattice.go and mecab.go --- lattice.go | 6 ++---- mecab.go | 12 ++++-------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lattice.go b/lattice.go index 6225c35..9fc436f 100644 --- a/lattice.go +++ b/lattice.go @@ -120,11 +120,9 @@ func (l Lattice) SetSentence(s string) { panic(errLatticeNotAvailable) } length := C.size_t(len(s)) - if s == "" { - s = "dummy" - } + input := C.CString(s) + defer C.free(unsafe.Pointer(input)) - input := (*C.char)(unsafe.Pointer(unsafe.StringData(s))) C.mecab_lattice_add_request_type(l.l.lattice, 64) // MECAB_ALLOCATE_SENTENCE = 64 C.mecab_lattice_set_sentence2(l.l.lattice, input, length) runtime.KeepAlive(l.l) diff --git a/mecab.go b/mecab.go index 3f59256..b2d6024 100644 --- a/mecab.go +++ b/mecab.go @@ -96,11 +96,9 @@ func (m MeCab) Parse(s string) (string, error) { panic(errMeCabNotAvailable) } length := C.size_t(len(s)) - if s == "" { - s = "dummy" - } + input := C.CString(s) + defer C.free(unsafe.Pointer(input)) - input := (*C.char)(unsafe.Pointer(unsafe.StringData(s))) result := C.mecab_sparse_tostr2(m.m.mecab, input, length) if result == nil { return "", newError(m.m.mecab) @@ -136,11 +134,9 @@ func (m MeCab) ParseToNode(s string) (Node, error) { panic(errMeCabNotAvailable) } length := C.size_t(len(s)) - if s == "" { - s = "dummy" - } + input := C.CString(s) + defer C.free(unsafe.Pointer(input)) - input := (*C.char)(unsafe.Pointer(unsafe.StringData(s))) node := C.mecab_sparse_tonode2(m.m.mecab, input, length) if node == nil { return Node{}, newError(m.m.mecab)