mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-02 08:23:04 +00:00
Review usage of string.Substring (part 1)
Reduced allocations by replacing string.Substring with ReadOnlySpan<char>.Slice
This commit is contained in:
@@ -35,7 +35,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.StartsWith("["))
|
||||
if (line[0] == '[')
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
return trackInfo;
|
||||
}
|
||||
|
||||
long GetTicks(string time)
|
||||
private long GetTicks(ReadOnlySpan<char> time)
|
||||
{
|
||||
return TimeSpan.TryParseExact(time, @"h\:mm\:ss\.ff", _usCulture, out var span)
|
||||
? span.Ticks : 0;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#nullable enable
|
||||
#pragma warning disable CS1591
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
public class ParserValues
|
||||
public static class ParserValues
|
||||
{
|
||||
public const string NewLine = "\r\n";
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
@@ -20,6 +22,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
var trackInfo = new SubtitleTrackInfo();
|
||||
@@ -55,11 +58,11 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
}
|
||||
|
||||
subEvent.StartPositionTicks = GetTicks(time[0]);
|
||||
var endTime = time[1];
|
||||
var idx = endTime.IndexOf(" ", StringComparison.Ordinal);
|
||||
var endTime = time[1].AsSpan();
|
||||
var idx = endTime.IndexOf(' ');
|
||||
if (idx > 0)
|
||||
{
|
||||
endTime = endTime.Substring(0, idx);
|
||||
endTime = endTime.Slice(0, idx);
|
||||
}
|
||||
|
||||
subEvent.EndPositionTicks = GetTicks(endTime);
|
||||
@@ -88,7 +91,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
return trackInfo;
|
||||
}
|
||||
|
||||
long GetTicks(string time)
|
||||
private long GetTicks(ReadOnlySpan<char> time)
|
||||
{
|
||||
return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out var span)
|
||||
? span.Ticks
|
||||
|
||||
@@ -8,8 +8,12 @@ using MediaBrowser.Model.MediaInfo;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// SRT subtitle writer.
|
||||
/// </summary>
|
||||
public class SrtWriter : ISubtitleWriter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
/// </summary>
|
||||
public class SsaParser : ISubtitleParser
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
var trackInfo = new SubtitleTrackInfo();
|
||||
@@ -45,7 +46,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
header.AppendLine(line);
|
||||
}
|
||||
|
||||
if (line.Trim().ToLowerInvariant() == "[events]")
|
||||
if (string.Equals(line.Trim(), "[events]", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
eventsStarted = true;
|
||||
}
|
||||
@@ -63,27 +64,27 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
format = line.ToLowerInvariant().Substring(8).Split(',');
|
||||
for (int i = 0; i < format.Length; i++)
|
||||
{
|
||||
if (format[i].Trim().ToLowerInvariant() == "layer")
|
||||
if (string.Equals(format[i].Trim(), "layer", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexLayer = i;
|
||||
}
|
||||
else if (format[i].Trim().ToLowerInvariant() == "start")
|
||||
else if (string.Equals(format[i].Trim(), "start", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexStart = i;
|
||||
}
|
||||
else if (format[i].Trim().ToLowerInvariant() == "end")
|
||||
else if (string.Equals(format[i].Trim(), "end", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexEnd = i;
|
||||
}
|
||||
else if (format[i].Trim().ToLowerInvariant() == "text")
|
||||
else if (string.Equals(format[i].Trim(), "text", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexText = i;
|
||||
}
|
||||
else if (format[i].Trim().ToLowerInvariant() == "effect")
|
||||
else if (string.Equals(format[i].Trim(), "effect", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexEffect = i;
|
||||
}
|
||||
else if (format[i].Trim().ToLowerInvariant() == "style")
|
||||
else if (string.Equals(format[i].Trim(), "style", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
indexStyle = i;
|
||||
}
|
||||
@@ -184,12 +185,10 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
int.Parse(timeCode[3]) * 10).Ticks;
|
||||
}
|
||||
|
||||
public static string GetFormattedText(string text)
|
||||
private static string GetFormattedText(string text)
|
||||
{
|
||||
text = text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
bool italic = false;
|
||||
|
||||
for (int i = 0; i < 10; i++) // just look ten times...
|
||||
{
|
||||
if (text.Contains(@"{\fn"))
|
||||
@@ -200,7 +199,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
string fontName = text.Substring(start + 4, end - (start + 4));
|
||||
string extraTags = string.Empty;
|
||||
CheckAndAddSubTags(ref fontName, ref extraTags, out italic);
|
||||
CheckAndAddSubTags(ref fontName, ref extraTags, out bool italic);
|
||||
text = text.Remove(start, end - start + 1);
|
||||
if (italic)
|
||||
{
|
||||
@@ -231,7 +230,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
string fontSize = text.Substring(start + 4, end - (start + 4));
|
||||
string extraTags = string.Empty;
|
||||
CheckAndAddSubTags(ref fontSize, ref extraTags, out italic);
|
||||
CheckAndAddSubTags(ref fontSize, ref extraTags, out bool italic);
|
||||
if (IsInteger(fontSize))
|
||||
{
|
||||
text = text.Remove(start, end - start + 1);
|
||||
@@ -265,7 +264,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
string color = text.Substring(start + 4, end - (start + 4));
|
||||
string extraTags = string.Empty;
|
||||
CheckAndAddSubTags(ref color, ref extraTags, out italic);
|
||||
CheckAndAddSubTags(ref color, ref extraTags, out bool italic);
|
||||
|
||||
color = color.Replace("&", string.Empty).TrimStart('H');
|
||||
color = color.PadLeft(6, '0');
|
||||
@@ -303,7 +302,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
string color = text.Substring(start + 5, end - (start + 5));
|
||||
string extraTags = string.Empty;
|
||||
CheckAndAddSubTags(ref color, ref extraTags, out italic);
|
||||
CheckAndAddSubTags(ref color, ref extraTags, out bool italic);
|
||||
|
||||
color = color.Replace("&", string.Empty).TrimStart('H');
|
||||
color = color.PadLeft(6, '0');
|
||||
@@ -354,14 +353,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
}
|
||||
|
||||
private static bool IsInteger(string s)
|
||||
{
|
||||
if (int.TryParse(s, out var i))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
=> int.TryParse(s, out _);
|
||||
|
||||
private static int CountTagInText(string text, string tag)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
@@ -365,7 +367,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
/// <returns>System.Object.</returns>
|
||||
private SemaphoreSlim GetLock(string filename)
|
||||
{
|
||||
return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
||||
return _semaphoreLocks.GetOrAdd(filename, _ => new SemaphoreSlim(1, 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -6,8 +6,12 @@ using MediaBrowser.Model.MediaInfo;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// TTML subtitle writer.
|
||||
/// </summary>
|
||||
public class TtmlWriter : ISubtitleWriter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
// Example: https://github.com/zmalltalker/ttml2vtt/blob/master/data/sample.xml
|
||||
@@ -36,9 +40,10 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
|
||||
text = Regex.Replace(text, @"\\n", "<br/>", RegexOptions.IgnoreCase);
|
||||
|
||||
writer.WriteLine("<p begin=\"{0}\" dur=\"{1}\">{2}</p>",
|
||||
writer.WriteLine(
|
||||
"<p begin=\"{0}\" dur=\"{1}\">{2}</p>",
|
||||
trackEvent.StartPositionTicks,
|
||||
(trackEvent.EndPositionTicks - trackEvent.StartPositionTicks),
|
||||
trackEvent.EndPositionTicks - trackEvent.StartPositionTicks,
|
||||
text);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase);
|
||||
|
||||
writer.WriteLine(text);
|
||||
writer.WriteLine(string.Empty);
|
||||
writer.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user