You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gstr
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
|
)
|
|
|
|
// AddSlashes quotes chars('"\) with slashes.
|
|
func AddSlashes(str string) string {
|
|
var buf bytes.Buffer
|
|
for _, char := range str {
|
|
switch char {
|
|
case '\'', '"', '\\':
|
|
buf.WriteRune('\\')
|
|
}
|
|
buf.WriteRune(char)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
// StripSlashes un-quotes a quoted string by AddSlashes.
|
|
func StripSlashes(str string) string {
|
|
return utils.StripSlashes(str)
|
|
}
|
|
|
|
// QuoteMeta returns a version of str with a backslash character (\)
|
|
// before every character that is among: .\+*?[^]($)
|
|
func QuoteMeta(str string, chars ...string) string {
|
|
var buf bytes.Buffer
|
|
for _, char := range str {
|
|
if len(chars) > 0 {
|
|
for _, c := range chars[0] {
|
|
if c == char {
|
|
buf.WriteRune('\\')
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
switch char {
|
|
case '.', '+', '\\', '(', '$', ')', '[', '^', ']', '*', '?':
|
|
buf.WriteRune('\\')
|
|
}
|
|
}
|
|
buf.WriteRune(char)
|
|
}
|
|
return buf.String()
|
|
}
|