-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_compdecomp.go
186 lines (145 loc) · 4.51 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package g
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"io"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
)
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} }
// Zstd compresses the wrapped String using the zstd compression algorithm and
// returns the compressed data as a String.
func (c comp) Zstd() String {
buffer := new(bytes.Buffer)
writer, _ := zstd.NewWriter(buffer)
_, _ = writer.Write(c.str.ToBytes())
_ = writer.Flush()
_ = writer.Close()
return String(buffer.String())
}
// Zstd decompresses the wrapped String using the zstd compression algorithm and
// returns the decompressed data as a Result[String].
func (d decomp) Zstd() Result[String] {
reader, err := zstd.NewReader(d.str.Reader())
if err != nil {
reader.Close()
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()))
}
// Brotli compresses the wrapped String using the Brotli compression algorithm and
// returns the compressed data as a String.
func (c comp) Brotli() String {
buffer := new(bytes.Buffer)
writer := brotli.NewWriter(buffer)
_, _ = writer.Write(c.str.ToBytes())
_ = writer.Flush()
_ = writer.Close()
return String(buffer.String())
}
// Brotli decompresses the wrapped String using the Brotli compression algorithm and
// returns the decompressed data as a Result[String].
func (d decomp) Brotli() Result[String] {
reader := brotli.NewReader(d.str.Reader())
buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, reader); err != nil {
return Err[String](err)
}
return Ok(String(buffer.String()))
}
// 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()))
}