Optimize Substring and StringBuilder usage

This commit is contained in:
Bond_009 2020-07-29 13:17:01 +02:00
parent 8c1dab146c
commit d4092846e4
12 changed files with 69 additions and 46 deletions

View File

@ -122,15 +122,15 @@ namespace Emby.Dlna
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendLine("No matching device profile found. The default will need to be used."); builder.AppendLine("No matching device profile found. The default will need to be used.");
builder.AppendLine(string.Format("DeviceDescription:{0}", profile.DeviceDescription ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "DeviceDescription:{0}", profile.DeviceDescription ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("FriendlyName:{0}", profile.FriendlyName ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "FriendlyName:{0}", profile.FriendlyName ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("Manufacturer:{0}", profile.Manufacturer ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "Manufacturer:{0}", profile.Manufacturer ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("ManufacturerUrl:{0}", profile.ManufacturerUrl ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "ManufacturerUrl:{0}", profile.ManufacturerUrl ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("ModelDescription:{0}", profile.ModelDescription ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "ModelDescription:{0}", profile.ModelDescription ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("ModelName:{0}", profile.ModelName ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "ModelName:{0}", profile.ModelName ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("ModelNumber:{0}", profile.ModelNumber ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "ModelNumber:{0}", profile.ModelNumber ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("ModelUrl:{0}", profile.ModelUrl ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "ModelUrl:{0}", profile.ModelUrl ?? string.Empty).AppendLine();
builder.AppendLine(string.Format("SerialNumber:{0}", profile.SerialNumber ?? string.Empty)); builder.AppendFormat(CultureInfo.InvariantCulture, "SerialNumber:{0}", profile.SerialNumber ?? string.Empty).AppendLine();
_logger.LogInformation(builder.ToString()); _logger.LogInformation(builder.ToString());
} }

View File

@ -194,7 +194,8 @@ namespace MediaBrowser.Api.Playback.Hls
var paddedBitrate = Convert.ToInt32(bitrate * 1.15); var paddedBitrate = Convert.ToInt32(bitrate * 1.15);
// Main stream // Main stream
builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + paddedBitrate.ToString(CultureInfo.InvariantCulture)); builder.Append("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=")
.AppendLine(paddedBitrate.ToString(CultureInfo.InvariantCulture));
var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8"); var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8");
builder.AppendLine(playlistUrl); builder.AppendLine(playlistUrl);

View File

@ -968,7 +968,8 @@ namespace MediaBrowser.Api.Playback.Hls
builder.AppendLine("#EXTM3U"); builder.AppendLine("#EXTM3U");
builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD"); builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
builder.AppendLine("#EXT-X-VERSION:3"); builder.AppendLine("#EXT-X-VERSION:3");
builder.AppendLine("#EXT-X-TARGETDURATION:" + Math.Ceiling(segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength).ToString(CultureInfo.InvariantCulture)); builder.Append("#EXT-X-TARGETDURATION:")
.AppendLine(Math.Ceiling(segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength).ToString(CultureInfo.InvariantCulture));
builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
var queryStringIndex = Request.RawUrl.IndexOf('?'); var queryStringIndex = Request.RawUrl.IndexOf('?');
@ -983,14 +984,17 @@ namespace MediaBrowser.Api.Playback.Hls
foreach (var length in segmentLengths) foreach (var length in segmentLengths)
{ {
builder.AppendLine("#EXTINF:" + length.ToString("0.0000", CultureInfo.InvariantCulture) + ", nodesc"); builder.Append("#EXTINF:")
.Append(length.ToString("0.0000", CultureInfo.InvariantCulture))
builder.AppendLine(string.Format("hls1/{0}/{1}{2}{3}", .AppendLine(", nodesc");
builder.AppendFormat(
CultureInfo.InvariantCulture,
"hls1/{0}/{1}{2}{3}",
name, name,
index.ToString(CultureInfo.InvariantCulture), index.ToString(CultureInfo.InvariantCulture),
GetSegmentFileExtension(request), GetSegmentFileExtension(request),
queryString)); queryString).AppendLine();
index++; index++;
} }

View File

@ -175,11 +175,12 @@ namespace MediaBrowser.Api.Subtitles
throw new ArgumentException("segmentLength was not given, or it was given incorrectly. (It should be bigger than 0)"); throw new ArgumentException("segmentLength was not given, or it was given incorrectly. (It should be bigger than 0)");
} }
builder.AppendLine("#EXTM3U"); builder.AppendLine("#EXTM3U")
builder.AppendLine("#EXT-X-TARGETDURATION:" + request.SegmentLength.ToString(CultureInfo.InvariantCulture)); .Append("#EXT-X-TARGETDURATION:")
builder.AppendLine("#EXT-X-VERSION:3"); .AppendLine(request.SegmentLength.ToString(CultureInfo.InvariantCulture))
builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); .AppendLine("#EXT-X-VERSION:3")
builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD"); .AppendLine("#EXT-X-MEDIA-SEQUENCE:0")
.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD");
long positionTicks = 0; long positionTicks = 0;
@ -190,7 +191,9 @@ namespace MediaBrowser.Api.Subtitles
var remaining = runtime - positionTicks; var remaining = runtime - positionTicks;
var lengthTicks = Math.Min(remaining, segmentLengthTicks); var lengthTicks = Math.Min(remaining, segmentLengthTicks);
builder.AppendLine("#EXTINF:" + TimeSpan.FromTicks(lengthTicks).TotalSeconds.ToString(CultureInfo.InvariantCulture) + ","); builder.Append("#EXTINF:")
.Append(TimeSpan.FromTicks(lengthTicks).TotalSeconds.ToString(CultureInfo.InvariantCulture))
.AppendLine(",");
var endPositionTicks = Math.Min(runtime, positionTicks + segmentLengthTicks); var endPositionTicks = Math.Min(runtime, positionTicks + segmentLengthTicks);

View File

@ -1367,7 +1367,9 @@ namespace MediaBrowser.MediaEncoding.Probing
// OR -> COMMENT. SUBTITLE: DESCRIPTION // OR -> COMMENT. SUBTITLE: DESCRIPTION
// e.g. -> 4/13. The Doctor's Wife: Science fiction drama. When he follows a Time Lord distress signal, the Doctor puts Amy, Rory and his beloved TARDIS in grave danger. Also in HD. [AD,S] // e.g. -> 4/13. The Doctor's Wife: Science fiction drama. When he follows a Time Lord distress signal, the Doctor puts Amy, Rory and his beloved TARDIS in grave danger. Also in HD. [AD,S]
// e.g. -> CBeebies Bedtime Hour. The Mystery: Animated adventures of two friends who live on an island in the middle of the big city. Some of Abney and Teal's favourite objects are missing. [S] // e.g. -> CBeebies Bedtime Hour. The Mystery: Animated adventures of two friends who live on an island in the middle of the big city. Some of Abney and Teal's favourite objects are missing. [S]
if (string.IsNullOrWhiteSpace(subTitle) && !string.IsNullOrWhiteSpace(description) && description.Substring(0, Math.Min(description.Length, MaxSubtitleDescriptionExtractionLength)).Contains(":")) // Check within the Subtitle size limit, otherwise from description it can get too long creating an invalid filename if (string.IsNullOrWhiteSpace(subTitle)
&& !string.IsNullOrWhiteSpace(description)
&& description.AsSpan().Slice(0, Math.Min(description.Length, MaxSubtitleDescriptionExtractionLength)).IndexOf(':') != -1) // Check within the Subtitle size limit, otherwise from description it can get too long creating an invalid filename
{ {
string[] parts = description.Split(':'); string[] parts = description.Split(':');
if (parts.Length > 0) if (parts.Length > 0)
@ -1375,7 +1377,7 @@ namespace MediaBrowser.MediaEncoding.Probing
string subtitle = parts[0]; string subtitle = parts[0];
try try
{ {
if (subtitle.Contains("/")) // It contains a episode number and season number if (subtitle.Contains('/', StringComparison.Ordinal)) // It contains a episode number and season number
{ {
string[] numbers = subtitle.Split(' '); string[] numbers = subtitle.Split(' ');
video.IndexNumber = int.Parse(numbers[0].Replace(".", "").Split('/')[0]); video.IndexNumber = int.Parse(numbers[0].Replace(".", "").Split('/')[0]);
@ -1390,8 +1392,11 @@ namespace MediaBrowser.MediaEncoding.Probing
} }
catch // Default parsing catch // Default parsing
{ {
if (subtitle.Contains(".")) // skip the comment, keep the subtitle if (subtitle.Contains('.', StringComparison.Ordinal))
{
// skip the comment, keep the subtitle
description = string.Join(".", subtitle.Split('.'), 1, subtitle.Split('.').Length - 1).Trim(); // skip the first description = string.Join(".", subtitle.Split('.'), 1, subtitle.Split('.').Length - 1).Trim(); // skip the first
}
else else
{ {
description = subtitle.Trim(); // Clean up whitespaces and save it description = subtitle.Trim(); // Clean up whitespaces and save it

View File

@ -731,19 +731,19 @@ namespace MediaBrowser.MediaEncoding.Subtitles
var date = _fileSystem.GetLastWriteTimeUtc(mediaPath); var date = _fileSystem.GetLastWriteTimeUtc(mediaPath);
var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam).GetMD5() + outputSubtitleExtension; ReadOnlySpan<char> filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam).GetMD5() + outputSubtitleExtension;
var prefix = filename.Substring(0, 1); var prefix = filename.Slice(0, 1);
return Path.Combine(SubtitleCachePath, prefix, filename); return Path.Join(SubtitleCachePath, prefix, filename);
} }
else else
{ {
var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5() + outputSubtitleExtension; ReadOnlySpan<char> filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5() + outputSubtitleExtension;
var prefix = filename.Substring(0, 1); var prefix = filename.Slice(0, 1);
return Path.Combine(SubtitleCachePath, prefix, filename); return Path.Join(SubtitleCachePath, prefix, filename);
} }
} }

View File

@ -93,7 +93,7 @@ namespace MediaBrowser.Providers.MediaInfo
private string GetAudioImagePath(Audio item) private string GetAudioImagePath(Audio item)
{ {
string filename = null; string filename;
if (item.GetType() == typeof(Audio)) if (item.GetType() == typeof(Audio))
{ {
@ -116,9 +116,9 @@ namespace MediaBrowser.Providers.MediaInfo
filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg"; filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg";
} }
var prefix = filename.Substring(0, 1); var prefix = filename.AsSpan().Slice(0, 1);
return Path.Combine(AudioImagesPath, prefix, filename); return Path.Join(AudioImagesPath, prefix, filename);
} }
public string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images"); public string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");

View File

@ -170,7 +170,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
item.SetProviderId(MetadataProvider.Imdb, result.imdbID); item.SetProviderId(MetadataProvider.Imdb, result.imdbID);
if (result.Year.Length > 0 if (result.Year.Length > 0
&& int.TryParse(result.Year.Substring(0, Math.Min(result.Year.Length, 4)), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedYear)) && int.TryParse(result.Year.AsSpan().Slice(0, Math.Min(result.Year.Length, 4)), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedYear))
{ {
item.ProductionYear = parsedYear; item.ProductionYear = parsedYear;
} }

View File

@ -62,7 +62,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
} }
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4 if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
&& int.TryParse(result.Year.Substring(0, 4), NumberStyles.Number, _usCulture, out var year) && int.TryParse(result.Year.AsSpan().Slice(0, 4), NumberStyles.Number, _usCulture, out var year)
&& year >= 0) && year >= 0)
{ {
item.ProductionYear = year; item.ProductionYear = year;
@ -163,7 +163,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
} }
if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4 if (!string.IsNullOrEmpty(result.Year) && result.Year.Length >= 4
&& int.TryParse(result.Year.Substring(0, 4), NumberStyles.Number, _usCulture, out var year) && int.TryParse(result.Year.AsSpan().Slice(0, 4), NumberStyles.Number, _usCulture, out var year)
&& year >= 0) && year >= 0)
{ {
item.ProductionYear = year; item.ProductionYear = year;

View File

@ -188,7 +188,7 @@ namespace MediaBrowser.Providers.Plugins.TheTvdb
for (var i = 0; i < episode.GuestStars.Length; ++i) for (var i = 0; i < episode.GuestStars.Length; ++i)
{ {
var currentActor = episode.GuestStars[i]; var currentActor = episode.GuestStars[i];
var roleStartIndex = currentActor.IndexOf('('); var roleStartIndex = currentActor.IndexOf('(', StringComparison.Ordinal);
if (roleStartIndex == -1) if (roleStartIndex == -1)
{ {
@ -207,7 +207,7 @@ namespace MediaBrowser.Providers.Plugins.TheTvdb
for (var j = i + 1; j < episode.GuestStars.Length; ++j) for (var j = i + 1; j < episode.GuestStars.Length; ++j)
{ {
var currentRole = episode.GuestStars[j]; var currentRole = episode.GuestStars[j];
var roleEndIndex = currentRole.IndexOf(')'); var roleEndIndex = currentRole.IndexOf(')', StringComparison.Ordinal);
if (roleEndIndex == -1) if (roleEndIndex == -1)
{ {

View File

@ -251,9 +251,9 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.People
private static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId) private static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId)
{ {
var letter = tmdbId.GetMD5().ToString().Substring(0, 1); var letter = tmdbId.GetMD5().ToString().AsSpan().Slice(0, 1);
return Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId); return Path.Join(GetPersonsDataPath(appPaths), letter, tmdbId);
} }
internal static string GetPersonDataFilePath(IApplicationPaths appPaths, string tmdbId) internal static string GetPersonDataFilePath(IApplicationPaths appPaths, string tmdbId)

View File

@ -222,8 +222,16 @@ namespace MediaBrowser.XbmcMetadata.Parsers
if (index != -1) if (index != -1)
{ {
var tmdbId = xml.Substring(index + srch.Length).TrimEnd('/').Split('-')[0]; var tmdbId = xml.AsSpan().Slice(index + srch.Length).TrimEnd('/');
if (!string.IsNullOrWhiteSpace(tmdbId) && int.TryParse(tmdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value)) index = tmdbId.IndexOf('-');
if (index != -1)
{
tmdbId = tmdbId.Slice(0, index);
}
if (!tmdbId.IsEmpty
&& !tmdbId.IsWhiteSpace()
&& int.TryParse(tmdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
{ {
item.SetProviderId(MetadataProvider.Tmdb, value.ToString(UsCulture)); item.SetProviderId(MetadataProvider.Tmdb, value.ToString(UsCulture));
} }
@ -237,8 +245,10 @@ namespace MediaBrowser.XbmcMetadata.Parsers
if (index != -1) if (index != -1)
{ {
var tvdbId = xml.Substring(index + srch.Length).TrimEnd('/'); var tvdbId = xml.AsSpan().Slice(index + srch.Length).TrimEnd('/');
if (!string.IsNullOrWhiteSpace(tvdbId) && int.TryParse(tvdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value)) if (!tvdbId.IsEmpty
&& !tvdbId.IsWhiteSpace()
&& int.TryParse(tvdbId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
{ {
item.SetProviderId(MetadataProvider.Tvdb, value.ToString(UsCulture)); item.SetProviderId(MetadataProvider.Tvdb, value.ToString(UsCulture));
} }
@ -442,8 +452,8 @@ namespace MediaBrowser.XbmcMetadata.Parsers
{ {
var val = reader.ReadElementContentAsString(); var val = reader.ReadElementContentAsString();
var hasAspectRatio = item as IHasAspectRatio; if (!string.IsNullOrWhiteSpace(val)
if (!string.IsNullOrWhiteSpace(val) && hasAspectRatio != null) && item is IHasAspectRatio hasAspectRatio)
{ {
hasAspectRatio.AspectRatio = val; hasAspectRatio.AspectRatio = val;
} }