aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/utils/EpisodeNameParser.cpp
blob: de2b8ac7c99c2e936b73344327d7db736b07b955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "../../../plugins/utils/EpisodeNameParser.hpp"

namespace QuickMedia {
    static bool has_season_in_name(std::string_view episode_name) {
        size_t sep_count = 0;
        size_t index = 0;
        while(true) {
            size_t next_index = episode_name.find(" - ", index);
            if(next_index == std::string_view::npos)
                break;

            index = next_index + 3;
            ++sep_count;
        }
        return sep_count >= 2;
    }

    static bool is_whitespace(char c) {
        return c == ' ' || c == '\n' || c == '\t' || c == '\v';
    }

    static std::string_view strip_left(std::string_view str) {
        size_t i = 0;
        for(; i < str.size(); ++i) {
            if(!is_whitespace(str[i]))
                break;
        }
        return str.substr(i);
    }

    static std::string_view strip_right(std::string_view str) {
        long i = (long)str.size() - 1;
        for(; i >= 0; --i) {
            if(!is_whitespace(str[i]))
                break;
        }
        return str.substr(0, i + 1);
    }

    static std::string_view episode_name_extract_group(std::string_view &episode_name) {
        episode_name = strip_left(episode_name);
        if(episode_name[0] == '[') {
            size_t group_end_index = episode_name.find(']', 1);
            if(group_end_index == std::string_view::npos)
                return {};

            std::string_view group = episode_name.substr(1, group_end_index - 1);
            episode_name.remove_prefix(group_end_index + 1);
            return group;
        }
        return {};
    }

    static std::string_view episode_name_extract_anime(std::string_view &episode_name) {
        episode_name = strip_left(episode_name);
        size_t episode_or_season_sep_index = episode_name.find(" - ");
        if(episode_or_season_sep_index == std::string_view::npos)
            episode_or_season_sep_index = episode_name.size();

        std::string_view anime = episode_name.substr(0, episode_or_season_sep_index);
        anime = strip_right(anime);
        if(episode_or_season_sep_index + 3 > episode_name.size())
            episode_name = {};
        else
            episode_name.remove_prefix(episode_or_season_sep_index + 3);

        return anime;
    }

    static std::string_view episode_name_extract_season(std::string_view &episode_name) {
        return episode_name_extract_anime(episode_name);
    }

    static bool is_num_real_char(char c) {
        return (c >= '0' && c <= '9') || c == '.';
    }

    static std::string_view episode_name_extract_episode(std::string_view &episode_name) {
        episode_name = strip_left(episode_name);
        size_t i = 0;
        for(; i < episode_name.size(); ++i) {
            if(!is_num_real_char(episode_name[i]))
                break;
        }

        if(i == 0)
            return {};

        std::string_view episode = episode_name.substr(0, i);
        episode_name.remove_prefix(i + 1);
        return episode;
    }

    std::optional<EpisodeNameParts> episode_name_extract_parts(std::string_view episode_name) {
        EpisodeNameParts name_parts;
        const bool has_season = has_season_in_name(episode_name);

        name_parts.group = episode_name_extract_group(episode_name);
        name_parts.anime = episode_name_extract_anime(episode_name);
        if(name_parts.anime.empty())
            return std::nullopt;
        
        if(has_season)
            name_parts.season = episode_name_extract_season(episode_name);

        name_parts.episode = episode_name_extract_episode(episode_name);
        if(name_parts.episode.empty())
            return std::nullopt;

        return name_parts;
    }
}