...
1 package conversion
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 "strings"
8 "unicode"
9 )
10
11 var MapNumberOverflowError = errors.New("Map number can't be bigger than 1035")
12 var MapIdIncorrectFormat = errors.New("Map id is incorrectly formatted")
13
14
15 func MapIdToNumber(id string) (uint, error) {
16 l := len(id)
17 switch {
18 case l == 1:
19 id = fmt.Sprintf("%s%s", "0", id)
20 case l > 2:
21 return 0, MapIdIncorrectFormat
22 }
23 var x, y byte
24 var p, q int
25
26 id = strings.ToUpper(id)
27 x = id[0]
28 y = id[1]
29 if unicode.IsDigit(rune(x)) {
30 if !unicode.IsDigit(rune(y)) {
31 return 0, MapIdIncorrectFormat
32 }
33 n, err := strconv.Atoi(id)
34 return uint(n), err
35 }
36
37 p = int(x - 'A')
38 if unicode.IsDigit(rune(y)) {
39 q = int(y - '0')
40 } else {
41 q = int(y-'A') + 10
42 }
43 return uint(100 + (36*p + q)), nil
44 }
45
46 func NumberToMapId(n uint) (string, error) {
47 if n < 100 {
48 return fmt.Sprintf("%02d", n), nil
49 }
50 if n > 1035 {
51 return "", MapNumberOverflowError
52 }
53 var x, p, q int
54 var a, b int
55 x = int(n - 100)
56 p = x / 36
57 q = x - 36*p
58 a = 'A' + p
59 if q > 10 {
60 b = 'A' + q - 10
61 } else {
62 b = '0' + q
63 }
64
65 return fmt.Sprintf("%c%c", a, b), nil
66 }
67
View as plain text