2 Star 0 Fork 0

洪流 / go-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
strings.go 905 Bytes
一键复制 编辑 原始数据 按行查看 历史
洪流 提交于 2023-07-20 11:32 . 新增字符串转换函数
/*
* @Author: hongliu
* @Date: 2022-12-26 17:39:36
* @LastEditors: hongliu
* @LastEditTime: 2023-07-20 11:28:22
* @FilePath: \go-tools\strings\strings.go
* @Description: 字符串处理工具库
*
* Copyright (c) 2022 by 洪流, All Rights Reserved.
*/
package string_tool
import (
"regexp"
"strings"
"unsafe"
)
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
// ToSnakeCase 小驼峰转下划线
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func StringToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
Go
1
https://gitee.com/hongliu9527/go-tools.git
git@gitee.com:hongliu9527/go-tools.git
hongliu9527
go-tools
go-tools
v0.0.8

搜索帮助

53164aa7 5694891 3bd8fe86 5694891