@@ -9,75 +9,52 @@ public class LyricInfo
9
9
{
10
10
public LyricInfo ( string lrcString )
11
11
{
12
- LrcLines = InitLines ( lrcString ) ;
12
+ LrcLines = LrcFormat ( lrcString ) ;
13
13
}
14
14
15
- public static string LrcFormat ( string oriLrc )
15
+ public static LrcLine [ ] LrcFormat ( string oriLrc )
16
16
{
17
- if ( string . IsNullOrWhiteSpace ( oriLrc ) )
18
- return string . Empty ;
19
- var lines = oriLrc . Split ( '\n ' ) . Where ( line => line . StartsWith ( "[" ) ) ;
20
- var offset = new TimeSpan ( ) ;
21
- var os = lines . FirstOrDefault ( o => Regex . IsMatch ( o , @"\[offset:[-\+]?\d+\]" ) ) ;
22
- if ( os != null )
23
- offset = TimeSpan . FromMilliseconds ( int . Parse ( Regex . Match ( os , @"-?\d+" ) . Value ) ) ;
24
- var list = new List < LrcLine > ( ) ;
25
- foreach ( var item in lines )
17
+ try
26
18
{
27
- var matches = Regex . Matches ( item , @"\[(\d{1,2}):(\d{1,2})([\.:](\d{1,3}))?\]" ) . Cast < Match > ( ) ;
28
- if ( matches . Count ( ) == 0 )
29
- continue ;
30
- var content = item . Substring ( matches . Sum ( m => m . Value . Length ) ) . Trim ( ) ;
31
- foreach ( Match m in matches )
19
+ if ( string . IsNullOrWhiteSpace ( oriLrc ) )
20
+ return null ;
21
+ var lines = oriLrc . Split ( '\n ' ) . Where ( line => line . StartsWith ( "[" ) ) ;
22
+ var offset = new TimeSpan ( ) ;
23
+ var os = lines . FirstOrDefault ( o => Regex . IsMatch ( o , @"\[offset:[-\+]?\d+\]" ) ) ;
24
+ if ( os != null )
25
+ offset = TimeSpan . FromMilliseconds ( int . Parse ( Regex . Match ( os , @"-?\d+" ) . Value ) ) ;
26
+ var list = new List < LrcLine > ( ) ;
27
+ foreach ( var item in lines )
32
28
{
33
- var s = $ "0:{ m . Groups [ 1 ] . Value } :{ m . Groups [ 2 ] . Value } ";
34
- if ( m . Groups [ 4 ] . Success )
35
- s = $ "{ s } .{ m . Groups [ 4 ] . Value } ";
36
- list . Add ( new LrcLine ( )
29
+ var matches = Regex . Matches ( item , @"\[(\d{1,2}):(\d{1,2})([\.:](\d{1,3}))?\]" ) . Cast < Match > ( ) ;
30
+ if ( matches . Count ( ) == 0 )
31
+ continue ;
32
+ var content = item . Substring ( matches . Sum ( m => m . Value . Length ) ) . Trim ( ) ;
33
+ foreach ( Match m in matches )
37
34
{
38
- Content = content ,
39
- TimePoint = TimeSpan . Parse ( s ) . Add ( offset ) . TotalSeconds ,
40
- } ) ;
35
+ var s = $ "0:{ m . Groups [ 1 ] . Value } :{ m . Groups [ 2 ] . Value } ";
36
+ if ( m . Groups [ 4 ] . Success )
37
+ s = $ "{ s } .{ m . Groups [ 4 ] . Value } ";
38
+ list . Add ( new LrcLine ( )
39
+ {
40
+ Content = content ,
41
+ TimePoint = TimeSpan . Parse ( s ) . Add ( offset ) . TotalSeconds ,
42
+ } ) ;
43
+ }
41
44
}
42
- }
43
- return string . Join ( "\n " , list . OrderBy ( lrc => lrc . TimePoint ) ) ;
44
- }
45
-
46
- public LrcLine [ ] LrcLines { get ; set ; }
47
-
48
- public string Lyric => string . Join ( "\n " , LrcLines . AsEnumerable ( ) ) ;
49
-
50
- private LrcLine [ ] InitLines ( string lrcString )
51
- {
52
- if ( string . IsNullOrWhiteSpace ( lrcString ) )
53
- return null ;
54
- try
55
- {
56
- return ParseLine ( lrcString ) ;
45
+ return list . OrderBy ( lrc => lrc . TimePoint ) . ToArray ( ) ;
57
46
}
58
47
catch
59
48
{
60
- lrcString = LrcFormat ( lrcString ) ;
61
- return ParseLine ( lrcString ) ;
49
+ return null ;
62
50
}
63
51
}
64
52
65
- private LrcLine [ ] ParseLine ( string str )
66
- {
67
- var lines = str . Split ( '\n ' ) ;
68
- var list = new List < LrcLine > ( ) ;
69
- foreach ( var item in lines )
70
- {
71
- list . Add ( new LrcLine ( )
72
- {
73
- Content = item . Substring ( 10 ) . Trim ( ) ,
74
- TimePoint = TimeSpan . Parse ( "00:" + item . Substring ( 1 , 8 ) ) . TotalSeconds ,
75
- } ) ;
76
- }
77
- return list . ToArray ( ) ;
78
- }
53
+ public LrcLine [ ] LrcLines { get ; set ; }
54
+
55
+ public string Lyric => string . Join ( "\n " , LrcLines . AsEnumerable ( ) ) ;
79
56
80
- public string Seek ( double sec ) => LrcLines . LastOrDefault ( ll => ll . TimePoint < sec ) ? . Content ;
57
+ public string Seek ( double sec ) => LrcLines ? . LastOrDefault ( ll => ll . TimePoint < sec ) ? . Content ;
81
58
}
82
59
83
60
public class LrcLine
0 commit comments