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:
parent
6b71050000
commit
976fec6e94
|
@ -370,7 +370,6 @@
|
|||
C1FEE57A1C3223CC00D62832 /* watchdir.cc in Sources */ = {isa = PBXBuildFile; fileRef = C1FEE5751C3223CC00D62832 /* watchdir.cc */; };
|
||||
C1FEE57B1C3223CC00D62832 /* watchdir.h in Headers */ = {isa = PBXBuildFile; fileRef = C1FEE5761C3223CC00D62832 /* watchdir.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 */; };
|
||||
EDBDFA9E25AFCCA60093D9C1 /* evutil_time.c in Sources */ = {isa = PBXBuildFile; fileRef = EDBDFA9D25AFCCA60093D9C1 /* evutil_time.c */; };
|
||||
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>"; };
|
||||
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>"; };
|
||||
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>"; };
|
||||
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>"; };
|
||||
|
@ -1390,7 +1388,6 @@
|
|||
4D1838DC09DEC04A0047D688 /* libtransmission */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
CAB35C63252F6F5E00552A55 /* mime-types.cc */,
|
||||
CAB35C62252F6F5E00552A55 /* mime-types.h */,
|
||||
C1077A4A183EB29600634C22 /* error.cc */,
|
||||
C1077A4B183EB29600634C22 /* error.h */,
|
||||
|
@ -2472,7 +2469,6 @@
|
|||
A201527E0D1C270F0081714F /* torrent-ctor.cc in Sources */,
|
||||
A2D22A130D65EEE700007D5F /* verify.cc in Sources */,
|
||||
4D4ADFC70DA1631500A68297 /* blocklist.cc in Sources */,
|
||||
CAB35C65252F6F5E00552A55 /* mime-types.cc in Sources */,
|
||||
A29DF8B90DB2544C00D04E5A /* resume.cc in Sources */,
|
||||
A2A4E9220DE0F7EB000CE197 /* web.cc in Sources */,
|
||||
A2A4EA0E0DE106EB000CE197 /* ConvertUTF.c in Sources */,
|
||||
|
|
|
@ -33,7 +33,6 @@ set(PROJECT_FILES
|
|||
magnet.cc
|
||||
makemeta.cc
|
||||
metainfo.cc
|
||||
mime-types.cc
|
||||
natpmp.cc
|
||||
net.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
|
||||
.map(([suffix, mime_type]) => ` { "${suffix}", "${mime_type}" }`)
|
||||
.map(([suffix, mime_type]) => ` { "${suffix}", "${mime_type}" }`)
|
||||
.sort()
|
||||
.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}
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MIME_TYPE_SUFFIX_MAXLEN ${max_suffix_len}
|
||||
#define MIME_TYPE_SUFFIX_COUNT ${suffixes.length}
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
|
||||
struct mime_type_suffix
|
||||
{
|
||||
char const* suffix;
|
||||
char const* mime_type;
|
||||
std::string_view suffix;
|
||||
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) {
|
||||
console.error(e.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <cstring> /* strerror(), memset(), memmem() */
|
||||
#include <ctime> /* nanosleep() */
|
||||
#include <exception>
|
||||
#include <iterator> // std::back_inserter
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -1914,40 +1915,32 @@ void tr_net_init(void)
|
|||
|
||||
/// 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 const* entry = static_cast<struct mime_type_suffix const*>(vb);
|
||||
return tr_strcmp0(suffix, entry->suffix);
|
||||
}
|
||||
|
||||
char const* tr_get_mime_type_for_filename(char const* filename)
|
||||
{
|
||||
struct mime_type_suffix const* info = nullptr;
|
||||
|
||||
char const* in = strrchr(filename, '.');
|
||||
if (in != nullptr)
|
||||
auto constexpr compare = [](mime_type_suffix const& entry, auto const& suffix)
|
||||
{
|
||||
++in; // walk past '.'
|
||||
if (strlen(in) <= MIME_TYPE_SUFFIX_MAXLEN)
|
||||
return entry.suffix < suffix;
|
||||
};
|
||||
|
||||
auto const pos = filename.rfind('.');
|
||||
if (pos != filename.npos)
|
||||
{
|
||||
// make a lowercase copy of the file suffix
|
||||
filename.remove_prefix(pos + 1);
|
||||
auto suffix_lc = std::string{};
|
||||
std::transform(
|
||||
std::begin(filename),
|
||||
std::end(filename),
|
||||
std::back_inserter(suffix_lc),
|
||||
[](auto c) { return std::tolower(c); });
|
||||
|
||||
// find it
|
||||
auto const it = std::lower_bound(std::begin(mime_type_suffixes), std::end(mime_type_suffixes), suffix_lc, compare);
|
||||
if (it != std::end(mime_type_suffixes) && suffix_lc == it->suffix)
|
||||
{
|
||||
char lowercase_suffix[MIME_TYPE_SUFFIX_MAXLEN + 1];
|
||||
char* out = lowercase_suffix;
|
||||
while (*in != '\0')
|
||||
{
|
||||
*out++ = (char)tolower((unsigned char)*in++);
|
||||
}
|
||||
|
||||
*out = '\0';
|
||||
|
||||
info = static_cast<struct mime_type_suffix const*>(bsearch(
|
||||
lowercase_suffix,
|
||||
mime_type_suffixes,
|
||||
TR_N_ELEMENTS(mime_type_suffixes),
|
||||
sizeof(*mime_type_suffixes),
|
||||
compareSuffix));
|
||||
return std::data(it->mime_type);
|
||||
}
|
||||
}
|
||||
|
||||
return info != nullptr ? info->mime_type : nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h> /* size_t */
|
||||
#include <string_view>
|
||||
#include <time.h> /* time_t */
|
||||
#include <vector>
|
||||
|
||||
|
@ -59,7 +60,7 @@ char const* tr_strip_positional_args(char const* fmt);
|
|||
|
||||
#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.
|
||||
|
|
Loading…
Reference in New Issue