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

master
Aaron Johnon 1 year ago
parent d98cdf5303
commit 6e61330e31

@ -38,25 +38,40 @@ 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
for i := 0; i < len(finalOutput); i += groupSize { // Check if a group size is provided
if i+groupSize < len(finalOutput) { if groupSize > 0 {
groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ") // Apply grouping and line size limits
} else { for i := 0; i < len(finalOutput); i += groupSize {
groupedOutput.WriteString(finalOutput[i:]) if i+groupSize < len(finalOutput) {
} groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ")
counter++ } else {
groupCounter++ groupedOutput.WriteString(finalOutput[i:])
}
counter++
groupCounter++
// Check if we reached the line size // Check if we reached the line size
if lineSize > 0 && groupCounter >= lineSize && i+groupSize < len(finalOutput) { if lineSize > 0 && groupCounter >= lineSize && i+groupSize < len(finalOutput) {
groupedOutput.WriteString("\n") groupedOutput.WriteString("\n")
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
}
} }
} }
@ -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