]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: Be more cautious when merging partial ranges
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 17:56:13 +0000 (14:56 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 17:56:13 +0000 (14:56 -0300)
Change the touch() method to not accept touches where the left side is a
partial episode with unknown number of parts. If the number of parts is
known, then only accept if it's the last part.

misc/episoderange.go
misc/episoderange_test.go

index 729d2c8ea2965366d22a691bdf9cebf306115c01..8f679b10440b76df8bccedc8f89816fb1d84b561 100644 (file)
@@ -114,15 +114,21 @@ func (a *EpisodeRange) Merge(b *EpisodeRange) (c *EpisodeRange) {
        if a.touches(b) {
                c = &EpisodeRange{Type: a.Type}
 
-               if a.Start.Number == b.Start.Number {
-                       if a.Start.Part <= b.Start.Part {
+               switch {
+               case a.Start.Number == b.Start.Number:
+                       switch {
+                       case a.Start.Part < 0:
+                               c.Start = a.Start
+                       case b.Start.Part < 0:
+                               c.Start = b.Start
+                       case a.Start.Part <= b.Start.Part:
                                c.Start = a.Start
-                       } else {
+                       default:
                                c.Start = b.Start
                        }
-               } else if a.Start.Number < b.Start.Number {
+               case a.Start.Number < b.Start.Number:
                        c.Start = a.Start
-               } else {
+               default:
                        c.Start = b.Start
                }
 
@@ -130,9 +136,14 @@ func (a *EpisodeRange) Merge(b *EpisodeRange) (c *EpisodeRange) {
                case a.End == nil || b.End == nil:
                        c.End = nil
                case a.End.Number == b.End.Number:
-                       if a.End.Part >= b.End.Part {
+                       switch {
+                       case a.End.Part < 0:
+                               c.End = a.End
+                       case b.End.Part < 0:
+                               c.End = b.End
+                       case a.End.Part >= b.End.Part:
                                c.End = a.End
-                       } else {
+                       default:
                                c.End = b.End
                        }
                case a.End.Number > b.End.Number:
@@ -291,9 +302,8 @@ func (a *EpisodeRange) Equals(b *EpisodeRange) bool {
        return false
 }
 
-// CORNER CASE: e.g. 1.3,2.0 (or 1.3,2) always touch,
-// even if there's an unlisted 1.4 between them; unless
-// the part count is known.
+// CORNER CASE: e.g. 1.3,2.0 (or 1.3,2) never touch,
+// unless it's known that 1.3 is the last part.
 func (a *EpisodeRange) touches(b *EpisodeRange) bool {
        if a == nil || b == nil || a.Type != b.Type {
                return false
@@ -388,8 +398,8 @@ func (a *EpisodeRange) touches(b *EpisodeRange) bool {
                        }
                case a.End.Number == b.Start.Number-1 && b.Start.Part <= 0:
                        switch {
-                       case b.End.Part == -1, b.End.Parts == 0,
-                               b.End.Part == b.End.Parts:
+                       case a.End.Part == -1, a.End.Part == a.End.Parts-1,
+                               a.End.Part == b.Start.Parts:
                                // log.Printf("[ %s ]{ %s }", a.End, b.Start)
                                return true
                        }
@@ -409,7 +419,7 @@ func (a *EpisodeRange) touches(b *EpisodeRange) bool {
                        }
                case b.End.Number == a.Start.Number-1 && a.Start.Part <= 0:
                        switch {
-                       case b.End.Part == -1, b.End.Parts == 0,
+                       case b.End.Part == -1, b.End.Part == b.End.Parts-1,
                                b.End.Part == b.End.Parts:
                                // log.Printf("{ %s }[ %s ]", b.End, a.Start)
                                return true
index 2402cdd5462940c11b9c60e387faab77bdb0182f..aec5636f547edcf386b75fc3a43d9d9e4a3abce0 100644 (file)
@@ -45,7 +45,7 @@ func ExampleEpisodeRange_PartialMerge() {
        fmt.Println(a.Merge(b)) // 2.0-2.3 + 1
 
        // Output:
-       // 2.1-3.0
+       // <nil>
        // <nil>
        // <nil>
        // 1-2.3