summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-14 00:55:41 +0700
committerShulhan <ms@kilabit.info>2021-10-14 00:55:41 +0700
commit8a0352321439e1fe10cfeeebf94372af9c9f7c04 (patch)
treee416055f69def7cd51483d8dafd68808a5d926de
parent9b0464c7b742264202951323b228269c139a5d1f (diff)
downloadpakakeh.go-8a0352321439e1fe10cfeeebf94372af9c9f7c04.tar.xz
lib/ascii: change signature of ToLower and ToUpper
Using pointer to slice on method or function is not a Go idiom. It is created when I still new to Go.
-rw-r--r--lib/ascii/ascii.go24
-rw-r--r--lib/ascii/ascii_example_test.go10
-rw-r--r--lib/ascii/benchmark_test.go42
-rw-r--r--lib/dns/message.go4
-rw-r--r--lib/dns/zone_parser.go14
-rw-r--r--lib/net/resolvconf.go2
6 files changed, 55 insertions, 41 deletions
diff --git a/lib/ascii/ascii.go b/lib/ascii/ascii.go
index 145006a8..b12e8789 100644
--- a/lib/ascii/ascii.go
+++ b/lib/ascii/ascii.go
@@ -110,25 +110,29 @@ func Random(source []byte, n int) []byte {
}
//
-// ToLower convert slice of ASCII characters to lower cases, in places.
+// ToLower convert slice of ASCII characters to lower cases, in places, which
+// means it will return the same slice instead of creating new one.
//
-func ToLower(data *[]byte) {
- for x := 0; x < len(*data); x++ {
- if (*data)[x] < 'A' || (*data)[x] > 'Z' {
+func ToLower(data []byte) []byte {
+ for x := 0; x < len(data); x++ {
+ if data[x] < 'A' || data[x] > 'Z' {
continue
}
- (*data)[x] += 32
+ data[x] += 32
}
+ return data
}
//
-// ToUpper convert slice of ASCII characters to upper cases, in places.
+// ToUpper convert slice of ASCII characters to upper cases, in places, which
+// means it will return the same slice instead of creating new one.
//
-func ToUpper(data *[]byte) {
- for x := 0; x < len(*data); x++ {
- if (*data)[x] < 'a' || (*data)[x] > 'z' {
+func ToUpper(data []byte) []byte {
+ for x := 0; x < len(data); x++ {
+ if data[x] < 'a' || data[x] > 'z' {
continue
}
- (*data)[x] -= 32
+ data[x] -= 32
}
+ return data
}
diff --git a/lib/ascii/ascii_example_test.go b/lib/ascii/ascii_example_test.go
index b835e779..7f651117 100644
--- a/lib/ascii/ascii_example_test.go
+++ b/lib/ascii/ascii_example_test.go
@@ -126,20 +126,14 @@ func ExampleRandom() {
func ExampleToLower() {
in := []byte("@ABCDEFGhijklmnoPQRSTUVWxyz{12345678")
-
- ToLower(&in)
-
- fmt.Println(string(in))
+ fmt.Printf("%s\n", ToLower(in))
// Output:
// @abcdefghijklmnopqrstuvwxyz{12345678
}
func ExampleToUpper() {
in := []byte("@ABCDEFGhijklmnoPQRSTUVWxyz{12345678")
-
- ToUpper(&in)
-
- fmt.Println(string(in))
+ fmt.Printf("%s\n", ToUpper(in))
// Output:
// @ABCDEFGHIJKLMNOPQRSTUVWXYZ{12345678
}
diff --git a/lib/ascii/benchmark_test.go b/lib/ascii/benchmark_test.go
index 1a2d4a4b..f3f95e45 100644
--- a/lib/ascii/benchmark_test.go
+++ b/lib/ascii/benchmark_test.go
@@ -31,19 +31,35 @@ func BenchmarkToLower(b *testing.B) {
b.ResetTimer()
for x := 0; x < b.N; x++ {
- ToLower(&in)
- copy(in, randomInput256)
+ ToLower(in)
+ copy(in, randomInput256) // Copy original input back.
}
}
-//
-// Output of above benchmarks,
-//
-// goos: linux
-// goarch: amd64
-// pkg: github.com/shuLhan/share/lib/ascii
-// BenchmarkToLowerStd-4 2066588 563 ns/op 256 B/op 1 allocs/op
-// BenchmarkToLower-4 5476693 213 ns/op 0 B/op 0 allocs/op
-// PASS
-// ok github.com/shuLhan/share/lib/ascii 3.149s
-//
+/****
+Output of above benchmarks,
+
+=== go version devel go1.18-d38f31d805
+
+Wed 13 Oct 17:45:42 UTC 2021
+
+goos: linux
+goarch: amd64
+pkg: github.com/shuLhan/share/lib/ascii
+cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
+BenchmarkToLowerStd-8 3683070 328.9 ns/op 256 B/op 1 allocs/op
+BenchmarkToLower-8 5221684 232.0 ns/op 0 B/op 0 allocs/op
+PASS
+ok github.com/shuLhan/share/lib/ascii 2.990s
+
+=== go version ???
+
+goos: linux
+goarch: amd64
+pkg: github.com/shuLhan/share/lib/ascii
+BenchmarkToLowerStd-4 2066588 563 ns/op 256 B/op 1 allocs/op
+BenchmarkToLower-4 5476693 213 ns/op 0 B/op 0 allocs/op
+PASS
+ok github.com/shuLhan/share/lib/ascii 3.149s
+
+****/
diff --git a/lib/dns/message.go b/lib/dns/message.go
index ff90e31d..d32f84f4 100644
--- a/lib/dns/message.go
+++ b/lib/dns/message.go
@@ -109,7 +109,7 @@ func NewMessageAddress(hname []byte, addresses [][]byte) (msg *Message) {
return nil
}
- ascii.ToLower(&hname)
+ hname = ascii.ToLower(hname)
rr := ResourceRecord{
Name: string(hname),
@@ -237,7 +237,7 @@ func (msg *Message) packDomainName(dname []byte, doCompress bool) (n int) {
d int
)
- ascii.ToLower(&dname)
+ dname = ascii.ToLower(dname)
msg.dname = string(dname)
if doCompress {
diff --git a/lib/dns/zone_parser.go b/lib/dns/zone_parser.go
index 731f8c7d..059c6cda 100644
--- a/lib/dns/zone_parser.go
+++ b/lib/dns/zone_parser.go
@@ -178,7 +178,7 @@ func (m *zoneParser) parse() (err error) {
m.lineno, m.reader.Rest())
}
- ascii.ToUpper(&tok)
+ tok = ascii.ToUpper(tok)
stok := string(tok)
switch stok {
@@ -272,7 +272,7 @@ func (m *zoneParser) parseDirectiveInclude() (err error) {
var incfile, dname string
- ascii.ToLower(&tok)
+ tok = ascii.ToLower(tok)
incfile = string(tok)
// check if include followed by domain name.
@@ -330,7 +330,7 @@ func (m *zoneParser) parseDirectiveTTL() (err error) {
return fmt.Errorf("line %d: empty $ttl directive", m.lineno)
}
- ascii.ToLower(&tok)
+ tok = ascii.ToLower(tok)
stok := string(tok)
m.ttl, err = parseTTL(tok, stok)
@@ -462,7 +462,7 @@ func (m *zoneParser) parseRR(prevRR *ResourceRecord, tok []byte) (
}
orgtok := libbytes.Copy(tok)
- ascii.ToUpper(&tok)
+ tok = ascii.ToUpper(tok)
stok = string(tok)
switch m.flag {
@@ -630,7 +630,7 @@ func (m *zoneParser) parseRRData(rr *ResourceRecord, tok []byte) (err error) {
}
func (m *zoneParser) parseSOA(rr *ResourceRecord, tok []byte) (err error) {
- ascii.ToLower(&tok)
+ tok = ascii.ToLower(tok)
rrSOA := &RDataSOA{
MName: m.generateDomainName(tok),
@@ -648,7 +648,7 @@ func (m *zoneParser) parseSOA(rr *ResourceRecord, tok []byte) (err error) {
return fmt.Errorf("line %d: Invalid SOA RNAME '%s'", m.lineno, tok)
}
- ascii.ToLower(&tok)
+ tok = ascii.ToLower(tok)
rrSOA.RName = m.generateDomainName(tok)
var v int64
@@ -956,7 +956,7 @@ out:
}
func (m *zoneParser) generateDomainName(dname []byte) (out string) {
- ascii.ToLower(&dname)
+ dname = ascii.ToLower(dname)
switch {
case dname[0] == '@':
out = m.origin
diff --git a/lib/net/resolvconf.go b/lib/net/resolvconf.go
index da435ad2..24c3621f 100644
--- a/lib/net/resolvconf.go
+++ b/lib/net/resolvconf.go
@@ -165,7 +165,7 @@ func (rc *ResolvConf) parse(reader *libio.Reader) {
continue
}
- ascii.ToLower(&tok)
+ tok = ascii.ToLower(tok)
v := string(tok)
switch v {
case "domain":