[gnome-subtitles] Don't write UTF-8 files with BOM
- From: Pedro Castro <pcastro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-subtitles] Don't write UTF-8 files with BOM
- Date: Sun, 28 Oct 2018 17:52:44 +0000 (UTC)
commit f117bf70a600c1074833aab97a5a2c807786f79e
Author: Pedro Castro <pedro gnomesubtitles org>
Date: Sun Oct 28 17:51:29 2018 +0000
Don't write UTF-8 files with BOM
Fixes #113 - Don't insert the BOM (Byte order mark) when saving files
with UTF-8 encoding
src/GnomeSubtitles/Core/Document.cs | 6 +++---
src/GnomeSubtitles/Core/Encodings.cs | 25 ++++++++++++++++++++-----
src/GnomeSubtitles/Ui/MainUi.cs | 6 +++---
3 files changed, 26 insertions(+), 11 deletions(-)
---
diff --git a/src/GnomeSubtitles/Core/Document.cs b/src/GnomeSubtitles/Core/Document.cs
index 2819e1c..edeee5f 100644
--- a/src/GnomeSubtitles/Core/Document.cs
+++ b/src/GnomeSubtitles/Core/Document.cs
@@ -1,6 +1,6 @@
/*
* This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2017 Pedro Castro
+ * Copyright (C) 2006-2018 Pedro Castro
*
* Gnome Subtitles is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -262,12 +262,12 @@ public class Document {
private Encoding GetFallbackEncoding () {
ConfigFileOpenFallbackEncoding fallbackEncodingConfig = Base.Config.FileOpenFallbackEncoding;
if (fallbackEncodingConfig == ConfigFileOpenFallbackEncoding.CurrentLocale)
- return Encoding.GetEncoding(Encodings.SystemDefault.CodePage);
+ return Encodings.GetEncoding(Encodings.SystemDefault.CodePage);
else {
string encodingName = Base.Config.FileOpenFallbackEncodingFixed;
EncodingDescription encodingDescription = EncodingDescription.Empty;
Encodings.Find(encodingName, ref encodingDescription);
- return Encoding.GetEncoding(encodingDescription.CodePage);
+ return Encodings.GetEncoding(encodingDescription.CodePage);
}
}
diff --git a/src/GnomeSubtitles/Core/Encodings.cs b/src/GnomeSubtitles/Core/Encodings.cs
index fc6be9c..c8b766a 100644
--- a/src/GnomeSubtitles/Core/Encodings.cs
+++ b/src/GnomeSubtitles/Core/Encodings.cs
@@ -1,6 +1,6 @@
/*
* This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2010 Pedro Castro
+ * Copyright (C) 2007-2018 Pedro Castro
*
* Gnome Subtitles is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -25,6 +25,10 @@ namespace GnomeSubtitles.Core {
public class Encodings {
+ /* UTF8 encoding is constructed in a different manner in order to disable the BOM. */
+ private static int CODEPAGE_UTF8 = 65001;
+ private static Encoding encodingUTF8 = null;
+
/* The original versions of the following tables were taken from gedit
* which on the other hand took them from profterm
* Copyright (C) 2002 Red Hat, Inc.
@@ -46,12 +50,12 @@ public class Encodings {
new EncodingDescription(28606, "ISO-8859-16", Catalog.GetString("Romanian")),
/* ISO-8859-8-I not used */
- new EncodingDescription(65001, "UTF-8", Catalog.GetString("Unicode")), /* Added */
+ new EncodingDescription(CODEPAGE_UTF8, "UTF-8", Catalog.GetString("Unicode")), /* Added */
new EncodingDescription(65000, "UTF-7", Catalog.GetString("Unicode")),
- new EncodingDescription(1200, "UTF-16", Catalog.GetString("Unicode")),
+ new EncodingDescription(1200, "UTF-16", Catalog.GetString("Unicode")), //Little endian
new EncodingDescription(1201, "UTF-16BE", Catalog.GetString("Unicode")),
new EncodingDescription(1200, "UTF-16LE", Catalog.GetString("Unicode")),
- new EncodingDescription(12000, "UTF-32", Catalog.GetString("Unicode")),
+ new EncodingDescription(12000, "UTF-32", Catalog.GetString("Unicode")), //Little endian
new EncodingDescription(12001, "UTF-32BE", Catalog.GetString("Unicode")), /* Added */
new EncodingDescription(12000, "UTF-32LE", Catalog.GetString("Unicode")), /* Added */
/* UCS-2 and UCS-4 not used */
@@ -163,7 +167,7 @@ public class Encodings {
return desc.Name;
try {
- Encoding encoding = Encoding.GetEncoding(codePage);
+ Encoding encoding = GetEncoding(codePage);
if (encoding != null)
return encoding.WebName;
}
@@ -172,6 +176,17 @@ public class Encodings {
}
return null;
}
+
+ public static Encoding GetEncoding (int codePage) {
+ if (codePage == CODEPAGE_UTF8) {
+ if (encodingUTF8 == null) {
+ encodingUTF8 = new UTF8Encoding(false);
+ }
+ return encodingUTF8;
+ }
+
+ return Encoding.GetEncoding(codePage);
+ }
}
diff --git a/src/GnomeSubtitles/Ui/MainUi.cs b/src/GnomeSubtitles/Ui/MainUi.cs
index 6bdce5a..e09563e 100644
--- a/src/GnomeSubtitles/Ui/MainUi.cs
+++ b/src/GnomeSubtitles/Ui/MainUi.cs
@@ -299,7 +299,7 @@ public class MainUi {
return null;
try {
- return Encoding.GetEncoding(codePage);
+ return Encodings.GetEncoding(codePage);
}
catch (NotSupportedException) {
throw new EncodingNotSupportedException();
@@ -312,7 +312,7 @@ public class MainUi {
/// <remarks>An error dialog is presented if an exception is caught during open.</remarks>
private void OpenTranslation (string path, int codePage) {
try {
- Encoding encoding = (codePage == -1 ? null : Encoding.GetEncoding(codePage));
+ Encoding encoding = (codePage == -1 ? null : Encodings.GetEncoding(codePage));
Base.OpenTranslation(path, encoding);
}
catch (Exception exception) {
@@ -362,7 +362,7 @@ public class MainUi {
return null;
string path = dialog.Filename;
- Encoding encoding = Encoding.GetEncoding(dialog.Encoding.CodePage);
+ Encoding encoding = Encodings.GetEncoding(dialog.Encoding.CodePage);
SubtitleType subtitleType = dialog.SubtitleType;
NewlineType newlineType = dialog.NewlineType;
TimingMode timingMode = Base.TimingMode;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]