-
Notifications
You must be signed in to change notification settings - Fork 0
/
srt.go
196 lines (183 loc) · 3.98 KB
/
srt.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"unicode"
)
// grammar:
//
// Subs := {«Sub»}
// Sub := «Number» "\n" «Timespan» "\n" {«Line»} "\n"
// Timespan := «Time» "-->" «Time» /.*$/
// Time := «Digit»«Digit» ":" «Digit»«Digit» ":" «Digit»«Digit» ["," | "."] «Digit»«Digit»«Digit»
// Line := /^[^ ].*$/
// Digit := /[0-9]/
// Number := /[1-9][0-9]*/
//
// We make some allowances to ignore vtt-style stuff.
func ReadSRTFile(fname string) (Subs, error) {
s, err := ioutil.ReadFile(fname)
if err != nil {
return Subs{}, err
}
return ParseSRT(string(s))
}
func ParseSRT(s string) (Subs, error) {
subs, i, err := parseSubs(s, 0)
if err == nil && i != len(s) {
err = fmt.Errorf("trailing data")
}
if err != nil {
n := i + 100
if n > len(s) {
n = len(s)
}
return Subs{}, fmt.Errorf("error: %s (at character %d around '%s')", err, i+1, s[i:n])
}
return subs, nil
}
func parseSubs(s string, i int) (Subs, int, error) {
subs := Subs{}
for i < len(s) {
var sub Sub
var err error
i = skipSpace(s, i)
sub, i, err = parseSub(s, i)
if err != nil {
return Subs{}, i, err
}
subs.Sub = append(subs.Sub, &sub)
}
return subs, i, nil
}
func parseSub(s string, i int) (Sub, int, error) {
num, i, err := parseNumber(s, i)
if err != nil {
return Sub{}, i, err
}
i = skipSpace(s, i)
from, to, i, err := parseTimespan(s, i)
if err != nil {
return Sub{}, i, err
}
lines, i, err := parseLines(s, i)
if err != nil {
return Sub{}, i, err
}
return Sub{num, from, to, lines}, i, nil
}
func parseNumber(s string, i int) (int, int, error) {
i0 := i
for i < len(s) && s[i] >= '0' && s[i] <= '9' {
i++
}
if i0 == i {
return 0, i0, fmt.Errorf("unexpected character -- expected a digit")
}
num, err := strconv.Atoi(s[i0:i])
if err != nil {
return 0, i0, fmt.Errorf("programmer error: %s", err)
}
return num, i, nil
}
func parseTimespan(s string, i int) (time.Duration, time.Duration, int, error) {
from, i, err := parseTime(s, i)
if err != nil {
return 0, 0, i, err
}
i = skipSpace(s, i)
i, err = skipString(s, i, "-->")
if err != nil {
return 0, 0, i, err
}
i = skipSpace(s, i)
to, i, err := parseTime(s, i)
if err != nil {
return 0, 0, i, err
}
for i < len(s) && s[i-1] != '\n' {
i++
}
return from, to, i, nil
}
func parseTime(s string, i int) (time.Duration, int, error) {
hour, i, err := parseNumber(s, i)
if err != nil {
return 0, i, err
}
i, err = skipString(s, i, ":")
if err != nil {
return 0, i, err
}
min, i, err := parseNumber(s, i)
if err != nil {
return 0, i, err
}
i, err = skipString(s, i, ":")
if err != nil {
return 0, i, err
}
sec, i, err := parseNumber(s, i)
if err != nil {
return 0, i, err
}
i, err = skipStringChoice(s, i, ",", ".")
if err != nil {
return 0, i, err
}
ms, i, err := parseNumber(s, i)
if err != nil {
return 0, i, err
}
return time.Duration(hour)*time.Hour + time.Duration(min)*time.Minute + time.Duration(sec)*time.Second + time.Duration(ms)*time.Millisecond, i, nil
}
func parseLines(s string, i int) ([]string, int, error) {
var lines []string
for {
j := i
for j < len(s) && s[j] != '\n' {
j++
}
if j < len(s) && s[j] == '\n' {
j++
}
l := strings.TrimSpace(s[i:j])
if l == "" {
return lines, j, nil
}
lines = append(lines, l)
i = j
}
}
func skipSpace(s string, i int) int {
for j, r := range s[i:] {
if !unicode.IsSpace(r) {
return i + j
}
}
return len(s)
}
func skipString(s string, i int, t string) (int, error) {
for j := 0; j < len(t); j++ {
i_ := j + i
if i_ >= len(s) {
return i_, fmt.Errorf("unexpected end of string -- expected '%v'", t[j])
}
if s[i_] != t[j] {
return i_, fmt.Errorf("unexpected character '%v' -- expected '%v'", s[i_], t[j])
}
}
return i + len(t), nil
}
func skipStringChoice(s string, i int, tt ...string) (int, error) {
for _, t := range tt {
i_, err := skipString(s, i, t)
if err == nil {
return i_, nil
}
}
return i, fmt.Errorf("expected one of: %v", tt)
}