Fixed issue with specifying -l without -g causing it to hang

master
Aaron Johnon 1 year ago
parent d98cdf5303
commit 6e61330e31

@ -38,12 +38,14 @@ func formatOutput(encoded []int, groupSize int, lineSize int) string {
output.WriteString(fmt.Sprintf("%02d", num)) output.WriteString(fmt.Sprintf("%02d", num))
} }
// Apply grouping
finalOutput := output.String() finalOutput := output.String()
var groupedOutput strings.Builder var groupedOutput strings.Builder
counter := 0 counter := 0
groupCounter := 0 groupCounter := 0
// Check if a group size is provided
if groupSize > 0 {
// Apply grouping and line size limits
for i := 0; i < len(finalOutput); i += groupSize { for i := 0; i < len(finalOutput); i += groupSize {
if i+groupSize < len(finalOutput) { if i+groupSize < len(finalOutput) {
groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ") groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ")
@ -59,6 +61,19 @@ func formatOutput(encoded []int, groupSize int, lineSize int) string {
groupCounter = 0 groupCounter = 0
} }
} }
} else {
// No group size specified, handle line size by number of characters
for i := 0; i < len(finalOutput); i++ {
groupedOutput.WriteByte(finalOutput[i])
counter++
// Check if we reached the line size
if lineSize > 0 && counter >= lineSize && i+1 < len(finalOutput) {
groupedOutput.WriteString("\n")
counter = 0
}
}
}
return strings.TrimSpace(groupedOutput.String()) return strings.TrimSpace(groupedOutput.String())
} }
@ -67,7 +82,7 @@ func main() {
// Define command-line flags // Define command-line flags
shift := flag.Int("s", 0, "Shift amount for the cipher") shift := flag.Int("s", 0, "Shift amount for the cipher")
groupSize := flag.Int("g", 0, "Group size for output formatting") groupSize := flag.Int("g", 0, "Group size for output formatting")
lineSize := flag.Int("l", 0, "Line size by number of groups") lineSize := flag.Int("l", 0, "Line size by number of groups or characters if no group size is specified")
flag.Parse() flag.Parse()
// Read input from stdin // Read input from stdin

Loading…
Cancel
Save