From 1b562b2bff46094520e49f003f4e429d34bae068 Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Wed, 7 Feb 2024 21:11:47 +0330 Subject: [PATCH] use range over integer (new in go 1.22) --- lib/generate_test.go | 12 ++++++------ lib/justify.go | 4 ++-- lib/trie.go | 4 ++-- lib/utils_test.go | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/generate_test.go b/lib/generate_test.go index c8b3320..73d9408 100644 --- a/lib/generate_test.go +++ b/lib/generate_test.go @@ -24,7 +24,7 @@ var ( func getBIP39WordMap() map[string]bool { count := bip39.WordCount() m := make(map[string]bool, count) - for i := 0; i < count; i++ { + for i := range count { word, ok := bip39.GetWord(i) if !ok { panic("NOT OK") @@ -513,7 +513,7 @@ func TestGenerate(t *testing.T) { PassLen: [2]int{14, 14}, Entropy: [2]float64{59.6, 59.7}, Validate: func(p string) bool { - for i := 0; i < 2; i++ { + for i := range 2 { k := 7 * i for _, c := range p[k : k+5] { if c < 'a' || c > 'z' { @@ -568,7 +568,7 @@ func TestGenerate(t *testing.T) { PassLen: [2]int{16, 16}, Entropy: [2]float64{59.6, 59.7}, Validate: func(p string) bool { - for i := 0; i < 2; i++ { + for i := range 2 { k := 8 * i for _, c := range p[k : k+5] { if c < 'a' || c > 'z' { @@ -654,7 +654,7 @@ func TestGenerate(t *testing.T) { PassLen: [2]int{8, 8}, Entropy: [2]float64{16, 16}, Validate: func(p string) bool { - for i := 0; i < len(p); i += 1 { + for i := range len(p) { switch p[i : i+1] { case "a", "b", "c", "d", "e", "f": default: @@ -669,7 +669,7 @@ func TestGenerate(t *testing.T) { PassLen: [2]int{1, 1}, Entropy: [2]float64{3, 3}, Validate: func(p string) bool { - for i := 0; i < len(p); i += 1 { + for i := range len(p) { switch p[i : i+1] { case "a", "b", "c", "d", "e", "f", "g", "h", "i", "j": default: @@ -684,7 +684,7 @@ func TestGenerate(t *testing.T) { PassLen: [2]int{8, 8}, Entropy: [2]float64{24, 24}, Validate: func(p string) bool { - for i := 0; i < len(p); i += 1 { + for i := range len(p) { switch p[i : i+1] { case "a", "b", "c", "d", "e", "f", "g", "h", "i", "j": default: diff --git a/lib/justify.go b/lib/justify.go index 982eb62..9eb6de1 100644 --- a/lib/justify.go +++ b/lib/justify.go @@ -11,7 +11,7 @@ func rjust(in []rune, width int, fillChar rune) []rune { } out := make([]rune, width) fcc := width - len(in) - for i := 0; i < fcc; i++ { + for i := range fcc { out[i] = fillChar } copy(out[fcc:], in) @@ -36,7 +36,7 @@ func center(in []rune, width int, fillChar rune) []rune { } out := make([]rune, width) fcc := int((width - len(in)) / 2) - for i := 0; i < fcc; i++ { + for i := range fcc { out[i] = fillChar } copy(out[fcc:], in) diff --git a/lib/trie.go b/lib/trie.go index 4aea901..3bf1d6b 100644 --- a/lib/trie.go +++ b/lib/trie.go @@ -69,11 +69,11 @@ func (t *Trie) convert(origin string) (result string) { originRune := []rune(origin) result = "" - for l := 0; l < len(originRune); l++ { + for l := range len(originRune) { t = root foundVal := "" depth := 0 - for i := 0; i+l < len(originRune); i++ { + for i := range len(originRune) - l { letter := string(originRune[l+i]) if t.children[letter] == nil { // not found diff --git a/lib/utils_test.go b/lib/utils_test.go index 0632be2..605220a 100644 --- a/lib/utils_test.go +++ b/lib/utils_test.go @@ -38,7 +38,7 @@ func Benchmark_removeDuplicateRunes(b *testing.B) { listLength := 100 count := b.N * 10000 lists := make([][]rune, count) - for i := 0; i < count; i++ { + for i := range count { list := make([]rune, listLength) for j := 0; j < listLength; j++ { list[j] = rune(math_rand.Int32N(256)) @@ -61,7 +61,7 @@ func Benchmark_excludeCharsASCII(b *testing.B) { listLength := 100 count := b.N * 10000 lists := make([][]rune, count) - for i := 0; i < count; i++ { + for i := range count { list := make([]rune, listLength) for j := 0; j < listLength; j++ { list[j] = rune(math_rand.Int32N(256))