-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_compdecomp.go
125 lines (98 loc) · 2.97 KB
/
string_compdecomp.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package g
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"io"
)
type (
// A struct that wraps a String for compression.
comp struct{ str String }
// A struct that wraps a String for decompression.
decomp struct{ str String }
)
// Comp returns a comp struct wrapping the given String.
func (s String) Comp() comp { return comp{s} }
// Decomp returns a decomp struct wrapping the given String.
func (s String) Decomp() decomp { return decomp{s} }
// Zlib compresses the wrapped String using the zlib compression algorithm and
// returns the compressed data as a String.
func (c comp) Zlib() String {
// gzcompress() php
buffer := new(bytes.Buffer)
writer := zlib.NewWriter(buffer)
_, _ = writer.Write(c.str.ToBytes())
_ = writer.Flush()
_ = writer.Close()
return String(buffer.String())
}
// Zlib decompresses the wrapped String using the zlib compression algorithm and
// returns the decompressed data as a Result[String].
func (d decomp) Zlib() Result[String] {
// gzuncompress() php
reader, err := zlib.NewReader(d.str.Reader())
if err != nil {
return Err[String](err)
}
defer reader.Close()
buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, reader); err != nil {
return Err[String](err)
}
return Ok(String(buffer.String()))
}
// Gzip compresses the wrapped String using the gzip compression format and
// returns the compressed data as a String.
func (c comp) Gzip() String {
// gzencode() php
buffer := new(bytes.Buffer)
writer := gzip.NewWriter(buffer)
_, _ = writer.Write(c.str.ToBytes())
_ = writer.Flush()
_ = writer.Close()
return String(buffer.String())
}
// Gzip decompresses the wrapped String using the gzip compression format and
// returns the decompressed data as a Result[String].
func (d decomp) Gzip() Result[String] {
// gzdecode() php
reader, err := gzip.NewReader(d.str.Reader())
if err != nil {
return Err[String](err)
}
defer reader.Close()
buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, reader); err != nil {
return Err[String](err)
}
return Ok(String(buffer.String()))
}
// Flate compresses the wrapped String using the flate (zlib) compression algorithm
// and returns the compressed data as a String.
// It accepts an optional compression level. If no level is provided, it defaults to 7.
func (c comp) Flate(level ...int) String {
// gzdeflate() php
buffer := new(bytes.Buffer)
l := 7
if len(level) != 0 {
l = level[0]
}
writer, _ := flate.NewWriter(buffer, l)
_, _ = writer.Write(c.str.ToBytes())
_ = writer.Flush()
_ = writer.Close()
return String(buffer.String())
}
// Flate decompresses the wrapped String using the flate (zlib) compression algorithm
// and returns the decompressed data as a Result[String].
func (d decomp) Flate() Result[String] {
// gzinflate() php
reader := flate.NewReader(d.str.Reader())
defer reader.Close()
buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, reader); err != nil {
return Err[String](err)
}
return Ok(String(buffer.String()))
}