Repository: xbmc/xbmc
File: xbmc/video/VideoInfoScanner.cpp (C++)

Code before:
      CRegExp reg2(true, CRegExp::autoUtf8);
      // check the remainder of the string for any further episodes.
      if (!byDate && reg2.RegComp(m_advancedSettings->m_tvshowMultiPartEnumRegExp))
      {
        int offset{0};
        int currentSeason{episode.iSeason};
        int currentEpisode{episode.iEpisode};

        // we want "long circuit" OR below so that both offsets are evaluated
        while (static_cast<int>((regexp2pos = reg2.RegFind(remainder.c_str() + offset)) > -1) |
               static_cast<int>((regexppos = reg.RegFind(remainder.c_str() + offset)) > -1))
        {
          if ((regexppos <= regexp2pos && regexppos != -1) || // season (or 'ep') match
              (regexppos >= 0 && regexp2pos == -1))
          {
            GetEpisodeAndSeasonFromRegExp(reg, episode, defaultSeason);
            if (currentSeason == episode.iSeason)
            {
              // Already added SxxEyy now loop (if needed) to SxxEzz
              const int last{episode.iEpisode};
              const int next{disableEpisodeRanges ? last : currentEpisode + 1};
              if (next <= last)
              {
                for (int e = next; e <= last; ++e)
                {
                  episode.iEpisode = e;
                  CLog::Log(LOGDEBUG, "VideoInfoScanner: Adding multipart episode {} [{}]",
                            episode.iEpisode, m_advancedSettings->m_tvshowMultiPartEnumRegExp);
                  episodeList.push_back(episode);
                }
              }
              else
              {
                // SxxEaa-SxxEbb is backwards - bb<aa
                CLog::Log(LOGDEBUG,
                          "VideoInfoScanner: Removing season {}, episode {} as range backwards",
                          episode.iSeason, episode.iEpisode);
                episodeList.pop_back();
              }
              currentEpisode = episode.iEpisode;
              remainder = reg.GetMatch(3);
            }
            else
            {
              // Two possible scenarios here:
              if (!remainder.starts_with("-"))
              {
                // (Sxx)Eyy has already been added and we now in a new range (eg. S00E01S01E01....)
                // Add first episode here
                currentSeason = episode.iSeason;
                currentEpisode = episode.iEpisode;

Review comment (refactor):
the function is getting pretty long, this block that expands a range is very similar to another block further down - could they be refactored/extracted? or could the function be refactored some other way?

Code after:
       * actual media file, we set episode.isFolder to true.
       */
      std::string strBasePath = item->GetBaseMoviePath(true);
      URIUtils::RemoveSlashAtEnd(strBasePath);
      strBasePath = URIUtils::GetFileName(strBasePath);

      if (reg.RegFind(strBasePath.c_str()) > -1)
      {
        EPISODE parent;
        if (byDate)
        {
          GetAirDateFromRegExp(reg, parent);
          if (episode.cDate == parent.cDate)
            episode.isFolder = true;
        }
        else
        {
          GetEpisodeAndSeasonFromRegExp(reg, parent, defaultSeason);
          if (episode.iSeason == parent.iSeason && episode.iEpisode == parent.iEpisode)
            episode.isFolder = true;
        }
      }

      // add what we found by now
      episodeList.push_back(episode);

      const bool disableEpisodeRanges{m_advancedSettings->m_disableEpisodeRanges};

      CRegExp reg2(true, CRegExp::autoUtf8);
      // check the remainder of the string for any further episodes.
      if (!byDate && reg2.RegComp(m_advancedSettings->m_tvshowMultiPartEnumRegExp))
      {
        int offset{0};
        int currentSeason{episode.iSeason};
        int currentEpisode{episode.iEpisode};

        // we want "long circuit" OR below so that both offsets are evaluated
        while (static_cast<int>((regexp2pos = reg2.RegFind(remainder.c_str() + offset)) > -1) |
               static_cast<int>((regexppos = reg.RegFind(remainder.c_str() + offset)) > -1))
        {
          if ((regexppos <= regexp2pos && regexppos != -1) || // season (or 'ep') match
              (regexppos >= 0 && regexp2pos == -1))
          {
            GetEpisodeAndSeasonFromRegExp(reg, episode, defaultSeason);
            if (currentSeason == episode.iSeason)
            {
              // Already added SxxEyy now loop (if needed) to SxxEzz
              const int last{episode.iEpisode};
              const int next{
                  disableEpisodeRanges || !remainder.starts_with("-") ? last : currentEpisode + 1};