mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
// 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 with slashes `\` for chars: '"\.
|
|
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 (`\`).
|
|
// If custom chars `chars` not given, it uses default chars: .\+*?[^]($)
|
|
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()
|
|
}
|