123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package util
- import (
- "strings"
- "unicode"
- )
- func IsUpperCase(r rune) bool {
- if r >= 'A' && r <= 'Z' {
- return true
- }
- return false
- }
- func IsLowerCase(r rune) bool {
- if r >= 'a' && r <= 'z' {
- return true
- }
- return false
- }
- func ToSnakeCase(s string) string {
- var out []rune
- for index, r := range s {
- if index == 0 {
- out = append(out, ToLowerCase(r))
- continue
- }
- if IsUpperCase(r) && index != 0 {
- if IsLowerCase(rune(s[index-1])) {
- out = append(out, '_', ToLowerCase(r))
- continue
- }
- if index < len(s)-1 && IsLowerCase(rune(s[index+1])) {
- out = append(out, '_', ToLowerCase(r))
- continue
- }
- out = append(out, ToLowerCase(r))
- continue
- }
- out = append(out, r)
- }
- return string(out)
- }
- func ToCamelCase(s string) string {
- s = ToLower(s)
- out := []rune{}
- for index, r := range s {
- if r == '_' {
- continue
- }
- if index == 0 {
- out = append(out, ToUpperCase(r))
- continue
- }
- if index > 0 && s[index-1] == '_' {
- out = append(out, ToUpperCase(r))
- continue
- }
- out = append(out, r)
- }
- return string(out)
- }
- func ToLowerCase(r rune) rune {
- dx := 'A' - 'a'
- if IsUpperCase(r) {
- return r - dx
- }
- return r
- }
- func ToUpperCase(r rune) rune {
- dx := 'A' - 'a'
- if IsLowerCase(r) {
- return r + dx
- }
- return r
- }
- func ToLower(s string) string {
- var out []rune
- for _, r := range s {
- out = append(out, ToLowerCase(r))
- }
- return string(out)
- }
- func ToUpper(s string) string {
- var out []rune
- for _, r := range s {
- out = append(out, ToUpperCase(r))
- }
- return string(out)
- }
- func LowerFirst(s string) string {
- if len(s) == 0 {
- return s
- }
- return ToLower(s[:1]) + s[1:]
- }
- func UpperFirst(s string) string {
- if len(s) == 0 {
- return s
- }
- return ToUpper(s[:1]) + s[1:]
- }
- func UnExport(text string) bool {
- var flag bool
- str := strings.Map(func(r rune) rune {
- if flag {
- return r
- }
- if unicode.IsLetter(r) {
- flag = true
- return unicode.ToLower(r)
- }
- return r
- }, text)
- return str == text
- }
|