Skip to content

Commit

Permalink
Change to not use unsafe package
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
naoina committed Nov 18, 2015
1 parent 7cf1d11 commit 6b638e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 17 additions & 0 deletions da.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sync"
"unicode"
"unicode/utf8"
"unsafe"
)

var (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6b638e9

Please sign in to comment.