Skip to content

Instantly share code, notes, and snippets.

@polynomialspace
Last active Apr 1, 2021
Embed
What would you like to do?
something about asdot idk
package main
import (
"fmt"
"strconv"
"strings"
)
var testASN = []string{
"AS 6541",
"AS54233",
"65536",
}
var testASDot = []string{
"AS4.603", //AS262747
"AS 6.23", //AS393239
}
func main() {
for _, asd := range testASDot {
asplain, err := ASDotToASN(asd)
if err != nil {
panic(err)
}
asdot, err := ASPlainToASDot(asplain)
if err != nil {
panic(err)
}
fmt.Printf("%v => %v => %v\n", asd, asplain, asdot)
}
for _, v := range testASDot {
fmt.Println(IsASDot(v))
}
for _, v := range testASN {
fmt.Println(IsASDot(v))
}
}
func ASPlainToASDot(as string) (string, error) {
asplain, err := ASDigits(as)
if err != nil {
return "", err
}
integer := asplain / 65536
remainder := asplain - (integer * 65536)
asdot := fmt.Sprintf("%d.%d", integer, remainder)
return asdot, nil
}
func IsASDot(as string) bool {
var integer, remainder int
i, _ := fmt.Sscanf(strings.TrimPrefix(as, "AS"), "%d.%d", &integer, &remainder)
return i == 2
}
func ASDotToASN(asdot string) (string, error) {
asdot = strings.TrimPrefix(asdot, "AS")
var integer, remainder int
_, err := fmt.Sscanf(asdot, "%d.%d", &integer, &remainder)
if err != nil {
return "", err
}
asplain := integer * 65536
asplain += remainder
return fmt.Sprintf("%d", asplain), nil
}
func ASDigits(as string) (int, error) {
stripAS := strings.IndexAny(as, "0123456789")
asn, err := strconv.Atoi(as[stripAS:])
return asn, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment