File: unicode.go

package info (click to toggle)
golang-github-dromara-dongle 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,440 kB
  • sloc: makefile: 4
file content (51 lines) | stat: -rw-r--r-- 866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package coding

import (
	"io"

	"github.com/dromara/dongle/coding/unicode"
)

// ByUnicode encodes by unicode.
func (e Encoder) ByUnicode() Encoder {
	if e.Error != nil {
		return e
	}

	// Streaming encoding mode
	if e.reader != nil {
		e.dst, e.Error = e.stream(func(w io.Writer) io.WriteCloser {
			return unicode.NewStreamEncoder(w)
		})
		return e
	}

	// Standard encoding mode
	if len(e.src) > 0 {
		e.dst = unicode.NewStdEncoder().Encode(e.src)
	}

	return e
}

// ByUnicode decodes by unicode.
func (d Decoder) ByUnicode() Decoder {
	if d.Error != nil {
		return d
	}

	// Streaming decoding mode
	if d.reader != nil {
		d.dst, d.Error = d.stream(func(r io.Reader) io.Reader {
			return unicode.NewStreamDecoder(r)
		})
		return d
	}

	// Standard decoding mode
	if len(d.src) > 0 {
		d.dst, d.Error = unicode.NewStdDecoder().Decode(d.src)
	}

	return d
}