From 5068fc65df3c3106f2cca6312805bcc12ddf6a6b Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Mon, 29 Jan 2024 07:56:06 +0100 Subject: [PATCH] Fix json text decode bug - thx darnn :) --- src/Test/Core/JsonTest.cs | 23 +++++++++++++++++++++++ src/Test/Test.csproj | 1 + src/libse/AutoTranslate/MyMemoryApi.cs | 2 +- src/libse/SubtitleFormats/Json.cs | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/Test/Core/JsonTest.cs diff --git a/src/Test/Core/JsonTest.cs b/src/Test/Core/JsonTest.cs new file mode 100644 index 000000000..377043752 --- /dev/null +++ b/src/Test/Core/JsonTest.cs @@ -0,0 +1,23 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nikse.SubtitleEdit.Core.SubtitleFormats; + +namespace Test.Core +{ + [TestClass] + public class JsonTest + { + [TestMethod] + public void TestUnicodeFirst() + { + var result = Json.DecodeJsonText("\u05d1 "); + Assert.AreEqual("ב ", result); + } + + [TestMethod] + public void TestUnicodeLast() + { + var result = Json.DecodeJsonText(" \u05d1"); + Assert.AreEqual(" ב", result); + } + } +} diff --git a/src/Test/Test.csproj b/src/Test/Test.csproj index b472b9172..b5187ba7a 100644 --- a/src/Test/Test.csproj +++ b/src/Test/Test.csproj @@ -64,6 +64,7 @@ + diff --git a/src/libse/AutoTranslate/MyMemoryApi.cs b/src/libse/AutoTranslate/MyMemoryApi.cs index e4534135a..28a4eabe9 100644 --- a/src/libse/AutoTranslate/MyMemoryApi.cs +++ b/src/libse/AutoTranslate/MyMemoryApi.cs @@ -23,7 +23,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate public void Initialize() { _httpClient = DownloaderFactory.MakeHttpClient(); - _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); + _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); _httpClient.BaseAddress = new Uri("https://api.mymemory.translated.net/get"); } diff --git a/src/libse/SubtitleFormats/Json.cs b/src/libse/SubtitleFormats/Json.cs index 06744d9ab..3926e209d 100644 --- a/src/libse/SubtitleFormats/Json.cs +++ b/src/libse/SubtitleFormats/Json.cs @@ -66,7 +66,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats if (c == '\\' && !keepNext) { keepNext = true; - if (i + 6 < text.Length && text[i + 1] == 'u' && + if (i + 5 < text.Length && text[i + 1] == 'u' && hexLetters.Contains(text[i + 2]) && hexLetters.Contains(text[i + 3]) && hexLetters.Contains(text[i + 4]) &&