-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.go
75 lines (62 loc) · 1.85 KB
/
parsers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package parsers
import (
"errors"
"regexp"
)
// Parser is the contract that the main command line application will use
type Parser interface {
CanParseIntoMachine(string) (bool, error)
CanParseFromMachine(string) (bool, error)
DoIntoMachine(string) (string, error)
DoFromMachine(string) (string, error)
}
// General purpose errors
var ErrNotANumber error = errors.New("Not a Number")
var ErrTooLarge error = errors.New("Too Beaucoup")
var ErrTooSmall error = errors.New("Number too small")
var ErrNotYetImplemented error = errors.New("Not Yet Implemented")
// Catch all error
var ErrUnparsable error = errors.New("Unparsable")
// Empty can be used to as a placeholder for when an
// interface is needed
func NewEmpty() *Empty {
return &Empty{}
}
type Empty struct{}
func (e *Empty) CanParseIntoMachine(string) (bool, error) {
return true, nil
}
func (e *Empty) CanParseFromMachine(string) (bool, error) {
return true, nil
}
func (e *Empty) DoIntoMachine(string) (string, error) {
return "Not Yet Implemented", nil
}
func (e *Empty) DoFromMachine(string) (string, error) {
return "Not Yet Implemented", nil
}
// IsMachineNumber validates that a number:
// Contains only digits
// The first digit must be 1-9 when >= 10
func isMachineNumber(s string) bool {
match, _ := regexp.MatchString(`^[0-9]$|^[1-9][0-9]+$`, s)
return match
}
// IsDelimitednumber validates that a number:
// Contains only digits and delimiters [., _]
// The first digit must be 1-9 when >= 10
// The first group can be 1-3 digits
// Subsequent groups must be 3 digits
// A number less than 1000 is considered a valid delimited number
func isDelimitedNumber(s string) bool {
// 0-999 are machine numbers
if len(s) < 4 {
if ok := isMachineNumber(s); ok {
return true
} else {
return false
}
}
match, _ := regexp.MatchString(`^[1-9][0-9]{0,2}([.,_ ][0-9]{3})+$`, s)
return match
}