-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
ipextract.go
52 lines (45 loc) · 1.38 KB
/
ipextract.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
package ipextract
import (
"net/netip"
"strings"
)
// IPv4 extracts all valid IPv4 addresses from a given
// text string. Each IPv4 address must be separated by a character
// not part of the IPv4 alphabet (0123456789.).
// Performance-wise, this extraction is at least x3 times faster
// than using a regular expression.
func IPv4(text string) (addresses []netip.Addr) {
const ipv4Alphabet = "0123456789."
return extract(text, ipv4Alphabet)
}
// IPv6 extracts all valid IPv6 addresses from a given
// text string. Each IPv6 address must be separated by a character
// not part of the IPv6 alphabet (0123456789abcdefABCDEF:).
// Performance-wise, this extraction is at least x3 times faster
// than using a regular expression.
func IPv6(text string) (addresses []netip.Addr) {
const ipv6Alphabet = "0123456789abcdefABCDEF:"
return extract(text, ipv6Alphabet)
}
func extract(text string, alphabet string) (addresses []netip.Addr) {
var start, end int
for {
for i := start; i < len(text); i++ {
r := rune(text[i])
if !strings.ContainsRune(alphabet, r) {
break
}
end++
}
possibleIPString := text[start:end]
ipAddress, err := netip.ParseAddr(possibleIPString)
if err == nil { // Valid IP address found
addresses = append(addresses, ipAddress)
}
if end == len(text) {
return addresses
}
start = end + 1 // + 1 to skip non alphabet match character
end = start
}
}