aboutsummaryrefslogtreecommitdiff
path: root/pkg/model/card.go
blob: 5c9de76639c2d7e098dda4d7bae772d92847b44e (plain)
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
package model

import (
	"regexp"
	"strconv"
	"strings"
)

func ParseCardLine(s string) (prefix string, number uint64, bonus string) {
	raw := strings.TrimSpace(s)
	if raw == "" {
		return "", 0, ""
	}
	if m := regexp.MustCompile(`(\d{3,})\D*$`).FindStringSubmatch(raw); len(m) == 2 {
		if n, err := strconv.ParseUint(m[1], 10, 64); err == nil {
			number = n
		}
	}

	tokRe := regexp.MustCompile(`[A-Za-z][A-Za-z'/-]*`)
	toks := tokRe.FindAllString(s, -1)

	for _, t := range toks {
		u := strings.ToUpper(t)
		if len(u) >= 2 && len(u) <= 3 && regexp.MustCompile(`^[A-Z]{2,3}$`).MatchString(u) {
			prefix = u
			break
		}
	}

	words := []string{}
	for _, t := range toks {
		if strings.ToUpper(t) == prefix {
			continue
		}
		words = append(words, t)
	}
	if len(words) > 0 {
		bonus = strings.Join(words, " ")
	}
	if bonus == "" && prefix != "" {
		bonus = prefix
	}
	return
}


func FirstNonEmpty(a, b string) string {
	if strings.TrimSpace(a) != "" {
		return a
	}
	return b
}