-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (89 loc) · 2.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bufio"
"fmt"
"os"
"strings"
"go-reloaded/handlers"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("need three arguments: go run main.go input.txt output.txt")
os.Exit(1)
}
inputFile := os.Args[1]
outputFile := os.Args[2]
input, err := os.Open(inputFile)
if err != nil {
fmt.Println("Error opening input file:", err)
os.Exit(1)
}
defer input.Close()
output, err := os.Create(outputFile)
if err != nil {
fmt.Println("Error creating output file:", err)
os.Exit(1)
}
defer output.Close()
scanner := bufio.NewScanner(input)
writer := bufio.NewWriter(output)
defer writer.Flush()
for scanner.Scan() {
line := scanner.Text()
words := strings.Split(line, " ")
// logic addressing cases of (hex), (bin), (up), (low), (cap)
for i, word := range words {
switch word {
case "(hex)":
hexStr := words[i-1]
words[i-1] = handlers.ChangeHexToDecimal(hexStr)
words = append(words[:i], words[i+1:]...)
case "(bin)":
binStr := words[i-1]
words[i-1] = handlers.ChangeBinToDecimal(binStr)
words = append(words[:i], words[i+1:]...)
case "(up)":
words[i-1] = handlers.ConvertUpToUpper(words[i-1])
words = append(words[:i], words[i+1:]...)
case "(low)":
words[i-1] = handlers.ConvertLowToLower(words[i-1])
words = append(words[:i], words[i+1:]...)
case "(cap)":
words[i-1] = handlers.ConvertCapToCapitalized(words[i-1])
words = append(words[:i], words[i+1:]...)
}
}
// adressing instances of (up, <number>) et al. within the sample.txt file
for i, word := range words {
if word == "(up," {
num := handlers.ExtractNumber(words[i+1])
for j := 1; j <= num; j++ {
if i-j >= 0 {
words[i-j] = handlers.ConvertUpToUpper(words[i-j])
}
}
words = append(words[:i], words[i+2:]...)
} else if word == "(low," {
num := handlers.ExtractNumber(words[i+1])
for j := 1; j <= num; j++ {
if i-j >= 0 {
words[i-j] = handlers.ConvertLowToLower(words[i-j])
}
}
words = append(words[:i], words[i+2:]...)
} else if word == "(cap," {
num := handlers.ExtractNumber(words[i+1])
for j := 1; j <= num; j++ {
if i-j >= 0 {
words[i-j] = handlers.ConvertCapToCapitalized(words[i-j])
}
}
words = append(words[:i], words[i+2:]...)
}
}
handlers.AToAn(words)
words = handlers.MovePunctuation(words)
words = handlers.SplitStringWithPunctuation(words)
fmt.Fprintln(writer, strings.Join(handlers.PunctuationCorrecting(words), " "))
}
}