~dricottone/parcels

ref: df2d62120dae0cceae1cc0fa59e9d26cd445e608 parcels/main.go -rw-r--r-- 2.2 KiB
df2d6212Dominic Ricottone Makefile updates 2 years ago
                                                                                
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main

import (
	"fmt"
	"os"
	"io"
	"bufio"
	"regexp"
	"flag"
)

func Replacement(i int) string {
	return fmt.Sprintf("[%d]", i)
}

func find_in_stream(reader io.Reader, target int) {
	// Create scanner from reader
	input := bufio.NewScanner(reader)

	// Initialize state
	re := regexp.MustCompile(UrlPattern)
	count := 0

	// Parse and print
	for input.Scan() {
		line := input.Text()
		line_indices := re.FindAllStringIndex(line, -1)
		count_after := count + len(line_indices)

		if target < count_after {
			beg, end := line_indices[target-count][0], line_indices[target-count][1]
			fmt.Println(line[beg:end])
			break
		}

		count = count_after
	}

	// Check for scanner errors
	if err := input.Err(); err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
}

func parse_stream(reader io.Reader) {
	// Create scanner from reader
	input := bufio.NewScanner(reader)

	// Initialize state
	re := regexp.MustCompile(UrlPattern)
	var parcels [999]string //assuming that never need 1000+ URLs
	offset := 0

	// Parse, modify, and print
	for input.Scan() {
		line := input.Text()

		line_indices := re.FindAllStringIndex(line, -1)
		for i := len(line_indices)-1; i >= 0; i-- {
			beg, end := line_indices[i][0], line_indices[i][1]
			parcels[offset+i] = line[beg:end]
			line = line[:beg] + Replacement(offset+i) + line[end:]
		}

		fmt.Println(line)

		offset = offset + len(line_indices)
	}

	// Print postscript
	fmt.Printf("\nURLs:\n")
	for index, url := range parcels {
		if url == "" {
			break
		}
		fmt.Printf("[%d] %s\n", index, url)
	}

	// Check for scanner errors
	if err := input.Err(); err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
}

func parse_file(filename string) {
	// Check file
	file, err := os.Open(filename)
	if err != nil {
		fmt.Printf("cannot read file '%s'\n", filename)
		os.Exit(1)
	}
	defer file.Close()

	// Parse
	parse_stream(file)
}

func main() {
	// Check STDIN
	_, err := os.Stdin.Stat()
	if err != nil {
		fmt.Println("cannot read input")
		os.Exit(1)
	}

	// Look for arguments
	var index = flag.Int("n", -1, "print Nth URL")
	flag.Parse()

	// Parse
	if *index < 0 {
		parse_stream(os.Stdin)
	} else {
		find_in_stream(os.Stdin, *index)
	}
}