修复srt序号错误以及模式3无译文的问题

This commit is contained in:
Hami Lemon 2022-03-28 20:37:37 +08:00
parent 8b57abe8ee
commit c381852b49
3 changed files with 32 additions and 7 deletions

View file

@ -230,11 +230,24 @@ func (l *LinkedList[E]) PullFront() *E {
// Iterator 获取该链表的迭代器
func (l *LinkedList[E]) Iterator() Iterator[E] {
return &LinkedListIterator[E]{next: l.First}
return &LinkedListIterator[E]{
reverse: false,
next: l.First,
}
}
// ReverseIterator 获取反向迭代器
func (l *LinkedList[E]) ReverseIterator() Iterator[E] {
return &LinkedListIterator[E]{
reverse: true,
next: l.Last,
}
}
type LinkedListIterator[E any] struct {
next *Node[E]
//是否反向,如果为true,则是从尾部向头部迭代
reverse bool
next *Node[E]
}
func (l *LinkedListIterator[E]) Has() bool {
@ -246,6 +259,10 @@ func (l *LinkedListIterator[E]) Next() E {
if e == nil {
panic("iterator is empty.")
}
l.next = e.Next
if l.reverse {
l.next = e.Prev
} else {
l.next = e.Next
}
return e.Element
}

View file

@ -20,6 +20,8 @@ type List[E any] interface {
Set(index uint, element E) bool
// Iterator 获取列表的迭代器
Iterator() Iterator[E]
// ReverseIterator 反向迭代器,从尾部向前迭代
ReverseIterator() Iterator[E]
}
// Queue 队列

14
srt.go
View file

@ -19,7 +19,7 @@ const (
)
type SRTContent struct {
//序号从1开始
//序号从1开始,只在写入文件的时候设置这个属性
Index int
//开始时间,单位毫秒
Start int
@ -73,7 +73,7 @@ func LrcToSrt(lrc *LRC) *SRT {
for it := lrc.LrcList.Iterator(); it.Has(); {
lrcNode := it.Next()
srtContent := &SRTContent{
Index: index,
Index: 0,
Start: lrcNode.time,
Text: lrcNode.content,
}
@ -159,7 +159,9 @@ func (s *SRT) mergeUp(other *SRT) {
func (s *SRT) mergeBottom(other *SRT) {
oq := other.Content
if oq.IsNotEmpty() {
s.Content.PushFront(*(oq.PullBack()))
for it := other.Content.ReverseIterator(); it.Has(); {
s.Content.PushFront(it.Next())
}
}
}
@ -179,8 +181,12 @@ func (s *SRT) Write(dst io.Writer) error {
//6KB的缓冲
bufSize := 1024 * 6
writer := bufio.NewWriterSize(dst, bufSize)
index := 1
for it := s.Content.Iterator(); it.Has(); {
_, err := writer.WriteString(it.Next().String())
content := it.Next()
content.Index = index
index++
_, err := writer.WriteString(content.String())
if err != nil {
return err
}