Skip to content

Commit c10863f

Browse files
committed
Replace Memory<char> with string
1 parent 9260be9 commit c10863f

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,12 @@ public void Dispose()
524524
private void ProcessComMarker(BufferedReadStream stream, int markerContentByteSize)
525525
{
526526
Span<byte> temp = new byte[markerContentByteSize];
527-
char[] chars = new char[markerContentByteSize];
528527
JpegMetadata metadata = this.Metadata.GetFormatMetadata(JpegFormat.Instance);
529528

530529
stream.Read(temp);
531-
Encoding.ASCII.GetChars(temp, chars);
530+
string comment = Encoding.ASCII.GetString(temp);
532531

533-
metadata.Comments.Add(chars);
532+
metadata.Comments.Add(comment);
534533
}
535534

536535
/// <summary>

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,23 @@ private void WriteComment(JpegMetadata metadata)
186186

187187
for (int i = 0; i < metadata.Comments.Count; i++)
188188
{
189-
Memory<char> chars = metadata.Comments[i];
189+
string comment = metadata.Comments[i];
190190

191-
if (chars.Length > maxCommentLength)
191+
if (comment.Length > maxCommentLength)
192192
{
193-
Memory<char> splitComment = chars.Slice(maxCommentLength, chars.Length - maxCommentLength);
193+
string splitComment = comment.Substring(maxCommentLength, comment.Length - maxCommentLength);
194194
metadata.Comments.Insert(i + 1, splitComment);
195195

196196
// We don't want to keep the extra bytes
197-
chars = chars.Slice(0, maxCommentLength);
197+
comment = comment.Substring(0, maxCommentLength);
198198
}
199199

200-
int commentLength = chars.Length + 4;
200+
int commentLength = comment.Length + 4;
201201

202-
Span<byte> comment = new byte[commentLength];
203-
Span<byte> markers = comment.Slice(0, 2);
204-
Span<byte> payloadSize = comment.Slice(2, 2);
205-
Span<byte> payload = comment.Slice(4, chars.Length);
202+
Span<byte> commentSpan = new byte[commentLength];
203+
Span<byte> markers = commentSpan.Slice(0, 2);
204+
Span<byte> payloadSize = commentSpan.Slice(2, 2);
205+
Span<byte> payload = commentSpan.Slice(4, comment.Length);
206206

207207
// Beginning of comment ff fe
208208
markers[0] = JpegConstants.Markers.XFF;
@@ -213,9 +213,9 @@ private void WriteComment(JpegMetadata metadata)
213213
payloadSize[0] = (byte)((comWithoutMarker >> 8) & 0xFF);
214214
payloadSize[1] = (byte)(comWithoutMarker & 0xFF);
215215

216-
Encoding.ASCII.GetBytes(chars.Span, payload);
216+
Encoding.ASCII.GetBytes(comment, payload);
217217

218-
this.outputStream.Write(comment, 0, comment.Length);
218+
this.outputStream.Write(commentSpan, 0, commentSpan.Length);
219219
}
220220
}
221221

src/ImageSharp/Formats/Jpeg/JpegMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class JpegMetadata : IDeepCloneable
1515
/// </summary>
1616
public JpegMetadata()
1717
{
18-
this.Comments = new List<Memory<char>>();
18+
this.Comments = new List<string>();
1919
}
2020

2121
/// <summary>
@@ -106,7 +106,7 @@ public int Quality
106106
/// <summary>
107107
/// Gets the comments.
108108
/// </summary>
109-
public IList<Memory<char>> Comments { get; }
109+
public IList<string> Comments { get; }
110110

111111
/// <inheritdoc/>
112112
public IDeepCloneable DeepClone() => new JpegMetadata(this);

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public void JpegDecoder_DecodeMetadataComment<TPixel>(TestImageProvider<TPixel>
435435
JpegMetadata metadata = image.Metadata.GetJpegMetadata();
436436

437437
Assert.Equal(1, metadata.Comments.Count);
438-
Assert.Equal(expectedComment.ToCharArray(), metadata.Comments.ElementAtOrDefault(0));
438+
Assert.Equal(expectedComment, metadata.Comments.ElementAtOrDefault(0));
439439
image.DebugSave(provider);
440440
image.CompareToOriginal(provider);
441441
}

tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void Encode_PreservesComments<TPixel>(TestImageProvider<TPixel> provider)
172172
JpegMetadata actual = output.Metadata.GetJpegMetadata();
173173
Assert.NotEmpty(actual.Comments);
174174
Assert.Equal(1, actual.Comments.Count);
175-
Assert.Equal("TEST COMMENT", actual.Comments.ElementAtOrDefault(0).ToString());
175+
Assert.Equal("TEST COMMENT", actual.Comments.ElementAtOrDefault(0));
176176
}
177177

178178
[Fact]
@@ -184,8 +184,8 @@ public void Encode_SavesMultipleComments()
184184
using var memStream = new MemoryStream();
185185

186186
// act
187-
meta.Comments.Add("First comment".ToCharArray());
188-
meta.Comments.Add("Second Comment".ToCharArray());
187+
meta.Comments.Add("First comment");
188+
meta.Comments.Add("Second Comment");
189189
input.Save(memStream, JpegEncoder);
190190

191191
// assert
@@ -194,8 +194,8 @@ public void Encode_SavesMultipleComments()
194194
JpegMetadata actual = output.Metadata.GetJpegMetadata();
195195
Assert.NotEmpty(actual.Comments);
196196
Assert.Equal(2, actual.Comments.Count);
197-
Assert.Equal(meta.Comments.ElementAtOrDefault(0).ToString(), actual.Comments.ElementAtOrDefault(0).ToString());
198-
Assert.Equal(meta.Comments.ElementAtOrDefault(1).ToString(), actual.Comments.ElementAtOrDefault(1).ToString());
197+
Assert.Equal(meta.Comments.ElementAtOrDefault(0), actual.Comments.ElementAtOrDefault(0));
198+
Assert.Equal(meta.Comments.ElementAtOrDefault(1), actual.Comments.ElementAtOrDefault(1));
199199
}
200200

201201
[Theory]

tests/ImageSharp.Tests/Formats/Jpg/JpegMetadataTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ public void Comment_EmptyComment()
6464
{
6565
var meta = new JpegMetadata();
6666

67-
Assert.True(Array.Empty<Memory<char>>().SequenceEqual(meta.Comments));
67+
Assert.True(Array.Empty<string>().SequenceEqual(meta.Comments));
6868
}
6969

7070
[Fact]
7171
public void Comment_OnlyComment()
7272
{
7373
string comment = "test comment";
74-
var expectedCollection = new Collection<Memory<char>> { new(comment.ToCharArray()) };
74+
var expectedCollection = new Collection<string> { comment };
7575

7676
var meta = new JpegMetadata();
77-
meta.Comments?.Add(comment.ToCharArray());
77+
meta.Comments.Add(comment);
7878

79-
Assert.Equal(1, meta.Comments?.Count);
80-
Assert.True(expectedCollection.FirstOrDefault().ToString() == meta.Comments?.FirstOrDefault().ToString());
79+
Assert.Equal(1, meta.Comments.Count);
80+
Assert.True(expectedCollection.FirstOrDefault() == meta.Comments.FirstOrDefault());
8181
}
8282
}

0 commit comments

Comments
 (0)