~seirdy/moac

1452c46f8b2a74de94e3e302bc9ca6d0cf081668 — Rohan Kumar 2 years ago 87a6ee8
Chore(style): remove unnecessary "else" statements

Remove unnecessary "else" statements, replacing them with early exits if
appropriate. This keeps  the code simpler and flatter, and also neatly
separates a "normal" flow from a "special-case" flow.
4 files changed, 12 insertions(+), 9 deletions(-)

M cmd/moac-pwgen/main.go
M givens.go
M pwgen/genpw_test.go
M pwgen/pwgen.go
M cmd/moac-pwgen/main.go => cmd/moac-pwgen/main.go +2 -2
@@ 164,11 164,11 @@ func main1() int {
func setCharsetNames(args []string) (charsetNames []string) {
	var badCharsets []string

	charsetNames = []string{"ascii"}

	if len(args) > 0 {
		charsetNames, badCharsets = sanitize.FilterStrings(args)
		warnOnBadCharacters(badCharsets)
	} else {
		charsetNames = []string{"ascii"}
	}

	return charsetNames

M givens.go => givens.go +3 -4
@@ 222,12 222,11 @@ func (givens *Givens) MinEntropy() (entropyNeeded float64, err error) {

	if givens.Time > 0 {
		timeBound := math.Log2(givens.Time * givens.GuessesPerSecond)
		entropyNeeded = math.Min(energyBound, timeBound)
	} else {
		entropyNeeded = energyBound

		return math.Min(energyBound, timeBound), nil
	}

	return entropyNeeded, nil
	return energyBound, nil
}

// MinEntropyQuantum is equivalent to MinEntropy, but accounts for

M pwgen/genpw_test.go => pwgen/genpw_test.go +3 -1
@@ 289,7 289,9 @@ func pwOnlyUsesCharsets(cs charsets.CharsetCollection, password []rune) (rune, b
		for i, allowedChar := range allowedRunes {
			if pwChar == allowedChar {
				break
			} else if i == len(allowedRunes)-1 {
			}

			if i == len(allowedRunes)-1 {
				return pwChar, false
			}
		}

M pwgen/pwgen.go => pwgen/pwgen.go +4 -2
@@ 146,9 146,11 @@ func buildFixedLengthPw(
		if i := indexOf(specialIndexes, currentLength); i >= 0 {
			addRuneToEnd(pwBuilder, cs[i].Runes()) // one of each charset @ a special index
			specialI++
		} else {
			addRuneToEnd(pwBuilder, combinedCharset)

			continue
		}

		addRuneToEnd(pwBuilder, combinedCharset)
	}

	return []rune(pwBuilder.String())