mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
refactor: make mime-type array inline constexpr (#1872)
* refactor: make mime-type array inline constexpr * fix: iwyu <iterator> for std::back_inserter * fix: remove newly-unused variable in mime-types.js
This commit is contained in:
@@ -370,7 +370,6 @@
|
|||||||
C1FEE57A1C3223CC00D62832 /* watchdir.cc in Sources */ = {isa = PBXBuildFile; fileRef = C1FEE5751C3223CC00D62832 /* watchdir.cc */; };
|
C1FEE57A1C3223CC00D62832 /* watchdir.cc in Sources */ = {isa = PBXBuildFile; fileRef = C1FEE5751C3223CC00D62832 /* watchdir.cc */; };
|
||||||
C1FEE57B1C3223CC00D62832 /* watchdir.h in Headers */ = {isa = PBXBuildFile; fileRef = C1FEE5761C3223CC00D62832 /* watchdir.h */; };
|
C1FEE57B1C3223CC00D62832 /* watchdir.h in Headers */ = {isa = PBXBuildFile; fileRef = C1FEE5761C3223CC00D62832 /* watchdir.h */; };
|
||||||
CAB35C64252F6F5E00552A55 /* mime-types.h in Headers */ = {isa = PBXBuildFile; fileRef = CAB35C62252F6F5E00552A55 /* mime-types.h */; };
|
CAB35C64252F6F5E00552A55 /* mime-types.h in Headers */ = {isa = PBXBuildFile; fileRef = CAB35C62252F6F5E00552A55 /* mime-types.h */; };
|
||||||
CAB35C65252F6F5E00552A55 /* mime-types.cc in Sources */ = {isa = PBXBuildFile; fileRef = CAB35C63252F6F5E00552A55 /* mime-types.cc */; };
|
|
||||||
E138A9780C04D88F00C5426C /* ProgressGradients.mm in Sources */ = {isa = PBXBuildFile; fileRef = E138A9760C04D88F00C5426C /* ProgressGradients.mm */; };
|
E138A9780C04D88F00C5426C /* ProgressGradients.mm in Sources */ = {isa = PBXBuildFile; fileRef = E138A9760C04D88F00C5426C /* ProgressGradients.mm */; };
|
||||||
EDBDFA9E25AFCCA60093D9C1 /* evutil_time.c in Sources */ = {isa = PBXBuildFile; fileRef = EDBDFA9D25AFCCA60093D9C1 /* evutil_time.c */; };
|
EDBDFA9E25AFCCA60093D9C1 /* evutil_time.c in Sources */ = {isa = PBXBuildFile; fileRef = EDBDFA9D25AFCCA60093D9C1 /* evutil_time.c */; };
|
||||||
F63480631E1D7274005B9E09 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F63480621E1D7274005B9E09 /* Images.xcassets */; };
|
F63480631E1D7274005B9E09 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F63480621E1D7274005B9E09 /* Images.xcassets */; };
|
||||||
@@ -1031,7 +1030,6 @@
|
|||||||
C1FEE5751C3223CC00D62832 /* watchdir.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = watchdir.cc; sourceTree = "<group>"; };
|
C1FEE5751C3223CC00D62832 /* watchdir.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = watchdir.cc; sourceTree = "<group>"; };
|
||||||
C1FEE5761C3223CC00D62832 /* watchdir.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = watchdir.h; sourceTree = "<group>"; };
|
C1FEE5761C3223CC00D62832 /* watchdir.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = watchdir.h; sourceTree = "<group>"; };
|
||||||
CAB35C62252F6F5E00552A55 /* mime-types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mime-types.h"; sourceTree = "<group>"; };
|
CAB35C62252F6F5E00552A55 /* mime-types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "mime-types.h"; sourceTree = "<group>"; };
|
||||||
CAB35C63252F6F5E00552A55 /* mime-types.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "mime-types.cc"; sourceTree = "<group>"; };
|
|
||||||
E138A9750C04D88F00C5426C /* ProgressGradients.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ProgressGradients.h; sourceTree = "<group>"; };
|
E138A9750C04D88F00C5426C /* ProgressGradients.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ProgressGradients.h; sourceTree = "<group>"; };
|
||||||
E138A9760C04D88F00C5426C /* ProgressGradients.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ProgressGradients.mm; sourceTree = "<group>"; };
|
E138A9760C04D88F00C5426C /* ProgressGradients.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ProgressGradients.mm; sourceTree = "<group>"; };
|
||||||
EDBDFA9D25AFCCA60093D9C1 /* evutil_time.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = evutil_time.c; sourceTree = "<group>"; };
|
EDBDFA9D25AFCCA60093D9C1 /* evutil_time.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = evutil_time.c; sourceTree = "<group>"; };
|
||||||
@@ -1390,7 +1388,6 @@
|
|||||||
4D1838DC09DEC04A0047D688 /* libtransmission */ = {
|
4D1838DC09DEC04A0047D688 /* libtransmission */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
CAB35C63252F6F5E00552A55 /* mime-types.cc */,
|
|
||||||
CAB35C62252F6F5E00552A55 /* mime-types.h */,
|
CAB35C62252F6F5E00552A55 /* mime-types.h */,
|
||||||
C1077A4A183EB29600634C22 /* error.cc */,
|
C1077A4A183EB29600634C22 /* error.cc */,
|
||||||
C1077A4B183EB29600634C22 /* error.h */,
|
C1077A4B183EB29600634C22 /* error.h */,
|
||||||
@@ -2472,7 +2469,6 @@
|
|||||||
A201527E0D1C270F0081714F /* torrent-ctor.cc in Sources */,
|
A201527E0D1C270F0081714F /* torrent-ctor.cc in Sources */,
|
||||||
A2D22A130D65EEE700007D5F /* verify.cc in Sources */,
|
A2D22A130D65EEE700007D5F /* verify.cc in Sources */,
|
||||||
4D4ADFC70DA1631500A68297 /* blocklist.cc in Sources */,
|
4D4ADFC70DA1631500A68297 /* blocklist.cc in Sources */,
|
||||||
CAB35C65252F6F5E00552A55 /* mime-types.cc in Sources */,
|
|
||||||
A29DF8B90DB2544C00D04E5A /* resume.cc in Sources */,
|
A29DF8B90DB2544C00D04E5A /* resume.cc in Sources */,
|
||||||
A2A4E9220DE0F7EB000CE197 /* web.cc in Sources */,
|
A2A4E9220DE0F7EB000CE197 /* web.cc in Sources */,
|
||||||
A2A4EA0E0DE106EB000CE197 /* ConvertUTF.c in Sources */,
|
A2A4EA0E0DE106EB000CE197 /* ConvertUTF.c in Sources */,
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ set(PROJECT_FILES
|
|||||||
magnet.cc
|
magnet.cc
|
||||||
makemeta.cc
|
makemeta.cc
|
||||||
metainfo.cc
|
metainfo.cc
|
||||||
mime-types.cc
|
|
||||||
natpmp.cc
|
natpmp.cc
|
||||||
net.cc
|
net.cc
|
||||||
peer-io.cc
|
peer-io.cc
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -34,39 +34,32 @@ https.get(url, (res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const max_suffix_len = suffixes
|
|
||||||
.reduce((acc, [suffix]) => Math.max(acc, suffix.length), 0);
|
|
||||||
|
|
||||||
const mime_type_lines = suffixes
|
const mime_type_lines = suffixes
|
||||||
.map(([suffix, mime_type]) => ` { "${suffix}", "${mime_type}" }`)
|
.map(([suffix, mime_type]) => ` { "${suffix}", "${mime_type}" }`)
|
||||||
.sort()
|
.sort()
|
||||||
.join(',\n');
|
.join(',\n');
|
||||||
fs.writeFileSync('mime-types.c', `${copyright}
|
|
||||||
|
|
||||||
#include "mime-types.h"
|
|
||||||
|
|
||||||
struct mime_type_suffix const mime_type_suffixes[MIME_TYPE_SUFFIX_COUNT] =
|
|
||||||
{
|
|
||||||
${mime_type_lines}
|
|
||||||
};
|
|
||||||
`);
|
|
||||||
fs.writeFileSync('mime-types.h', `${copyright}
|
fs.writeFileSync('mime-types.h', `${copyright}
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define MIME_TYPE_SUFFIX_MAXLEN ${max_suffix_len}
|
#include <array>
|
||||||
#define MIME_TYPE_SUFFIX_COUNT ${suffixes.length}
|
#include <string_view>
|
||||||
|
|
||||||
struct mime_type_suffix
|
struct mime_type_suffix
|
||||||
{
|
{
|
||||||
char const* suffix;
|
std::string_view suffix;
|
||||||
char const* mime_type;
|
std::string_view mime_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct mime_type_suffix const mime_type_suffixes[MIME_TYPE_SUFFIX_COUNT];
|
inline auto constexpr mime_type_suffixes = std::array<mime_type_suffix, ${suffixes.length}>
|
||||||
|
{{
|
||||||
|
${mime_type_lines}
|
||||||
|
}};
|
||||||
|
|
||||||
`);
|
`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e.message);
|
console.error(e.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#include <cstring> /* strerror(), memset(), memmem() */
|
#include <cstring> /* strerror(), memset(), memmem() */
|
||||||
#include <ctime> /* nanosleep() */
|
#include <ctime> /* nanosleep() */
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <iterator> // std::back_inserter
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -1914,40 +1915,32 @@ void tr_net_init(void)
|
|||||||
|
|
||||||
/// mime-type
|
/// mime-type
|
||||||
|
|
||||||
static int compareSuffix(void const* va, void const* vb)
|
char const* tr_get_mime_type_for_filename(std::string_view filename)
|
||||||
{
|
{
|
||||||
auto const* suffix = static_cast<char const*>(va);
|
auto constexpr compare = [](mime_type_suffix const& entry, auto const& suffix)
|
||||||
auto const* entry = static_cast<struct mime_type_suffix const*>(vb);
|
{
|
||||||
return tr_strcmp0(suffix, entry->suffix);
|
return entry.suffix < suffix;
|
||||||
}
|
};
|
||||||
|
|
||||||
char const* tr_get_mime_type_for_filename(char const* filename)
|
auto const pos = filename.rfind('.');
|
||||||
{
|
if (pos != filename.npos)
|
||||||
struct mime_type_suffix const* info = nullptr;
|
{
|
||||||
|
// make a lowercase copy of the file suffix
|
||||||
char const* in = strrchr(filename, '.');
|
filename.remove_prefix(pos + 1);
|
||||||
if (in != nullptr)
|
auto suffix_lc = std::string{};
|
||||||
{
|
std::transform(
|
||||||
++in; // walk past '.'
|
std::begin(filename),
|
||||||
if (strlen(in) <= MIME_TYPE_SUFFIX_MAXLEN)
|
std::end(filename),
|
||||||
{
|
std::back_inserter(suffix_lc),
|
||||||
char lowercase_suffix[MIME_TYPE_SUFFIX_MAXLEN + 1];
|
[](auto c) { return std::tolower(c); });
|
||||||
char* out = lowercase_suffix;
|
|
||||||
while (*in != '\0')
|
// find it
|
||||||
{
|
auto const it = std::lower_bound(std::begin(mime_type_suffixes), std::end(mime_type_suffixes), suffix_lc, compare);
|
||||||
*out++ = (char)tolower((unsigned char)*in++);
|
if (it != std::end(mime_type_suffixes) && suffix_lc == it->suffix)
|
||||||
}
|
{
|
||||||
|
return std::data(it->mime_type);
|
||||||
*out = '\0';
|
}
|
||||||
|
}
|
||||||
info = static_cast<struct mime_type_suffix const*>(bsearch(
|
|
||||||
lowercase_suffix,
|
return nullptr;
|
||||||
mime_type_suffixes,
|
|
||||||
TR_N_ELEMENTS(mime_type_suffixes),
|
|
||||||
sizeof(*mime_type_suffixes),
|
|
||||||
compareSuffix));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return info != nullptr ? info->mime_type : nullptr;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h> /* size_t */
|
#include <stddef.h> /* size_t */
|
||||||
|
#include <string_view>
|
||||||
#include <time.h> /* time_t */
|
#include <time.h> /* time_t */
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ char const* tr_strip_positional_args(char const* fmt);
|
|||||||
|
|
||||||
#define TR_N_ELEMENTS(ary) (sizeof(ary) / sizeof(*(ary)))
|
#define TR_N_ELEMENTS(ary) (sizeof(ary) / sizeof(*(ary)))
|
||||||
|
|
||||||
char const* tr_get_mime_type_for_filename(char const* filename);
|
char const* tr_get_mime_type_for_filename(std::string_view filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
|
* @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
|
||||||
|
|||||||
Reference in New Issue
Block a user