123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // 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()
- }
|