Skip to content

Commit d0f664b

Browse files
committed
Fix crash
1 parent 5836831 commit d0f664b

File tree

3 files changed

+64
-59
lines changed

3 files changed

+64
-59
lines changed

Sources/LyricsService/Parser/KugouKrcParser.swift

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ import Foundation
1111
import LyricsCore
1212

1313
extension Lyrics {
14-
1514
convenience init?(kugouKrcContent content: String) {
1615
var idTags: [IDTagKey: String] = [:]
1716
var languageHeader: KugouKrcHeaderFieldLanguage?
18-
id3TagRegex.matches(in: content).forEach { match in
17+
for match in id3TagRegex.matches(in: content) {
1918
guard let key = match[1]?.content.trimmingCharacters(in: .whitespaces),
20-
let value = match[2]?.content.trimmingCharacters(in: .whitespaces),
21-
!key.isEmpty,
22-
!value.isEmpty else {
23-
return
19+
let value = match[2]?.content.trimmingCharacters(in: .whitespaces),
20+
!key.isEmpty,
21+
!value.isEmpty else {
22+
continue
2423
}
2524
if key == "language" {
2625
if let data = Data(base64Encoded: value) {
@@ -31,17 +30,17 @@ extension Lyrics {
3130
idTags[.init(key)] = value
3231
}
3332
}
34-
33+
3534
var lines: [LyricsLine] = krcLineRegex.matches(in: content).map { match in
3635
let timeTagStr = match[1]!.content
3736
let timeTag = TimeInterval(timeTagStr)! / 1000
38-
37+
3938
let durationStr = match[2]!.content
4039
let duration = TimeInterval(durationStr)! / 1000
41-
40+
4241
var lineContent = ""
4342
var attachment = LyricsLine.Attachments.InlineTimeTag(tags: [.init(index: 0, time: 0)], duration: duration)
44-
kugouInlineTagRegex.matches(in: content, range: match[3]!.range).forEach { m in
43+
for m in kugouInlineTagRegex.matches(in: content, range: match[3]!.range) {
4544
let t1 = Int(m[1]!.content)!
4645
let t2 = Int(m[2]!.content)!
4746
let t = TimeInterval(t1 + t2) / 1000
@@ -52,15 +51,15 @@ extension Lyrics {
5251
attachment.tags.append(.init(index: lineContent.count, time: t))
5352
}
5453
}
55-
54+
5655
let att = LyricsLine.Attachments(attachments: [.timetag: attachment])
5756
return LyricsLine(content: lineContent, position: timeTag, attachments: att)
5857
}
5958
guard !lines.isEmpty else {
6059
return nil
6160
}
6261
self.init(lines: lines, idTags: idTags)
63-
62+
6463
// TODO: multiple translation
6564
if let transContent = languageHeader?.content.first?.lyricContent {
6665
transContent.prefix(lines.count).enumerated().forEach { index, item in
@@ -72,49 +71,3 @@ extension Lyrics {
7271
}
7372
}
7473
}
75-
76-
77-
extension Lyrics {
78-
79-
convenience init?(qqmusicQrcContent content: String) {
80-
var idTags: [IDTagKey: String] = [:]
81-
id3TagRegex.matches(in: content).forEach { match in
82-
guard let key = match[1]?.content.trimmingCharacters(in: .whitespaces),
83-
let value = match[2]?.content.trimmingCharacters(in: .whitespaces),
84-
!key.isEmpty,
85-
!value.isEmpty else {
86-
return
87-
}
88-
idTags[.init(key)] = value
89-
}
90-
91-
let lines: [LyricsLine] = qrcLineRegex.matches(in: content).map { match in
92-
let timeTagStr = match[1]!.content
93-
let timeTag = TimeInterval(timeTagStr)! / 1000
94-
95-
let durationStr = match[2]!.content
96-
let duration = TimeInterval(durationStr)! / 1000
97-
98-
var lineContent = ""
99-
var attachment = LyricsLine.Attachments.InlineTimeTag(tags: [.init(index: 0, time: 0)], duration: duration)
100-
qqmusicInlineTagRegex.matches(in: content, range: match[3]!.range).forEach { m in
101-
let t1 = Int(m[2]!.content)! - Int(timeTagStr)!
102-
let t2 = Int(m[3]!.content)!
103-
let t = TimeInterval(t1 + t2) / 1000
104-
let fragment = m[1]!.content
105-
let prevCount = lineContent.count
106-
lineContent += fragment
107-
if lineContent.count > prevCount {
108-
attachment.tags.append(.init(index: lineContent.count, time: t))
109-
}
110-
}
111-
112-
let att = LyricsLine.Attachments(attachments: [.timetag: attachment])
113-
return LyricsLine(content: lineContent, position: timeTag, attachments: att)
114-
}
115-
guard !lines.isEmpty else {
116-
return nil
117-
}
118-
self.init(lines: lines, idTags: idTags)
119-
}
120-
}

Sources/LyricsService/Parser/QQMusicQrcDecrypter.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QrcDecoder {
3434

3535
enum DecodeError: Error {
3636
case convertStringError
37+
case dataEmpty
3738
}
3839

3940
static func decode(_ hex: String) throws -> String {
@@ -44,8 +45,13 @@ class QrcDecoder {
4445
try des(&data, KEY2, dataLen)
4546
try ddes(&data, KEY3, dataLen)
4647
var byteData = Data(data)
48+
49+
guard !byteData.isEmpty else {
50+
throw DecodeError.dataEmpty
51+
}
52+
4753
byteData.removeFirst(2)
48-
54+
4955
let decompressedData = try (byteData as NSData).decompressed(using: .zlib)
5056
guard let result = String(data: decompressedData as Data, encoding: .utf8)
5157
else {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Foundation
2+
import LyricsCore
3+
4+
extension Lyrics {
5+
convenience init?(qqmusicQrcContent content: String) {
6+
var idTags: [IDTagKey: String] = [:]
7+
for match in id3TagRegex.matches(in: content) {
8+
guard let key = match[1]?.content.trimmingCharacters(in: .whitespaces),
9+
let value = match[2]?.content.trimmingCharacters(in: .whitespaces),
10+
!key.isEmpty,
11+
!value.isEmpty else {
12+
continue
13+
}
14+
idTags[.init(key)] = value
15+
}
16+
17+
let lines: [LyricsLine] = qrcLineRegex.matches(in: content).map { match in
18+
let timeTagStr = match[1]!.content
19+
let timeTag = TimeInterval(timeTagStr)! / 1000
20+
21+
let durationStr = match[2]!.content
22+
let duration = TimeInterval(durationStr)! / 1000
23+
24+
var lineContent = ""
25+
var attachment = LyricsLine.Attachments.InlineTimeTag(tags: [.init(index: 0, time: 0)], duration: duration)
26+
for m in qqmusicInlineTagRegex.matches(in: content, range: match[3]!.range) {
27+
let t1 = Int(m[2]!.content)! - Int(timeTagStr)!
28+
let t2 = Int(m[3]!.content)!
29+
let t = TimeInterval(t1 + t2) / 1000
30+
let fragment = m[1]!.content
31+
let prevCount = lineContent.count
32+
lineContent += fragment
33+
if lineContent.count > prevCount {
34+
attachment.tags.append(.init(index: lineContent.count, time: t))
35+
}
36+
}
37+
38+
let att = LyricsLine.Attachments(attachments: [.timetag: attachment])
39+
return LyricsLine(content: lineContent, position: timeTag, attachments: att)
40+
}
41+
guard !lines.isEmpty else {
42+
return nil
43+
}
44+
self.init(lines: lines, idTags: idTags)
45+
}
46+
}

0 commit comments

Comments
 (0)