~seirdy/moac

e561b8675a43d8a9030719a1329898692a2300a7 — Rohan Kumar 5 months ago 805d2b3
Chore(style): more descriptive varnames

Some varnames had confusing names, which were enough to confuse the
maintainer after a couple weeks away from the project. The varnamelen
linter recently introduced to golangci-lint helped pinpoint
non-descriptive names used across a nontrivial number of lines.

Single-letter names used across fewer than 5 lines are fine, though.
5 files changed, 28 insertions(+), 28 deletions(-)

M charsets/buildcharsets_test.go
M charsets/charsets.go
M entropy/entropy.go
M givens_test.go
M pwgen/pwgen.go
M charsets/buildcharsets_test.go => charsets/buildcharsets_test.go +4 -4
@@ 101,11 101,11 @@ func buildCharsetsTables() []buildCharsetsTestCase { //nolint:funlen // single s

func formerNotFoundInLatter(former, latter charsets.CharsetCollection) (missing charsets.CharsetCollection) {
	// for each charset in former: add to missing if it isn't in latter
	for _, f := range former {
	for _, formerItem := range former {
		isMissing := true

		for _, l := range latter {
			if f.String() == l.String() {
		for _, latterItem := range latter {
			if formerItem.String() == latterItem.String() {
				isMissing = false

				break


@@ 113,7 113,7 @@ func formerNotFoundInLatter(former, latter charsets.CharsetCollection) (missing 
		}

		if isMissing {
			missing = append(missing, f)
			missing = append(missing, formerItem)
		}
	}


M charsets/charsets.go => charsets/charsets.go +14 -14
@@ 42,16 42,16 @@ func (cc *CustomCharset) dedupe() {
		return
	}

	n := 1
	removals := 1

	for i := 1; i < len(*cc); i++ {
		if (*cc)[i] != (*cc)[i-1] {
			(*cc)[n] = (*cc)[i]
			n++
			(*cc)[removals] = (*cc)[i]
			removals++
		}
	}

	*cc = (*cc)[:n]
	*cc = (*cc)[:removals]
}

// CharsetCollection holds a list of charsets, and can bulk-convert them to strings, runes, and names.


@@ 146,17 146,17 @@ func parseCharset(cs *CharsetCollection, charsetName string) {
}

//nolint:gocritic // c1 is ptr bc it's modified
func minimizeRedundancyInLatter(c1 *Charset, c2 *CustomCharset) {
	c1c := CustomCharset((*c1).Runes())
	moveOverlapToSmaller(&c1c, c2)
func minimizeRedundancyInLatter(former *Charset, latter *CustomCharset) {
	c1c := CustomCharset((*former).Runes())
	moveOverlapToSmaller(&c1c, latter)

	if len(c1c) == 0 {
		*c1, *c2 = *c2, c1c
		*former, *latter = *latter, c1c

		return
	}

	*c1 = c1c
	*former = c1c
}

func moveOverlapToSmaller(c1, c2 *CustomCharset) {


@@ 168,14 168,14 @@ func moveOverlapToSmaller(c1, c2 *CustomCharset) {
		deleteFromMe = c2
	}

	for i := 0; i < len(*deleteFromMe); i++ {
		for j := 0; j < len(*preserveMe); j++ {
			if (*deleteFromMe)[i] != (*preserveMe)[j] {
	for delI := 0; delI < len(*deleteFromMe); delI++ {
		for presI := 0; presI < len(*preserveMe); presI++ {
			if (*deleteFromMe)[delI] != (*preserveMe)[presI] {
				continue
			}

			*deleteFromMe = append((*deleteFromMe)[:i], (*deleteFromMe)[i+1:]...)
			i--
			*deleteFromMe = append((*deleteFromMe)[:delI], (*deleteFromMe)[delI+1:]...)
			delI--

			break
		}

M entropy/entropy.go => entropy/entropy.go +2 -2
@@ 18,14 18,14 @@ import (
func Entropy(password string) float64 {
	charsetsUsed := findCharsetsUsed(password)

	e, err := FromCharsets(charsetsUsed, utf8.RuneCountInString(password))
	entropy, err := FromCharsets(charsetsUsed, utf8.RuneCountInString(password))
	// Should be impossible for FromCharsets to return an error when
	// charsetsUsed cannot be too long. If there's an error, we have a bug.
	if err != nil {
		log.Panicf("error measuring generated password entropy: %v", err)
	}

	return e
	return entropy
}

// FromCharsets computes the number of entropy bits in a string

M givens_test.go => givens_test.go +7 -7
@@ 240,17 240,17 @@ func TestMinEntropy(t *testing.T) {
	for i := range tcs {
		t.Run(tcs[i].name, func(t *testing.T) {
			test := tcs[i]
			me, errME := test.given.MinEntropy()
			meq, errMEQ := test.given.MinEntropyQuantum()
			minEnt, errMinEnt := test.given.MinEntropy()
			minEntQ, errMinEntQ := test.given.MinEntropyQuantum()

			validateErrors(t, errME, errMEQ, test.expectedErrME, "MinEntropy")
			validateErrors(t, errMinEnt, errMinEntQ, test.expectedErrME, "MinEntropy")

			if beyondAcceptableMargin(me, test.expectedME) {
				t.Errorf("MinEntropy() = %.4g; want %.4g", me, test.expectedME)
			if beyondAcceptableMargin(minEnt, test.expectedME) {
				t.Errorf("MinEntropy() = %.4g; want %.4g", minEnt, test.expectedME)
			}

			if beyondAcceptableMargin(meq, me*2) {
				t.Errorf("MinEntropyQuantum() = %.4g; want %.4g", meq, me*2)
			if beyondAcceptableMargin(minEntQ, minEnt*2) {
				t.Errorf("MinEntropyQuantum() = %.4g; want %.4g", minEntQ, minEnt*2)
			}
		})
	}

M pwgen/pwgen.go => pwgen/pwgen.go +1 -1
@@ 96,7 96,7 @@ func computePasswordLength(charsetSize int, pwEntropy float64, minLen, maxLen in
func computeSpecialIndexes(pwLength, charsetCount int) []int {
	res := make([]int, charsetCount)

	for i := 0; i < charsetCount; i++ {
	for i := 0; i < charsetCount; i++ { //nolint:varnamelen // the only index var
		newInt := randInt(pwLength)

		// must be unique