misc

package
v0.0.0-...-57d0119 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2014 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Definitions and utilities used by the other go-anidb modules, or that don't fit within any of the other modules.

Index

Examples

Constants

View Source
const (
	EpisodeTypeRegular = EpisodeType(1 + iota)
	EpisodeTypeSpecial // "S" episode
	EpisodeTypeCredits // "C" episode
	EpisodeTypeTrailer // "T" episode
	EpisodeTypeParody  // "P" episode
	EpisodeTypeOther   // "O" episode
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Episode

type Episode struct {
	Type   EpisodeType
	Number int
	Part   int
	Parts  int
}

An episode (duh).

func ParseEpisode

func ParseEpisode(s string) *Episode

Parses a string in the usual AniDB API episode format and converts into an Episode.

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	fmt.Printf("%#v\n", misc.ParseEpisode("1"))
	fmt.Printf("%#v\n", misc.ParseEpisode("S2"))
	fmt.Printf("%#v\n", misc.ParseEpisode("03"))
	fmt.Printf("%#v\n", misc.ParseEpisode("")) // invalid episode

}
Output:

&misc.Episode{Type:1, Number:1, Part:-1, Parts:0}
&misc.Episode{Type:2, Number:2, Part:-1, Parts:0}
&misc.Episode{Type:1, Number:3, Part:-1, Parts:0}
(*misc.Episode)(nil)

func (*Episode) ContainsEpisodes

func (ep *Episode) ContainsEpisodes(ec EpisodeContainer) bool

Returns true if ec is an Episode and is identical to this episode, or if ec is a single episode EpisodeRange / EpisodeList that contain only this episode.

func (*Episode) DecNumber

func (ep *Episode) DecNumber()

func (*Episode) DecPart

func (ep *Episode) DecPart()

func (*Episode) Episodes

func (ep *Episode) Episodes() chan Episode

func (Episode) Format

func (ep Episode) Format(width int) string

func (*Episode) FormatLog

func (ep *Episode) FormatLog(max int) string

func (*Episode) IncNumber

func (ep *Episode) IncNumber()

func (*Episode) IncPart

func (ep *Episode) IncPart()

func (Episode) String

func (ep Episode) String() string

Converts the Episode into AniDB API episode format.

type EpisodeContainer

type EpisodeContainer interface {
	// Returns true if this EpisodeContainer is equivalent or a superset of the given EpisodeContainer
	ContainsEpisodes(EpisodeContainer) bool
	// Returns a channel meant for iterating with for/range.
	// Sends all contained episodes in order.
	Episodes() chan Episode
}

type EpisodeCount

type EpisodeCount struct {
	RegularCount int
	SpecialCount int
	CreditsCount int
	OtherCount   int
	TrailerCount int
	ParodyCount  int
}

type EpisodeList

type EpisodeList []*EpisodeRange

func ContainerToList

func ContainerToList(ec EpisodeContainer) EpisodeList

func EpisodeToList

func EpisodeToList(ep *Episode) EpisodeList

func ParseEpisodeList

func ParseEpisodeList(s string) (el EpisodeList)

Parses a string in the AniDB API list format and converts into an EpisodeList.

ParseEpisodeList("01")       <=> EpisodeList{ParseEpisodeRange("01")}
ParseEpisodeList("S2-S3")    <=> EpisodeList{ParseEpisodeRange("S2-S3")}
ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}

func RangesToList

func RangesToList(ranges ...*EpisodeRange) EpisodeList

func (*EpisodeList) Add

func (el *EpisodeList) Add(ec EpisodeContainer)
Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	a := misc.ParseEpisodeList("1-3")
	a.Add(misc.ParseEpisode("3.1"))
	fmt.Println(a)

	a.Add(misc.ParseEpisode("4.0"))
	fmt.Println(a)

	a.Add(misc.ParseEpisode("4"))
	fmt.Println(a)

	a.Add(misc.ParseEpisode("5.1"))
	fmt.Println(a)

	a.Add(misc.ParseEpisode("6"))
	fmt.Println(a)

}
Output:

1-3
1-4.0
1-4
1-4,5.1
1-4,5.1,6

func (EpisodeList) ContainsEpisodes

func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool

Returns true if any of the contained EpisodeRanges contain the given EpisodeContainer.

func (EpisodeList) CountEpisodes

func (el EpisodeList) CountEpisodes() (ec EpisodeCount)

func (EpisodeList) Episodes

func (el EpisodeList) Episodes() chan Episode

Returns a channel that can be used to iterate using for/range.

If the EpisodeList is infinite, then the channel is also infinite. The caller is allowed to close the channel in such case.

NOTE: Not thread safe.

func (EpisodeList) FormatLog

func (el EpisodeList) FormatLog(ec EpisodeCount) string

Formats the list according to the number of digits of the count for its type, given in the EpisodeCount.

func (EpisodeList) Infinite

func (el EpisodeList) Infinite() bool

func (EpisodeList) Len

func (el EpisodeList) Len() int

func (EpisodeList) Less

func (el EpisodeList) Less(i, j int) bool

func (EpisodeList) MarshalJSON

func (el EpisodeList) MarshalJSON() ([]byte, error)

Equivalent to marshaling el.String()

func (EpisodeList) Simplify

func (el EpisodeList) Simplify() EpisodeList

Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	a := misc.ParseEpisodeList("1,2,3,5,10-14,13-15,,S3-S6,C7-C10,S1,S7,S8-")
	fmt.Println(a.Simplify())

}
Output:

01-03,05,10-15,S1,S3-,C07-C10

func (EpisodeList) String

func (el EpisodeList) String() string

Converts the EpisodeList into the AniDB API list format.

func (*EpisodeList) Sub

func (el *EpisodeList) Sub(ec EpisodeContainer)

func (EpisodeList) Swap

func (el EpisodeList) Swap(i, j int)

func (EpisodeList) UnmarshalJSON

func (el EpisodeList) UnmarshalJSON(b []byte) error

NOTE: Since the String() representation doesn't include them, it's not exactly reversible if the user has set .Parts in any of the contained episodes.

type EpisodeRange

type EpisodeRange struct {
	Type  EpisodeType // Must be equal to both the Start and End types; if End is nil, must be equal to the Start type
	Start *Episode    // The start of the range
	End   *Episode    // The end of the range; may be nil, which represents an endless range
}

A range of episodes with a start and possibly without an end.

func EpisodeToRange

func EpisodeToRange(ep *Episode) *EpisodeRange

func ParseEpisodeRange

func ParseEpisodeRange(s string) *EpisodeRange

Parses a string in the AniDB API range format and converts into an EpisodeRange.

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	fmt.Println(misc.ParseEpisodeRange("01"))
	fmt.Println(misc.ParseEpisodeRange("S1-")) // endless range
	fmt.Println(misc.ParseEpisodeRange("T1-T3"))
	fmt.Println(misc.ParseEpisodeRange("5-S3")) // different episode types in range
	fmt.Println(misc.ParseEpisodeRange(""))     // invalid start of range

}
Output:

1
S1-
T1-T3
<nil>
<nil>

func (*EpisodeRange) ContainsEpisodes

func (er *EpisodeRange) ContainsEpisodes(ec EpisodeContainer) bool

If ec is an *Episode, returns true if the Episode is of the same type as the range and has a Number >= Start.Number; if End is defined, then the episode's Number must also be <= End.Number.

If ec is an *EpisodeRange, returns true if they are both of the same type and the ec's Start.Number is >= this range's Start.Number; also returns true if this EpisodeRange is unbounded or if the ec is bounded and ec's End.Number is <= this range's End.Number.

If ec is an EpisodeList, returns true if all listed EpisodeRanges are contained by this EpisodeRange.

Returns false otherwise.

func (*EpisodeRange) Episodes

func (er *EpisodeRange) Episodes() chan Episode

Returns a channel that can be used to iterate using for/range.

If the EpisodeRange is infinite, then the channel is also infinite. The caller is allowed to close the channel in such case.

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	a := misc.ParseEpisodeRange("C1-C3.2")
	for ep := range a.Episodes() {
		fmt.Println(ep)
	}

}
Output:

C1
C2
C3.0
C3.1
C3.2

func (*EpisodeRange) Equals

func (a *EpisodeRange) Equals(b *EpisodeRange) bool

Returns true if both ranges are of the same type and have identical start/end positions

func (*EpisodeRange) Format

func (er *EpisodeRange) Format(width int) string

func (*EpisodeRange) FormatLog

func (er *EpisodeRange) FormatLog(max int) string

func (*EpisodeRange) Infinite

func (er *EpisodeRange) Infinite() bool

func (*EpisodeRange) Len

func (er *EpisodeRange) Len() int

Returns the number of episodes that Episodes() would return.

Returns -1 for infinite ranges.

func (*EpisodeRange) Merge

func (a *EpisodeRange) Merge(b *EpisodeRange) (c *EpisodeRange)

Tries to merge a with b, returning a new *EpisodeRange that's a superset of both a and b.

Returns nil if a and b don't intersect, or are not adjacent.

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	a := misc.ParseEpisodeRange("5-7")
	b := misc.ParseEpisodeRange("8-12")
	fmt.Println(a.Merge(b)) // 5-7 + 8-12

	b = misc.ParseEpisodeRange("3-6")
	fmt.Println(a.Merge(b)) // 5-7 + 3-6

	b = misc.ParseEpisodeRange("10-12")
	fmt.Println(a.Merge(b)) // 5-7 + 10-12 (invalid, not touching)

	b = misc.ParseEpisodeRange("S1-S3")
	fmt.Println(a.Merge(b)) // 5-7 + S1-S3 (invalid, different types)

	a = misc.ParseEpisodeRange("S3-S10")
	fmt.Println(a.Merge(b)) // S3-S10 + S1-S3

}
Output:

05-12
3-7
<nil>
<nil>
S01-S10

func (*EpisodeRange) Simplify

func (er *EpisodeRange) Simplify() *EpisodeRange

Simplifies the Start/End ranges if one contains the other. Sets the pointers to be identical if the range is modified.

Modifies in-place, returns itself.

func (*EpisodeRange) Split

func (er *EpisodeRange) Split(ep *Episode) []*EpisodeRange

Splits the range into one or two ranges, using the given Episode as the split point. The Episode is not included in the resulting ranges.

Example
package main

import (
	"fmt"
	"github.com/Kovensky/go-anidb/misc"
)

func main() {
	a := misc.ParseEpisodeRange("1.0-1.3")
	b := misc.ParseEpisode("1.2")
	fmt.Println(a.Split(b))

	b = misc.ParseEpisode("1")
	fmt.Println(a.Split(b))

	a = misc.ParseEpisodeRange("1.1-2")
	fmt.Println(a.Split(b))

	b = misc.ParseEpisode("2")
	fmt.Println(a.Split(b))

	a = misc.ParseEpisodeRange("1-10")
	fmt.Println(a.Split(b))

	b = misc.ParseEpisode("4")
	fmt.Println(a.Split(b))

}
Output:

[1.0-1.1 1.3]
[<nil> <nil>]
[<nil> 2]
[1.1 <nil>]
[1 03-10]
[1-3 05-10]

func (*EpisodeRange) String

func (er *EpisodeRange) String() string

Converts the EpisodeRange into AniDB API range format.

func (*EpisodeRange) Valid

func (er *EpisodeRange) Valid() bool

Check if the given range is not nil, has a defined start and, if it has an end, that the end ends after the start.

type EpisodeType

type EpisodeType int

func (EpisodeType) String

func (et EpisodeType) String() string

type Formatter

type Formatter interface {
	// Returns a string where the number portion is 0-padded to fit 'width' digits
	Format(width int) string

	// Returns a string where the number portion is 0-padded to be the same length
	// as max.
	FormatLog(max int) string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL