package shellquoteimport ()// Join quotes each argument and joins them with a space.// If passed to /bin/sh, the resulting string will be split back into the// original arguments.func ( ...string) string {varbytes.Bufferfor , := range {if != 0 { .WriteByte(' ') }quote(, &) }return .String()}const ( specialChars = "\\'\"`${[|&;<>()*?!" extraSpecialChars = " \t\n" prefixChars = "~")func quote( string, *bytes.Buffer) {// We want to try to produce a "nice" output. As such, we will // backslash-escape most characters, but if we encounter a space, or if we // encounter an extra-special char (which doesn't work with // backslash-escaping) we switch over to quoting the whole word. We do this // with a space because it's typically easier for people to read multi-word // arguments when quoted with a space rather than with ugly backslashes // everywhere. := .Len()iflen() == 0 {// oops, no content .WriteString("''")return } , := , := trueforlen() > 0 { , := utf8.DecodeRuneInString() = [:]ifstrings.ContainsRune(specialChars, ) || ( && strings.ContainsRune(prefixChars, )) {// copy the non-special chars up to this pointiflen() < len() { .WriteString([0 : len()-len()-]) } .WriteByte('\\') .WriteRune() = } elseifstrings.ContainsRune(extraSpecialChars, ) {// start over in quote mode .Truncate()goto } = false }iflen() > 0 { .WriteString() }return:// quote mode // Use single-quotes, but if we find a single-quote in the word, we need // to terminate the string, emit an escaped quote, and start the string up // again := falseforlen() > 0 { := strings.IndexRune(, '\'')if == -1 {break }if > 0 {if ! { .WriteByte('\'') = true } .WriteString([0:]) } = [+1:]if { .WriteByte('\'') = false } .WriteString("\\'") }iflen() > 0 {if ! { .WriteByte('\'') } .WriteString() .WriteByte('\'') }}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.