ronantakizawa/codereview-bench huggingface dataset:
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:
#include <algorithm>
#include <cassert>
#include <charconv>
#include <ranges>
#include <vector>

#include <arpa/inet.h>
#include <netinet/in.h>

using namespace PVR;
using namespace XFILE;

const CAdvancedSettings* URIUtils::m_advancedSettings = nullptr;

namespace
{

constexpr std::string_view DecodeURLSpecialChars{"+%"};

std::optional<char> DecodeOctlet(std::string_view& encoded)
{
  if (encoded.length() < 2)
    return {};

  uint8_t decimal{};
  const auto res = std::from_chars(&encoded[0], &encoded[2], decimal, 16);

  if (res.ec != std::errc() || res.ptr != &encoded[2])
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char& specialChar = encoded[special];

Review comment (suggestion):
If your length is only two the `[2]` de-references a value after the string. You have to use pointer arithmetic to create the end iterator.
```suggestion
  const auto res = std::from_chars(encoded.data(), encoded.data() + 2, decimal, 16);
```

Code after:
#include <algorithm>
#include <cassert>
#include <charconv>
#include <ranges>
#include <vector>

#include <arpa/inet.h>
#include <netinet/in.h>

using namespace PVR;
using namespace XFILE;

const CAdvancedSettings* URIUtils::m_advancedSettings = nullptr;

namespace
{

constexpr std::string_view DecodeURLSpecialChars{"+%"};

std::optional<char> DecodeOctlet(std::string_view& encoded)
{
  if (encoded.length() < 2)
    return {};

  uint8_t decimal{};
  const auto res = std::from_chars(encoded.data(), encoded.data() + 2, decimal, 16);

  if (res.ec != std::errc() || res.ptr != encoded.data() + 2)
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char specialChar = encoded[special];
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:
#include <charconv>
#include <ranges>
#include <vector>

#include <arpa/inet.h>
#include <netinet/in.h>

using namespace PVR;
using namespace XFILE;

const CAdvancedSettings* URIUtils::m_advancedSettings = nullptr;

namespace
{

constexpr std::string_view DecodeURLSpecialChars{"+%"};

std::optional<char> DecodeOctlet(std::string_view& encoded)
{
  if (encoded.length() < 2)
    return {};

  uint8_t decimal{};
  const auto res = std::from_chars(&encoded[0], &encoded[2], decimal, 16);

  if (res.ec != std::errc() || res.ptr != &encoded[2])
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char& specialChar = encoded[special];
    encoded = encoded.substr(special + 1);


Review comment (suggestion):
For the same reason as above:
```suggestion
  if (res.ec != std::errc() || res.ptr != encoded.data() + 2)
```

Code after:
#include <charconv>
#include <ranges>
#include <vector>

#include <arpa/inet.h>
#include <netinet/in.h>

using namespace PVR;
using namespace XFILE;

const CAdvancedSettings* URIUtils::m_advancedSettings = nullptr;

namespace
{

constexpr std::string_view DecodeURLSpecialChars{"+%"};

std::optional<char> DecodeOctlet(std::string_view& encoded)
{
  if (encoded.length() < 2)
    return {};

  uint8_t decimal{};
  const auto res = std::from_chars(encoded.data(), encoded.data() + 2, decimal, 16);

  if (res.ec != std::errc() || res.ptr != encoded.data() + 2)
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char specialChar = encoded[special];
    encoded = encoded.substr(special + 1);
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:
  const auto res = std::from_chars(&encoded[0], &encoded[2], decimal, 16);

  if (res.ec != std::errc() || res.ptr != &encoded[2])
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char& specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else if (auto decoded = DecodeOctlet(encoded); decoded)
      decodedUrl += *decoded; // Decoded octet triplet '%2f'
    else
      decodedUrl += '%';
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, const std::string_view& URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      oss << fmt::format("%{:02x}", ch);
  }
  return oss.str();
}

Review comment (suggestion):
```suggestion
    const char specialChar = encoded[special];
```

Code after:
  const auto res = std::from_chars(encoded.data(), encoded.data() + 2, decimal, 16);

  if (res.ec != std::errc() || res.ptr != encoded.data() + 2)
    return {};

  encoded = encoded.substr(2);
  return decimal;
}

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else
      decodedUrl += DecodeOctlet(encoded).value_or('%'); // Decoded octet triplet '%2f'
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, std::string_view URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      fmt::format_to(std::ostreambuf_iterator(oss), "%{:02x}", ch);
  }
  return std::move(oss).str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char& specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else if (auto decoded = DecodeOctlet(encoded); decoded)
      decodedUrl += *decoded; // Decoded octet triplet '%2f'
    else
      decodedUrl += '%';
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, const std::string_view& URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      oss << fmt::format("%{:02x}", ch);
  }
  return oss.str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{

Review comment (suggestion):
```suggestion
    if (specialChar == '+')
      decodedUrl += ' ';
    else
      decodedUrl += DecodeOctlet(encoded).value_or('%');
```

Code after:

} // Unnamed namespace

std::string URIUtils::URLDecode(std::string_view encoded)
{
  /* result will always be less than or equal to source */
  std::string decodedUrl{};
  decodedUrl.reserve(encoded.length());

  while (true)
  {
    const auto special = encoded.find_first_of(DecodeURLSpecialChars);
    decodedUrl += encoded.substr(0, special);

    if (special == std::string::npos)
      break;

    const char specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else
      decodedUrl += DecodeOctlet(encoded).value_or('%'); // Decoded octet triplet '%2f'
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, std::string_view URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      fmt::format_to(std::ostreambuf_iterator(oss), "%{:02x}", ch);
  }
  return std::move(oss).str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{
  m_advancedSettings = nullptr;
}
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:
    if (special == std::string::npos)
      break;

    const char& specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else if (auto decoded = DecodeOctlet(encoded); decoded)
      decodedUrl += *decoded; // Decoded octet triplet '%2f'
    else
      decodedUrl += '%';
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, const std::string_view& URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      oss << fmt::format("%{:02x}", ch);
  }
  return oss.str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{
  m_advancedSettings = nullptr;
}

/* returns filename extension including period of filename */
std::string URIUtils::GetExtension(const CURL& url)
{
  return URIUtils::GetExtension(url.GetFileName());
}

std::string URIUtils::GetExtension(const std::string& strFileName)
{
  if (IsURL(strFileName))
  {
    CURL url(strFileName);

Review comment (suggestion):
No need for a temporary string:
```suggestion
      fmt::format_to(std::ostreambuf_iterator(oss), "%{:02x}", ch);
```

Code after:
    if (special == std::string::npos)
      break;

    const char specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else
      decodedUrl += DecodeOctlet(encoded).value_or('%'); // Decoded octet triplet '%2f'
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, std::string_view URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      fmt::format_to(std::ostreambuf_iterator(oss), "%{:02x}", ch);
  }
  return std::move(oss).str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{
  m_advancedSettings = nullptr;
}

/* returns filename extension including period of filename */
std::string URIUtils::GetExtension(const CURL& url)
{
  return URIUtils::GetExtension(url.GetFileName());
}

std::string URIUtils::GetExtension(const std::string& strFileName)
{
  if (IsURL(strFileName))
  {
    CURL url(strFileName);
    return GetExtension(url.GetFileName());
  }
Repository: xbmc/xbmc
File: xbmc/utils/URIUtils.cpp (C++)

Code before:

    const char& specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else if (auto decoded = DecodeOctlet(encoded); decoded)
      decodedUrl += *decoded; // Decoded octet triplet '%2f'
    else
      decodedUrl += '%';
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, const std::string_view& URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      oss << fmt::format("%{:02x}", ch);
  }
  return oss.str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{
  m_advancedSettings = nullptr;
}

/* returns filename extension including period of filename */
std::string URIUtils::GetExtension(const CURL& url)
{
  return URIUtils::GetExtension(url.GetFileName());
}

std::string URIUtils::GetExtension(const std::string& strFileName)
{
  if (IsURL(strFileName))
  {
    CURL url(strFileName);
    return GetExtension(url.GetFileName());
  }

Review comment (suggestion):
Move the string out of the `std::ostringstream` instead of creating a copy:
```suggestion
  return std::move(oss).str();
```

Code after:

    const char specialChar = encoded[special];
    encoded = encoded.substr(special + 1);

    if (specialChar == '+')
      decodedUrl += ' ';
    else
      decodedUrl += DecodeOctlet(encoded).value_or('%'); // Decoded octet triplet '%2f'
  }

  return decodedUrl;
}

std::string URIUtils::URLEncode(std::string_view decoded, std::string_view URLSpec)
{
  std::ostringstream oss;
  for (const auto& ch : decoded)
  {
    if (StringUtils::isasciialphanum(ch) || URLSpec.find(ch) != std::string::npos)
      oss << ch;
    else
      fmt::format_to(std::ostreambuf_iterator(oss), "%{:02x}", ch);
  }
  return std::move(oss).str();
}

void URIUtils::RegisterAdvancedSettings(const CAdvancedSettings& advancedSettings)
{
  m_advancedSettings = &advancedSettings;
}

void URIUtils::UnregisterAdvancedSettings()
{
  m_advancedSettings = nullptr;
}

/* returns filename extension including period of filename */
std::string URIUtils::GetExtension(const CURL& url)
{
  return URIUtils::GetExtension(url.GetFileName());
}

std::string URIUtils::GetExtension(const std::string& strFileName)
{
  if (IsURL(strFileName))
  {
    CURL url(strFileName);
    return GetExtension(url.GetFileName());
  }

  size_t period = strFileName.find_last_of("./\\");
Repository: xbmc/xbmc
File: xbmc/utils/test/TestURIUtils.cpp (C++)

Code before:
  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "zip://%2fpath%2fto%2farchive%2fsome%2darchive%2efile%2ezip/video.avi";
  newUrl = "zip://%2fpath%2fto%2farchive%2fsome-archive.file.zip/video.avi";

  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long%2dnamed%2efile";
  newUrl = "/path/to/some/long%2dnamed%2efile";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long-named.file";
  newUrl = "/path/to/some/long-named.file";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());
}

struct URLEncodings
{
  std::string input;
  std::string encoded;
};

URLEncodings EncodingTestData[] = {
    // No crash
    {"", ""},

    // No Encoding needed
    {"a", "a"},
    {"z", "z"},
    {"A", "A"},
    {"Z", "Z"},
    {"0", "0"},
    {"9", "9"},

    // Encoding needed
    {" ", "%20"},
    {"\"", "%22"},
    {"#", "%23"},
    {"$", "%24"},
    {"%", "%25"},
    {"&", "%26"},
    {"'", "%27"},
    {"*", "%2a"},
    {"+", "%2b"},
    {",", "%2c"},

Review comment (suggestion):
```suggestion
  std::string_view input;
  std::string_view encoded;
```

Code after:
  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "zip://%2fpath%2fto%2farchive%2fsome%2darchive%2efile%2ezip/video.avi";
  newUrl = "zip://%2fpath%2fto%2farchive%2fsome-archive.file.zip/video.avi";

  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long%2dnamed%2efile";
  newUrl = "/path/to/some/long%2dnamed%2efile";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long-named.file";
  newUrl = "/path/to/some/long-named.file";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());
}

struct URLEncodings
{
  std::string_view input;
  std::string_view encoded;
};

constexpr URLEncodings EncodingTestData[] = {
    // No crash
    {"", ""},

    // No Encoding needed
    {"a", "a"},
    {"z", "z"},
    {"A", "A"},
    {"Z", "Z"},
    {"0", "0"},
    {"9", "9"},

    // Encoding needed
    {" ", "%20"},
    {"\"", "%22"},
    {"#", "%23"},
    {"$", "%24"},
    {"%", "%25"},
    {"&", "%26"},
    {"'", "%27"},
    {"*", "%2a"},
    {"+", "%2b"},
    {",", "%2c"},
Repository: xbmc/xbmc
File: xbmc/platform/posix/filesystem/PosixDirectory.cpp (C++)

Code before:
          fileItems.emplace_back(std::make_shared<CFileItem>(itemLabel));

      // Unix-based readdir implementations may return an incorrect dirent.d_ino value that
      // is not equal to the (correct) stat() obtained one. In this case the file type
      // could not be determined and the value of dirent.d_type is set to DT_UNKNOWN.
      // In order to get a correct value we have to incur the cost of calling stat.
      bool isDir = entry->d_type == DT_DIR;
      if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK ||
          !(m_flags & DIR_FLAG_NO_FILE_INFO))
      {
        if (stat(itemPath.c_str(), &buffer) == 0)
        {
          isDir = S_ISDIR(buffer.st_mode);

          KODI::TIME::FileTime fileTime;
          KODI::TIME::FileTime localTime;
          KODI::TIME::TimeTToFileTime(buffer.st_mtime, &fileTime);
          KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime);

          item->SetDateTime(localTime);
          item->SetSize(isDir ? 0 : buffer.st_size);
        }
      }

      if (isDir)
        itemPath += "/";

      item->SetFolder(isDir);
      item->SetPath(itemPath);

      if (StringUtils::StartsWith(name, "."))
        item->SetProperty("file:hidden", true);
    }
  }
  items.AddItems(std::move(fileItems));

  closedir(dir);
  return true;
}

bool CPosixDirectory::Create(const CURL& url)
{
  if (!Create(url.Get()))
    return Exists(url);

  return true;
}

bool CPosixDirectory::Create(const std::string& path)
{
  if (mkdir(path.c_str(), 0755) != 0)

Review comment (suggestion):
```suggestion
        itemPath.push_back('/');
```

Code after:
      // Unix-based readdir implementations may return an incorrect dirent.d_ino value that
      // is not equal to the (correct) stat() obtained one. In this case the file type
      // could not be determined and the value of dirent.d_type is set to DT_UNKNOWN.
      // In order to get a correct value we have to incur the cost of calling stat.
      bool isDir = entry->d_type == DT_DIR;
      if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK ||
          !(m_flags & DIR_FLAG_NO_FILE_INFO))
      {
        if (stat(itemPath.c_str(), &buffer) == 0)
        {
          isDir = S_ISDIR(buffer.st_mode);

          KODI::TIME::FileTime fileTime;
          KODI::TIME::FileTime localTime;
          KODI::TIME::TimeTToFileTime(buffer.st_mtime, &fileTime);
          KODI::TIME::FileTimeToLocalFileTime(&fileTime, &localTime);

          item->SetDateTime(localTime);
          item->SetSize(isDir ? 0 : buffer.st_size);
        }
      }

      if (isDir)
        itemPath.push_back('/');

      item->SetFolder(isDir);
      item->SetPath(itemPath);

      if (name.starts_with('.'))
        item->SetProperty("file:hidden", true);
    }
  }
  items.AddItems(std::move(fileItems));

  closedir(dir);
  return true;
}

bool CPosixDirectory::Create(const CURL& url)
{
  if (!Create(url.Get()))
    return Exists(url);

  return true;
}

bool CPosixDirectory::Create(const std::string& path)
{
  if (mkdir(path.c_str(), 0755) != 0)
  {
    if (errno == ENOENT)