-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
57 lines (44 loc) · 905 Bytes
/
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
package main
import (
"bufio"
"io"
"strconv"
"strings"
)
const defaultBpm = 128
const defaultSongName = "Four-on-the-floor"
const maxSongTitle = 30
const maxBpm = 128
const minBpm = 30
func parseTitle(r io.Reader) (string, error) {
var title string
reader := bufio.NewReader(r)
title, _ = reader.ReadString('\n')
title = strings.TrimSpace(title)
if len(title) > maxSongTitle {
return "", songTitleTooLong
}
if title == "" {
title = defaultSongName
}
return title, nil
}
func parseTempo(r io.Reader) (int, error) {
var bpm int
var err error
var bpmStr string
reader := bufio.NewReader(r)
bpmStr, _ = reader.ReadString('\n')
bpmStr = strings.TrimSpace(bpmStr)
if len(bpmStr) == 0 {
return defaultBpm, nil
}
bpm, err = strconv.Atoi(bpmStr)
if err != nil {
return 0, tempoNotNumber
}
if bpm < minBpm || bpm > maxBpm {
return 0, tempoRange
}
return bpm, nil
}