123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package gdnative
- import "C"
- import (
- "strings"
- "unicode"
- "unsafe"
- )
- // unsafeToGoString will convert a null pointer with an underlying CString into
- // a Go string.
- func unsafeToGoString(p unsafe.Pointer) string {
- // Cast the pointer to a CString.
- cString := (*C.char)(p)
- goString := C.GoString(cString)
- return goString
- }
- // camelToSnake will convert the given string from camelcase to snake case.
- // Attribution:
- // Author: https://github.com/mantenie
- // Source: https://github.com/serenize/snaker
- func camelToSnake(s string) string { //nolint:deadcode,unused
- var result string
- var words []string
- var lastPos int
- rs := []rune(s)
- for i := 0; i < len(rs); i++ {
- if i > 0 && unicode.IsUpper(rs[i]) {
- if initialism := startsWithInitialism(s[lastPos:]); initialism != "" {
- words = append(words, initialism)
- i += len(initialism) - 1
- lastPos = i
- continue
- }
- words = append(words, s[lastPos:i])
- lastPos = i
- }
- }
- // append the last word
- if s[lastPos:] != "" {
- words = append(words, s[lastPos:])
- }
- for k, word := range words {
- if k > 0 {
- result += "_"
- }
- result += strings.ToLower(word)
- }
- return result
- }
- // startsWithInitialism returns the initialism if the given string begins with it
- func startsWithInitialism(s string) string { //nolint:unused
- var initialism string
- // the longest initialism is 5 char, the shortest 2
- for i := 1; i <= 5; i++ {
- if len(s) > i-1 && commonInitialisms[s[:i]] {
- initialism = s[:i]
- }
- }
- return initialism
- }
- // commonInitialisms, taken from
- // https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
- var commonInitialisms = map[string]bool{ //nolint:unused
- "ACL": true,
- "API": true,
- "ASCII": true,
- "CPU": true,
- "CSS": true,
- "DNS": true,
- "EOF": true,
- "GUID": true,
- "HTML": true,
- "HTTP": true,
- "HTTPS": true,
- "ID": true,
- "IP": true,
- "JSON": true,
- "LHS": true,
- "OS": true,
- "QPS": true,
- "RAM": true,
- "RHS": true,
- "RPC": true,
- "SLA": true,
- "SMTP": true,
- "SQL": true,
- "SSH": true,
- "TCP": true,
- "TLS": true,
- "TTL": true,
- "UDP": true,
- "UI": true,
- "UID": true,
- "UUID": true,
- "URI": true,
- "URL": true,
- "UTF8": true,
- "VM": true,
- "XML": true,
- "XMPP": true,
- "XSRF": true,
- "XSS": true,
- }
|