Skip to content

Commit

Permalink
Add option to preserve diaereses; closes gnames#208
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymarsden committed Nov 19, 2021
1 parent f36b72e commit 4b5eeb6
Show file tree
Hide file tree
Showing 24 changed files with 2,900 additions and 2,743 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ and graft-chimaeras like ``+ Crataegomespilus``
: Return more details for a parsed name. This flag is ignored for CSV/TSV
formatting.

``--diaereses -D``
: Preserves diaereses within names, e.g. ``Leptochloöpsis virgata``. The stemmed
canonical name will be generated without diaereses.

``--format -f``
: output format. Can be ``csv``, ``tsv``, ``compact``, ``pretty``.
Default is ``csv``.
Expand Down
Binary file added binding/.DS_Store
Binary file not shown.
112 changes: 58 additions & 54 deletions binding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
package main

/*
#include "stdlib.h"
#include "stdlib.h"
*/
import "C"

import (
"strings"
"unsafe"
"strings"
"unsafe"

"github.com/gnames/gnfmt"
"github.com/gnames/gnparser"
"github.com/gnames/gnfmt"
"github.com/gnames/gnparser"
)

// ParseToString function takes a name-string, desired format, a withDetails
Expand All @@ -23,28 +23,30 @@ import (
// true.
//export ParseToString
func ParseToString(
name *C.char,
f *C.char,
details C.int,
cultivars C.int,
name *C.char,
f *C.char,
details C.int,
cultivars C.int,
diaereses C.int,
) *C.char {
goname := C.GoString(name)
opts := []gnparser.Option{
gnparser.OptFormat(C.GoString(f)),
gnparser.OptWithDetails(int(details) > 0),
gnparser.OptWithCultivars(int(cultivars) > 0),
}
cfg := gnparser.NewConfig(opts...)
gnp := gnparser.New(cfg)
parsed := gnp.ParseName(goname).Output(gnp.Format())
goname := C.GoString(name)
opts := []gnparser.Option{
gnparser.OptFormat(C.GoString(f)),
gnparser.OptWithDetails(int(details) > 0),
gnparser.OptWithCultivars(int(cultivars) > 0),
gnparser.OptWithPreserveDiaereses(int(diaereses) > 0),
}
cfg := gnparser.NewConfig(opts...)
gnp := gnparser.New(cfg)
parsed := gnp.ParseName(goname).Output(gnp.Format())

return C.CString(parsed)
return C.CString(parsed)
}

// FreeMemory takes a string pointer and frees its memory.
//export FreeMemory
func FreeMemory(p *C.char) {
C.free(unsafe.Pointer(p))
C.free(unsafe.Pointer(p))
}

// ParseAryToString function takes an array of names, parsing format, and a
Expand All @@ -54,45 +56,47 @@ func FreeMemory(p *C.char) {
// true.
//export ParseAryToString
func ParseAryToString(
in **C.char,
length C.int,
f *C.char,
details C.int,
cultivars C.int,
in **C.char,
length C.int,
f *C.char,
details C.int,
cultivars C.int,
diaereses C.int,
) *C.char {
names := make([]string, int(length))
names := make([]string, int(length))

opts := []gnparser.Option{
gnparser.OptFormat(C.GoString(f)),
gnparser.OptWithDetails(int(details) > 0),
gnparser.OptWithCultivars(int(cultivars) > 0),
}
start := unsafe.Pointer(in)
pointerSize := unsafe.Sizeof(in)
opts := []gnparser.Option{
gnparser.OptFormat(C.GoString(f)),
gnparser.OptWithDetails(int(details) > 0),
gnparser.OptWithCultivars(int(cultivars) > 0),
gnparser.OptWithPreserveDiaereses(int(diaereses) > 0),
}
start := unsafe.Pointer(in)
pointerSize := unsafe.Sizeof(in)

for i := 0; i < int(length); i++ {
// Copy each input string into a Go string and add it to the slice.
pointer := (**C.char)(unsafe.Pointer(uintptr(start) + uintptr(i)*pointerSize))
name := C.GoString(*pointer)
names[i] = name
}
for i := 0; i < int(length); i++ {
// Copy each input string into a Go string and add it to the slice.
pointer := (**C.char)(unsafe.Pointer(uintptr(start) + uintptr(i)*pointerSize))
name := C.GoString(*pointer)
names[i] = name
}

cfg := gnparser.NewConfig(opts...)
gnp := gnparser.New(cfg)
cfg := gnparser.NewConfig(opts...)
gnp := gnparser.New(cfg)

var res string
parsed := gnp.ParseNames(names)
if gnp.Format() == gnfmt.CSV {
csv := make([]string, length)
for i := range parsed {
csv[i] = parsed[i].Output(gnfmt.CSV)
}
res = strings.Join(csv, "\n")
} else {
json, _ := gnfmt.GNjson{}.Encode(parsed)
res = string(json)
}
return C.CString(res)
var res string
parsed := gnp.ParseNames(names)
if gnp.Format() == gnfmt.CSV {
csv := make([]string, length)
for i := range parsed {
csv[i] = parsed[i].Output(gnfmt.CSV)
}
res = strings.Join(csv, "\n")
} else {
json, _ := gnfmt.GNjson{}.Encode(parsed)
res = string(json)
}
return C.CString(res)
}

func main() {}
Loading

0 comments on commit 4b5eeb6

Please sign in to comment.