Source File
encodedword.go
Belonging Package
mime
package mime
import (
)
type WordEncoder byte
QEncoding = WordEncoder('q')
)
var (
errInvalidWord = errors.New("mime: invalid RFC 2047 encoded-word")
)
func ( WordEncoder) (, string) string {
if !needsEncoding() {
return
}
return .encodeWord(, )
}
func ( string) bool {
for , := range {
if ( < ' ' || > '~') && != '\t' {
return true
}
}
return false
}
func ( WordEncoder) (, string) string {
maxContentLen = maxEncodedWordLen - len("=?UTF-8?q?") - len("?=")
)
var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen)
func ( WordEncoder) ( *strings.Builder, , string) {
if !isUTF8() || base64.StdEncoding.EncodedLen(len()) <= maxContentLen {
io.WriteString(, )
.Close()
return
}
var , , int
_, = utf8.DecodeRuneInString([:])
if + <= maxBase64Len {
+=
} else {
io.WriteString(, [:])
.Close()
.splitWord(, )
=
=
}
}
io.WriteString(, [:])
.Close()
}
if !isUTF8() {
writeQString(, )
return
}
var , int
for := 0; < len(); += {
var int
if >= ' ' && <= '~' && != '=' && != '?' && != '_' {
, = 1, 1
} else {
_, = utf8.DecodeRuneInString([:])
= 3 *
}
if + > maxContentLen {
.splitWord(, )
= 0
}
writeQString(, [:+])
+=
}
}
func ( WordEncoder) ( *strings.Builder, string) {
.WriteString("=?")
.WriteString()
.WriteByte('?')
.WriteByte(byte())
.WriteByte('?')
}
func ( *strings.Builder) {
.WriteString("?=")
}
:= [:]
if len() == 0 {
return "", errInvalidWord
}
if len() < +3 {
return "", errInvalidWord
}
:= strings.Index(, "=?")
if == -1 {
return , nil
}
var strings.Builder
.WriteString([:])
= [:]
:= false
for {
:= strings.Index(, "=?")
if == -1 {
break
}
:= + len("=?")
:= strings.Index([:], "?")
if == -1 {
break
}
:= [ : +]
+= + len("?")
if len() < +len("Q??=") {
break
}
:= []
++
if [] != '?' {
break
}
++
:= strings.Index([:], "?=")
if == -1 {
break
}
:= [ : +]
:= + + len("?=")
, := decode(, )
if != nil {
= false
.WriteString([:+2])
= [+2:]
continue
}
if > 0 && (! || hasNonWhitespace([:])) {
.WriteString([:])
}
if := .convert(&, , ); != nil {
return "",
}
= [:]
= true
}
if len() > 0 {
.WriteString()
}
return .String(), nil
}
func ( byte, string) ([]byte, error) {
switch {
case 'B', 'b':
return base64.StdEncoding.DecodeString()
case 'Q', 'q':
return qDecode()
default:
return nil, errInvalidWord
}
}
func ( *WordDecoder) ( *strings.Builder, string, []byte) error {
switch {
case strings.EqualFold("utf-8", ):
.Write()
case strings.EqualFold("iso-8859-1", ):
for , := range {
.WriteRune(rune())
}
case strings.EqualFold("us-ascii", ):
for , := range {
if >= utf8.RuneSelf {
.WriteRune(unicode.ReplacementChar)
} else {
.WriteByte()
}
}
default:
if .CharsetReader == nil {
return fmt.Errorf("mime: unhandled charset %q", )
}
, := .CharsetReader(strings.ToLower(), bytes.NewReader())
if != nil {
return
}
if _, = io.Copy(, ); != nil {
return
}
}
return nil
}
func ( string) ([]byte, error) {
:= make([]byte, len())
:= 0
for := 0; < len(); ++ {
switch := []; {
case == '_':
[] = ' '
case == '=':
if +2 >= len() {
return nil, errInvalidWord
}
, := readHexByte([+1], [+2])
if != nil {
return nil,
}
[] =
+= 2
case ( <= '~' && >= ' ') || == '\n' || == '\r' || == '\t':
[] =
default:
return nil, errInvalidWord
}
++
}
return [:], nil
}
![]() |
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. |