Skip to content

Commit

Permalink
Merge pull request #4 from shogo82148/use-string-header
Browse files Browse the repository at this point in the history
use reflect.StringHeader
  • Loading branch information
shogo82148 authored Dec 31, 2018
2 parents 6eae0bf + a840019 commit fed2fcf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package mecab
import "C"
import (
"errors"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -59,7 +60,9 @@ func (l Lattice) SetSentence(s string) {
if s == "" {
s = "dummy"
}
input := *(**C.char)(unsafe.Pointer(&s))
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))

C.mecab_lattice_add_request_type(l.lattice, 64) // MECAB_ALLOCATE_SENTENCE = 64
C.mecab_lattice_set_sentence2(l.lattice, input, length)
}
Expand Down
8 changes: 6 additions & 2 deletions mecab.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "C"
import (
"errors"
"fmt"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -59,7 +60,9 @@ func (m MeCab) Parse(s string) (string, error) {
if s == "" {
s = "dummy"
}
input := *(**C.char)(unsafe.Pointer(&s))
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))

result := C.mecab_sparse_tostr2(m.mecab, input, length)
if result == nil {
return "", m.Error()
Expand All @@ -86,7 +89,8 @@ func (m MeCab) ParseToNode(s string) (Node, error) {
if s == "" {
s = "dummy"
}
input := *(**C.char)(unsafe.Pointer(&s))
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))

node := C.mecab_sparse_tonode2(m.mecab, input, length)
if node == nil {
Expand Down

0 comments on commit fed2fcf

Please sign in to comment.