Minor fixes for json

This commit is contained in:
Nikolaj Olsson 2016-02-14 12:15:48 +01:00
parent d490604d33
commit 8fcf78f908
4 changed files with 10 additions and 6 deletions

View File

@ -100,7 +100,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var sb = new StringBuilder();
foreach (string s in lines)
sb.Append(s);
if (!sb.ToString().TrimStart().StartsWith("[{\"start"))
if (!sb.ToString().TrimStart().StartsWith("[{\"", StringComparison.Ordinal))
return;
foreach (string line in sb.ToString().Replace("},{", Environment.NewLine).SplitToLines())
@ -145,6 +145,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
return s;
}
private static readonly char[] CommaAndEndCurlyBracket = { ',', '}' };
public static string ReadTag(string s, string tag)
{
var startIndex = s.IndexOfAny(new[] { "\"" + tag + "\"", "'" + tag + "'" }, StringComparison.Ordinal);
@ -153,7 +155,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var res = s.Substring(startIndex + 3 + tag.Length).Trim().TrimStart(':').TrimStart();
if (res.StartsWith('"'))
{ // text
res = Json.ConvertJsonSpecialCharacters(res);
res = ConvertJsonSpecialCharacters(res);
res = res.Replace("\\\"", "@__1");
int endIndex = res.IndexOf("\"}", StringComparison.Ordinal);
int endAlternate = res.IndexOf("\",", StringComparison.Ordinal);
@ -169,7 +171,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else
{ // number
var endIndex = res.IndexOfAny(new[] { ',', '}' });
var endIndex = res.IndexOfAny(CommaAndEndCurlyBracket);
if (endIndex < 0)
return null;
return res.Substring(0, endIndex);

View File

@ -56,7 +56,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var sb = new StringBuilder();
foreach (string s in lines)
sb.Append(s);
if (!sb.ToString().TrimStart().StartsWith("[{\"startMillis"))
if (!sb.ToString().TrimStart().StartsWith("[{\"", StringComparison.Ordinal))
return;
foreach (string line in sb.ToString().Replace("},{", Environment.NewLine).SplitToLines())

View File

@ -57,7 +57,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var sb = new StringBuilder();
foreach (string s in lines)
sb.Append(s);
int startIndex = sb.ToString().IndexOf("[{\"duration", StringComparison.Ordinal);
int startIndex = sb.ToString().IndexOf("[{\"", StringComparison.Ordinal);
if (startIndex < 0)
return;

View File

@ -28,6 +28,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
return subtitle.Paragraphs.Count > _errorCount;
}
private static readonly char[] CharSpace = { ' ' };
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
@ -38,7 +40,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
//split words
string text = p.Text.Replace(Environment.NewLine, " ").Replace(" ", " ");
var words = text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var words = text.Split(CharSpace, StringSplitOptions.RemoveEmptyEntries);
var times = GenerateTimes(words, text, p.StartTime, p.EndTime);
for (int j = 0; j < words.Length; j++)
{