From 2e219ed69cba6cd1659f5eb36c13b2cab60a0b0a Mon Sep 17 00:00:00 2001 From: Ildar Khayrutdinov Date: Tue, 18 Jan 2022 11:08:00 +0300 Subject: [PATCH] renaming, cosmetics --- .../Metadata/Profiles/Exif/ExifConstants.cs | 3 ++- .../Metadata/Profiles/Exif/ExifReader.cs | 4 ++-- .../Metadata/Profiles/Exif/ExifWriter.cs | 4 ++-- .../Profiles/Exif/Values/EncodedString.cs | 18 +++++++++--------- .../Profiles/Exif/Values/EncodedStringCode.cs | 6 +++--- .../Profiles/Exif/Values/ExifEncodedString.cs | 14 +++++--------- .../Formats/Jpg/JpegDecoderTests.Metadata.cs | 2 +- .../Profiles/Exif/Values/ExifValuesTests.cs | 2 +- 8 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs index 898a0d6d1e..e6c7ec2473 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifConstants.cs @@ -32,7 +32,8 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif 0x2A }; - public static Encoding DefaultAsciiEncoding => Encoding.UTF8; + // UTF-8 is better than ASCII, UTF-8 encodes the ASCII codes the same way + public static Encoding DefaultEncoding => Encoding.UTF8; public static Encoding JIS0208Encoding => Encoding.GetEncoding(932); diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs index 496035073c..4aff835a9c 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs @@ -252,7 +252,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif buffer = buffer.Slice(0, nullCharIndex); } - return ExifConstants.DefaultAsciiEncoding.GetString(buffer); + return ExifConstants.DefaultEncoding.GetString(buffer); } private object ConvertValue(ExifDataType dataType, ReadOnlySpan buffer, bool isArray) @@ -364,7 +364,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif if (ExifConstants.TryDetect(buffer, out EncodedStringCode code)) { string text = ExifConstants.GetEncoding(code).GetString(buffer.Slice(ExifConstants.CharacterCodeBytesLength)); - return new EncodedString(text, code); + return new EncodedString(code, text); } return buffer.ToArray(); diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs index d6fe6fe5f9..4150a9a50a 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs @@ -276,7 +276,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif if (exifValue.DataType == ExifDataType.Ascii) { - return (uint)ExifConstants.DefaultAsciiEncoding.GetByteCount((string)value) + 1; + return (uint)ExifConstants.DefaultEncoding.GetByteCount((string)value) + 1; } if (value is EncodedString encodedString) @@ -378,7 +378,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif switch (dataType) { case ExifDataType.Ascii: - offset = Write(ExifConstants.DefaultAsciiEncoding.GetBytes((string)value), destination, offset); + offset = Write(ExifConstants.DefaultEncoding.GetBytes((string)value), destination, offset); destination[offset] = 0; return offset + 1; case ExifDataType.Byte: diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedString.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedString.cs index 1169d2397c..9e2e2aa14f 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedString.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedString.cs @@ -13,32 +13,32 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif /// /// Initializes a new instance of the struct. /// - /// The text. + /// The text value. public EncodedString(string text) - : this(text, EncodedStringCode.Unicode) + : this(EncodedStringCode.Unicode, text) { } /// /// Initializes a new instance of the struct. /// - /// The text. - /// The code. - public EncodedString(string text, EncodedStringCode code) + /// The character code. + /// The text value. + public EncodedString(EncodedStringCode code, string text) { this.Text = text; this.Code = code; } /// - /// Gets the text. + /// Gets the character ode. /// - public string Text { get; } + public EncodedStringCode Code { get; } /// - /// Gets the character ode. + /// Gets the text. /// - public EncodedStringCode Code { get; } + public string Text { get; } /// public override bool Equals(object obj) => obj is EncodedString other && this.Equals(other); diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedStringCode.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedStringCode.cs index ee88b4dc09..ce812de84b 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedStringCode.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/EncodedStringCode.cs @@ -4,17 +4,17 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif { /// - /// The 8-byte The character code enum. + /// The 8-byte character code enum. /// public enum EncodedStringCode { /// - /// The ASCII ITU-T T.50 IA5 character code. + /// The ASCII (ITU-T T.50 IA5) character code. /// ASCII, /// - /// The JIS X208-1990 character code. + /// The JIS (X208-1990) character code. /// JIS, diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifEncodedString.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifEncodedString.cs index dc9885d022..4055573c57 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifEncodedString.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifEncodedString.cs @@ -33,17 +33,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return true; } - switch (value) + if (value is string stringValue) { - case string stringValue: - this.Value = new EncodedString(stringValue); - return true; - case byte[] bytes: - this.Value = new EncodedString(ExifConstants.DefaultAsciiEncoding.GetString(bytes)); - return true; - default: - return false; + this.Value = new EncodedString(stringValue); + return true; } + + return false; } public override IExifValue DeepClone() => new ExifEncodedString(this); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs index 32f76be0c9..247dd6441d 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs @@ -301,7 +301,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg exif.SetValue(ExifTag.XPAuthor, Encoding.GetEncoding("UCS-2").GetBytes("Dan Petitt")); exif.SetValue(ExifTag.XPTitle, Encoding.GetEncoding("UCS-2").GetBytes("A bit of test metadata for image title")); - exif.SetValue(ExifTag.UserComment, new EncodedString("A bit of normal comment text", EncodedStringCode.ASCII)); + exif.SetValue(ExifTag.UserComment, new EncodedString(EncodedStringCode.ASCII, "A bit of normal comment text")); exif.SetValue(ExifTag.GPSDateStamp, "2022-01-06"); exif.SetValue(ExifTag.XPKeywords, new byte[] { 0x41, 0x53, 0x43, 0x49, 0x49, 00, 00, 00, 0x41, 0x41, 0x41 }); diff --git a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/Values/ExifValuesTests.cs b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/Values/ExifValuesTests.cs index 032667120a..9cab56c5c3 100644 --- a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/Values/ExifValuesTests.cs +++ b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/Values/ExifValuesTests.cs @@ -602,7 +602,7 @@ namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.Exif.Values [MemberData(nameof(EncodedStringTags))] public void ExifEncodedStringTests(ExifTag tag) { - var expected = new EncodedString("test string", EncodedStringCode.JIS); + var expected = new EncodedString(EncodedStringCode.JIS, "test string"); ExifValue value = ExifValues.Create(tag); Assert.False(value.TrySetValue(123));