代码拉取完成,页面将自动刷新
package xstring
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"unicode"
"unsafe"
)
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func String2Bytes(s string) []byte {
stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: stringHeader.Data,
Len: stringHeader.Len,
Cap: stringHeader.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}
func SubString(str string, start int, end int) string {
tmpLen := len(str)
if start < 0 {
start = 0
}
if end < 0 {
end = tmpLen
}
if start >= tmpLen {
return ""
}
if end >= tmpLen {
end = tmpLen
}
return str[start:end]
}
func ParseCmdLine(str string) []string {
isComment := false
var commentChar byte
var commentFlag bool
var tmpBuf bytes.Buffer
resp := make([]string, 0)
for i := 0; i < len(str); i++ {
c := str[i]
nextC := byte(0)
if i+1 < len(str) {
nextC = str[i+1]
}
if isComment == false {
if c == '\'' || c == '"' {
isComment = true
commentChar = c
commentFlag = true
tmpBuf.WriteByte('*')
continue
}
}
if isComment {
if c == commentChar && nextC == ' ' {
isComment = false
continue
}
tmpBuf.WriteByte(c)
continue
}
if c == ' ' {
if tmpBuf.Len() > 0 {
if commentFlag {
commentFlag = false
resp = append(resp, tmpBuf.String()[1:])
} else {
resp = append(resp, tmpBuf.String())
}
tmpBuf.Reset()
}
} else {
tmpBuf.WriteByte(c)
}
}
if tmpBuf.Len() > 0 {
if commentFlag {
commentFlag = false
resp = append(resp, tmpBuf.String()[1:])
} else {
resp = append(resp, tmpBuf.String())
}
}
return resp
}
// 清除多余字符的函数
func cleanUnneededChar(price string) string {
//统计顶头是否有多余的正/负号,顶头只允许一个正/负号
symbolI, symbolJ := 0, 1
for {
if symbolJ == len(price)-1 {
break
}
if price[symbolI] == '-' || price[symbolI] == '+' {
if price[symbolJ] == '-' || price[symbolJ] == '+' {
symbolI++
symbolJ++
} else {
break
}
} else {
break
}
}
price = price[symbolI:] //裁剪掉顶头多余的正/负号
//检查这个字符串是否带有一个正/负号。如果带有符号,就把符号先单独提取出来
symbolString := ""
if price[0] == '-' || price[0] == '+' {
symbolString = string(price[0])
price = price[1:]
}
if len(price) == 1 {
return symbolString + price
}
//统计顶头有多少个多余的 0
zeroI := 0
for {
if price[zeroI] != '0' {
break
} else {
zeroI++
}
}
//如果这个数值不是浮点数,那么后面的0都是不允许动的
dotIndex := strings.Index(price, ".")
if dotIndex == -1 {
if zeroI > 0 { //zeroI 是统计顶头是否有多余的 0,如果这个值大于了 0,说明顶头有多余的 0
return symbolString + price[zeroI:] //裁剪掉顶头多余的 0,然后带上符号并返回
}
return symbolString + price //顶头没有多余的 0,没必要裁剪了
}
//代码能走到这里说明这个肯定是浮点数了,至少是带有小数点了
//查找一下小数点后面有没有字符。如果没有字符,说明只有孤零零的一个小数点字符,那就把这个多余的小数点清除掉
if price[dotIndex:] == "." {
price = price[zeroI:dotIndex]
}
//只有为浮点数值了,才清除掉末尾多余的0
end := len(price) //最末的下标,往前推
for {
if price[end-1] != '0' {
if price[end-1] == '.' { //判断一下末尾是不是还有多余的 '.'
end--
}
break
} else {
end--
}
}
return symbolString + price[zeroI:end]
}
// 核心功能:从右向左,每隔 3 位,加一个单字符的逗号 ','
func comma(s string) string {
if len(s) <= 3 {
return s
}
return comma(s[:len(s)-3]) + "," + comma(s[len(s)-3:])
}
// 校验这个字符串是不是合法的数值
func validateDigit(s string) bool {
dotCount := 0 //统计小数点有几个,只能出现一个小数点
for _, v := range s {
if v == '.' {
dotCount++
if dotCount > 1 { //只允许有一个小数点存在
return false
}
continue
}
if !unicode.IsDigit(v) {
return false
}
}
return true
}
//将一个数值字符串按金融化输出(每隔 3 位加一个逗号)
/*
思路:暴力遍历的方式
1.清理掉字符串中多余的正/负号、多余的 0。
2.检查这个字符串是否带有符号,有则提取出这个符号,到最后用于拼接。
3.已经清洗完了多余的字符,进行校验,判断这个字符串是否为合法的数值。数值中最多只能有一个小数点`.`、数值中不能出现非数值字符(不考虑科学计数以及 complex 类型)。
4.如果这个字符串是浮点数值,把小数部分,连带小数点全部提取出来,到最后用于拼接。
5.执行核心功能,每隔 3 位,加一个单字符的逗号 `,`(代码来源:《Goc 程序设计语言》第 3.5.4, P54, gopl.io/ch3/comma)。
6.最终的拼接,按顺序将:正/负号、分隔完成的字符串、小数部分,拼接成一个最终的完整字符串。
*/
func FormatFinancialString(price string) string {
//清理掉多余的字符。比如:浮点数末尾的0、开头的0、多余的正/负号
price = cleanUnneededChar(price)
//检查这个字符串是否带有正/负号。如果带有符号,就把符号先单独提取出来
symbolString := ""
if price[0] == '-' || price[0] == '+' {
symbolString = string(price[0])
price = price[1:]
}
//清洗完了多余字符,开始校验这个数值字符串
if !validateDigit(price) {
return "非法的数值!请检查您提供的数值是否正确!数值允许是浮点数,数字的前面一位允许带有一个正/负号!"
}
//小数点前没有写0,就补一个0进去补齐,让数字字符串看起来更好看
if price[0] == '.' {
return "0" + price
}
//判断这个数字是不是浮点数值
dotIndex, decimalString := strings.Index(price, "."), ""
if dotIndex != -1 {
decimalString = price[dotIndex:]
price = price[:dotIndex]
} else if dotIndex == -1 {
dotIndex = len(price)
}
return fmt.Sprintf("%s%s%s", symbolString, comma(price[:dotIndex]), decimalString)
}
// GetPathLastItem 获取文件路径的最后一个项目
func GetPathLastItem(path string) string {
tmpL := len(path)
if tmpL <= 0 {
return path
}
if path[tmpL-1] == '/' || path[tmpL-1] == '\\' {
path = path[:tmpL-1]
}
index := strings.LastIndexByte(path, '/')
index2 := strings.LastIndexByte(path, '\\')
if index2 > index {
index = index2
}
if index <= 0 {
return ""
}
return path[index+1:]
}
// similar_text()
func SimilarText(first, second string, percent *float64) int {
var similarText func(string, string, int, int) int
similarText = func(str1, str2 string, len1, len2 int) int {
var sum, max int
pos1, pos2 := 0, 0
// Find the longest segment of the same section in two strings
for i := 0; i < len1; i++ {
for j := 0; j < len2; j++ {
for l := 0; (i+l < len1) && (j+l < len2) && (str1[i+l] == str2[j+l]); l++ {
if l+1 > max {
max = l + 1
pos1 = i
pos2 = j
}
}
}
}
if sum = max; sum > 0 {
if pos1 > 0 && pos2 > 0 {
sum += similarText(str1, str2, pos1, pos2)
}
if (pos1+max < len1) && (pos2+max < len2) {
s1 := []byte(str1)
s2 := []byte(str2)
sum += similarText(string(s1[pos1+max:]), string(s2[pos2+max:]), len1-pos1-max, len2-pos2-max)
}
}
return sum
}
l1, l2 := len(first), len(second)
if l1+l2 == 0 {
return 0
}
sim := similarText(first, second, l1, l2)
if percent != nil {
*percent = float64(sim*200) / float64(l1+l2)
}
return sim
}
/*
1字节 0xxxxxxx
2字节 110xxxxx 10xxxxxx 0xC0
3字节 1110xxxx 10xxxxxx 10xxxxxx 0xE0
4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 0xF0
5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0xF8
6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 0xFC
*/
func SplitUTF8Char(str string, c byte) []string {
var retStr []string
pos := 0
for i := 0; i < len(str); i++ {
if (str[i]&0x80) == 0x80 && str[i] == c {
if pos == i {
retStr = append(retStr, "")
} else {
retStr = append(retStr, string(str[pos:i]))
}
pos = i + 1
continue
}
if (str[i] & 0xC0) == 0xC0 {
i += 1
continue
}
if (str[i] & 0xE0) == 0xE0 {
i += 2
continue
}
if (str[i] & 0xF0) == 0xF0 {
i += 3
continue
}
if (str[i] & 0xF8) == 0xF8 {
i += 4
continue
}
if (str[i] & 0xC0) == 0xFC {
i += 5
continue
}
if str[i] == c {
if pos == i {
retStr = append(retStr, "")
} else {
retStr = append(retStr, string(str[pos:i]))
}
pos = i + 1
continue
}
}
retStr = append(retStr, strings.Trim(str[pos:], "\r\n"))
return retStr
}
// FileExist 检查文件或目录是否存在
// 如果由 filename 指定的文件或目录存在则返回 true,否则返回 false
func FileExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
func ConnPath(args ...string) string {
if len(args) <= 0 {
return ""
}
retStr := ""
for i := 0; i < len(args); i++ {
if args[i] == "" {
continue
}
tmpStr := args[i]
if i != 0 {
if strings.Index(tmpStr, "../") == 0 || strings.Index(tmpStr, "..\\") == 0 {
tmpStr = "/" + tmpStr
}
}
if tmpStr == "" {
continue
}
if retStr == "" {
retStr = tmpStr
} else {
l := len(retStr)
if (retStr[l-1] == '/' || retStr[l-1] == '\\') && (tmpStr[0] == '/' || tmpStr[0] == '\\') {
retStr += tmpStr[1:]
} else if (retStr[l-1] == '/' || retStr[l-1] == '\\') && tmpStr[0] != '/' && tmpStr[0] != '\\' {
retStr += tmpStr
} else if retStr[l-1] != '/' && retStr[l-1] != '\\' && (tmpStr[0] == '/' || tmpStr[0] == '\\') {
retStr += tmpStr
} else {
retStr += "/" + tmpStr
}
}
}
absPath, err := filepath.Abs(retStr)
if err == nil && FileExist(absPath) {
return absPath
}
return retStr
}
// GetFileName 获取文件的名称
func GetFileName(filename string) string {
index := strings.LastIndex(filename, "/")
index2 := strings.LastIndex(filename, "\\")
if index2 > index {
index = index2
}
name := ""
if index >= 0 {
name = filename[index+1:]
} else {
name = filename
}
return name
}
func GetFileExt(str string) string {
return path.Ext(str)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。