Source File
quote.go
Belonging Package
strconv
if cap()-len() < len() {
:= make([]byte, len(), len()+1+len()+1)
copy(, )
=
}
= append(, )
for := 0; len() > 0; = [:] {
:= rune([0])
= 1
if >= utf8.RuneSelf {
, = utf8.DecodeRuneInString()
}
if == 1 && == utf8.RuneError {
= append(, `\x`...)
= append(, lowerhex[[0]>>4])
= append(, lowerhex[[0]&0xF])
continue
}
= appendEscapedRune(, , , , )
}
= append(, )
return
}
func ( []byte, rune, byte, , bool) []byte {
= append(, )
if !utf8.ValidRune() {
= utf8.RuneError
}
= appendEscapedRune(, , , , )
= append(, )
return
}
func ( []byte, rune, byte, , bool) []byte {
var [utf8.UTFMax]byte
if == rune() || == '\\' { // always backslashed
= append(, '\\')
= append(, byte())
return
}
if {
if < utf8.RuneSelf && IsPrint() {
= append(, byte())
return
}
} else if IsPrint() || && isInGraphicList() {
:= utf8.EncodeRune([:], )
= append(, [:]...)
return
}
switch {
case '\a':
= append(, `\a`...)
case '\b':
= append(, `\b`...)
case '\f':
= append(, `\f`...)
case '\n':
= append(, `\n`...)
case '\r':
= append(, `\r`...)
case '\t':
= append(, `\t`...)
case '\v':
= append(, `\v`...)
default:
switch {
case < ' ':
= append(, `\x`...)
= append(, lowerhex[byte()>>4])
= append(, lowerhex[byte()&0xF])
case > utf8.MaxRune:
= 0xFFFD
fallthrough
case < 0x10000:
= append(, `\u`...)
for := 12; >= 0; -= 4 {
= append(, lowerhex[>>uint()&0xF])
}
default:
= append(, `\U`...)
for := 28; >= 0; -= 4 {
= append(, lowerhex[>>uint()&0xF])
}
}
}
return
}
func ( rune) string {
return quoteRuneWith(, '\'', false, false)
}
func ( rune) string {
return quoteRuneWith(, '\'', true, false)
}
func ( rune) string {
return quoteRuneWith(, '\'', false, true)
}
func ( string) bool {
for len() > 0 {
, := utf8.DecodeRuneInString()
= [:]
if > 1 {
if == '\ufeff' {
return false // BOMs are invisible and should not be quoted.
}
continue // All other multibyte runes are correctly encoded and assumed printable.
}
if == utf8.RuneError {
return false
}
if ( < ' ' && != '\t') || == '`' || == '\u007F' {
return false
}
}
return true
}
func ( byte) ( rune, bool) {
:= rune()
switch {
case '0' <= && <= '9':
return - '0', true
case 'a' <= && <= 'f':
return - 'a' + 10, true
case 'A' <= && <= 'F':
return - 'A' + 10, true
}
return
}
if len() <= 1 {
= ErrSyntax
return
}
:= [1]
= [2:]
switch {
case 'a':
= '\a'
case 'b':
= '\b'
case 'f':
= '\f'
case 'n':
= '\n'
case 'r':
= '\r'
case 't':
= '\t'
case 'v':
= '\v'
case 'x', 'u', 'U':
:= 0
switch {
case 'x':
= 2
case 'u':
= 4
case 'U':
= 8
}
var rune
if len() < {
= ErrSyntax
return
}
for := 0; < ; ++ {
, := unhex([])
if ! {
= ErrSyntax
return
}
= <<4 |
}
= [:]
=
break
}
if > utf8.MaxRune {
= ErrSyntax
return
}
=
= true
case '0', '1', '2', '3', '4', '5', '6', '7':
:= rune() - '0'
if len() < 2 {
= ErrSyntax
return
}
for := 0; < 2; ++ { // one digit already; two more
:= rune([]) - '0'
if < 0 || > 7 {
= ErrSyntax
return
}
= ( << 3) |
}
= [2:]
if > 255 {
= ErrSyntax
return
}
=
case '\\':
= '\\'
case '\'', '"':
if != {
= ErrSyntax
return
}
= rune()
default:
= ErrSyntax
return
}
=
return
}
if !contains(, '\\') && !contains(, ) {
switch {
case '"':
if utf8.ValidString() {
return , nil
}
case '\'':
, := utf8.DecodeRuneInString()
if == len() && ( != utf8.RuneError || != 1) {
return , nil
}
}
}
var [utf8.UTFMax]byte
:= make([]byte, 0, 3*len()/2) // Try to avoid more allocations.
for len() > 0 {
, , , := UnquoteChar(, )
if != nil {
return "",
}
=
if < utf8.RuneSelf || ! {
= append(, byte())
} else {
:= utf8.EncodeRune([:], )
= append(, [:]...)
}
func ( string, byte) bool {
return bytealg.IndexByteString(, ) != -1
}
if <= 0xFF {
return true
}
return != 0xAD // ...except for the bizarre soft hyphen.
}
return false
}
if 0 <= && < 1<<16 {
, , := uint16(), isPrint16, isNotPrint16
:= bsearch16(, )
if >= len() || < [&^1] || [|1] < {
return false
}
:= bsearch16(, )
return >= len() || [] !=
}
, , := uint32(), isPrint32, isNotPrint32
:= bsearch32(, )
if >= len() || < [&^1] || [|1] < {
return false
}
if >= 0x20000 {
return true
}
-= 0x10000
:= bsearch16(, uint16())
return >= len() || [] != uint16()
}
func ( rune) bool {
if IsPrint() {
return true
}
return isInGraphicList()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin 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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |