Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Jan 28, 2024
1 parent afbfa1b commit f787796
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion JL.Core/Network/WebSocketUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public static class WebSocketUtils
{
private static Task? s_webSocketTask = null;
private static CancellationTokenSource? s_webSocketCancellationTokenSource = null;

private static readonly Encoding s_utf8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: false)
{
EncoderFallback = new EncoderReplacementFallback("\uFFFE"),
DecoderFallback = new DecoderReplacementFallback("\uFFFE")
};

public static bool Connected => !s_webSocketTask?.IsCompleted ?? false;

public static void HandleWebSocket()
Expand Down Expand Up @@ -66,7 +73,7 @@ private static void ListenWebSocket(CancellationToken cancellationToken)

_ = memoryStream.Seek(0, SeekOrigin.Begin);

string text = Encoding.UTF8.GetString(memoryStream.ToArray());
string text = s_utf8NoBom.GetString(memoryStream.ToArray());
_ = Task.Run(() => Utils.Frontend.CopyFromWebSocket(text), cancellationToken).ConfigureAwait(false);
}
}
Expand Down
4 changes: 2 additions & 2 deletions JL.Core/Utilities/TextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private static int FirstInvalidUnicodeSequenceIndex(string text)
{
char c = text[i];

if (c >= '\ud800')
if (c >= '\uD800')
{
if (c is '\uFFFE' || char.IsLowSurrogate(c))
{
Expand Down Expand Up @@ -39,7 +39,7 @@ private static string RemoveInvalidUnicodeSequences(string text, int index)
{
char c = text[i];

if (c < '\ud800')
if (c < '\uD800')
{
_ = sb.Append(c);
}
Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/GUI/AddNameWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)

string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath);
string line = string.Create(CultureInfo.InvariantCulture, $"{spelling}\t{reading}\t{nameType}\t{extraInfo}\n");
await File.AppendAllTextAsync(path, line, Encoding.UTF8).ConfigureAwait(false);
await File.AppendAllTextAsync(path, line).ConfigureAwait(false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/GUI/AddWordWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)
: string.Create(CultureInfo.InvariantCulture, $"{spellings}\t{readings}\t{definitions}\t{rawPartOfSpeech}\t{wordClasses}\n");

string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath);
await File.AppendAllTextAsync(path, line, Encoding.UTF8).ConfigureAwait(false);
await File.AppendAllTextAsync(path, line).ConfigureAwait(false);
}
}

Expand Down

0 comments on commit f787796

Please sign in to comment.