From 6b638e95a32d0c1131db0e7fe83775cbea4a0d0b Mon Sep 17 00:00:00 2001 From: Naoya Inada Date: Thu, 19 Nov 2015 08:44:43 +0900 Subject: [PATCH] Change to not use unsafe package Closes #1 --- da.go | 17 +++++++++++++++++ strings.go | 9 ++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/da.go b/da.go index b10342b..8fe6516 100644 --- a/da.go +++ b/da.go @@ -86,6 +86,23 @@ func (da *doubleArray) Lookup(path string) (length int) { return -1 } +func (da *doubleArray) LookupByBytes(path []byte) (length int) { + idx := 1 + tmpIdx := idx + for i := 0; i < len(path); i++ { + c := path[i] + tmpIdx = da.nextIndex(da.bc[tmpIdx].Base(), c) + if tmpIdx >= len(da.bc) || da.bc[tmpIdx].Check() != c { + break + } + idx = tmpIdx + } + if next := da.nextIndex(da.bc[idx].Base(), terminationCharacter); next < len(da.bc) && da.bc[next].Check() == terminationCharacter { + return da.node[da.bc[next].Base()] + } + return -1 +} + func (da *doubleArray) build(srcs []record, idx, depth int, usedBase map[int]struct{}) error { sort.Stable(recordSlice(srcs)) base, siblings, leaf, err := da.arrange(srcs, idx, depth, usedBase) diff --git a/strings.go b/strings.go index 3cc2ab7..881ca2c 100644 --- a/strings.go +++ b/strings.go @@ -4,7 +4,6 @@ import ( "sync" "unicode" "unicode/utf8" - "unsafe" ) var ( @@ -80,7 +79,7 @@ func ToUpperCamelCase(s string) string { initialism = append(initialism, runeBuf[:n]...) } } - if length := commonInitialism.Lookup(*(*string)(unsafe.Pointer(&initialism))); length > 0 { + if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } start = len(result) @@ -113,7 +112,7 @@ func ToUpperCamelCase(s string) string { initialism = append(initialism, runeBuf[:n]...) } } - if length := commonInitialism.Lookup(*(*string)(unsafe.Pointer(&initialism))); length > 0 { + if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } return string(result) @@ -140,7 +139,7 @@ func ToUpperCamelCaseASCII(s string) string { for _, b := range candidate { initialism = append(initialism, toUpperASCII(b)) } - if length := commonInitialism.Lookup(*(*string)(unsafe.Pointer(&initialism))); length > 0 { + if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } start = len(result) @@ -158,7 +157,7 @@ func ToUpperCamelCaseASCII(s string) string { for _, b := range candidate { initialism = append(initialism, toUpperASCII(b)) } - if length := commonInitialism.Lookup(*(*string)(unsafe.Pointer(&initialism))); length > 0 { + if length := commonInitialism.LookupByBytes(initialism); length > 0 { result = append(result[:start], initialism...) } return string(result)