github.com/searKing/golang/go@v1.2.117/database/sql/named.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sql 6 7 import "strings" 8 9 // CompliantName returns a compliant id name 10 // that can be used for a bind or as var. 11 // replace special runes with '_' 12 // a.b -> a_b 13 func CompliantName(in string) string { 14 var buf strings.Builder 15 for i, c := range in { 16 if !isLetter(uint16(c)) { 17 if i == 0 || !isDigit(uint16(c)) { 18 buf.WriteByte('_') 19 continue 20 } 21 } 22 buf.WriteRune(c) 23 } 24 return buf.String() 25 } 26 27 func isDigit(ch uint16) bool { 28 return '0' <= ch && ch <= '9' 29 } 30 func isLetter(ch uint16) bool { 31 return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch == '$' 32 }