From 2ee74e7c670b2ae5d40a671f40d62a6a2ef0f35f Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 6 Mar 2024 17:52:57 -0800 Subject: [PATCH] legacy/helper/hashcode: Entirely remove This package is no longer called from anywhere in this codebase. --- internal/legacy/helper/hashcode/hashcode.go | 38 ------------------ .../legacy/helper/hashcode/hashcode_test.go | 40 ------------------- 2 files changed, 78 deletions(-) delete mode 100644 internal/legacy/helper/hashcode/hashcode.go delete mode 100644 internal/legacy/helper/hashcode/hashcode_test.go diff --git a/internal/legacy/helper/hashcode/hashcode.go b/internal/legacy/helper/hashcode/hashcode.go deleted file mode 100644 index 6e18b525752d..000000000000 --- a/internal/legacy/helper/hashcode/hashcode.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package hashcode - -import ( - "bytes" - "fmt" - "hash/crc32" -) - -// String hashes a string to a unique hashcode. -// -// crc32 returns a uint32, but for our use we need -// a non negative integer. Here we cast to an integer -// and invert it if the result is negative. -func String(s string) int { - v := int(crc32.ChecksumIEEE([]byte(s))) - if v >= 0 { - return v - } - if -v >= 0 { - return -v - } - // v == MinInt - return 0 -} - -// Strings hashes a list of strings to a unique hashcode. -func Strings(strings []string) string { - var buf bytes.Buffer - - for _, s := range strings { - buf.WriteString(fmt.Sprintf("%s-", s)) - } - - return fmt.Sprintf("%d", String(buf.String())) -} diff --git a/internal/legacy/helper/hashcode/hashcode_test.go b/internal/legacy/helper/hashcode/hashcode_test.go deleted file mode 100644 index 2f9d1f6650ce..000000000000 --- a/internal/legacy/helper/hashcode/hashcode_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: BUSL-1.1 - -package hashcode - -import ( - "testing" -) - -func TestString(t *testing.T) { - v := "hello, world" - expected := String(v) - for i := 0; i < 100; i++ { - actual := String(v) - if actual != expected { - t.Fatalf("bad: %#v\n\t%#v", actual, expected) - } - } -} - -func TestStrings(t *testing.T) { - v := []string{"hello", ",", "world"} - expected := Strings(v) - for i := 0; i < 100; i++ { - actual := Strings(v) - if actual != expected { - t.Fatalf("bad: %#v\n\t%#v", actual, expected) - } - } -} - -func TestString_positiveIndex(t *testing.T) { - // "2338615298" hashes to uint32(2147483648) which is math.MinInt32 - ips := []string{"192.168.1.3", "192.168.1.5", "2338615298"} - for _, ip := range ips { - if index := String(ip); index < 0 { - t.Fatalf("Bad Index %#v for ip %s", index, ip) - } - } -}