Skip to content

Commit 24c938a

Browse files
TsudaKageyudecriptor
authored andcommitted
Support Latin-1 (or local encodings) in RIFF INFO tag
Minor logic change, code cleanup, etc - Stephen Shaw @decriptor
1 parent 84f6cd0 commit 24c938a

File tree

3 files changed

+157
-41
lines changed

3 files changed

+157
-41
lines changed

src/TaglibSharp.Tests/TaggingFormats/InfoTagTest.cs

Lines changed: 107 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using NUnit.Framework;
22
using TagLib;
3+
using TagLib.Riff;
34

45
namespace TaglibSharp.Tests.TaggingFormats
56
{
7+
// NOTE: If StringType == UTF8 (default) then don't set it during the test.
8+
// We want to make sure that default still works as expected
69
[TestFixture]
710
public class InfoTagTest
811
{
@@ -15,10 +18,15 @@ public class InfoTagTest
1518
static readonly string[] val_gnre = {"Rap",
1619
"Jazz", "Non-Genre", "Blues"};
1720

18-
[Test]
19-
public void TestTitle ()
21+
[TestCase (StringType.Latin1)]
22+
[TestCase (StringType.UTF8)]
23+
public void TestTitle (StringType stringType)
2024
{
21-
var tag = new TagLib.Riff.InfoTag ();
25+
InfoTag tag;
26+
if (stringType == StringType.UTF8)
27+
tag = new TagLib.Riff.InfoTag ();
28+
else
29+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
2230

2331
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
2432
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -40,10 +48,15 @@ public void TestTitle ()
4048
});
4149
}
4250

43-
[Test]
44-
public void TestDescription ()
51+
[TestCase (StringType.Latin1)]
52+
[TestCase (StringType.UTF8)]
53+
public void TestDescription (StringType stringType)
4554
{
46-
var tag = new TagLib.Riff.InfoTag ();
55+
InfoTag tag;
56+
if (stringType == StringType.UTF8)
57+
tag = new TagLib.Riff.InfoTag ();
58+
else
59+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
4760

4861
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
4962
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -65,10 +78,15 @@ public void TestDescription ()
6578
});
6679
}
6780

68-
[Test]
69-
public void TestAlbum ()
81+
[TestCase (StringType.Latin1)]
82+
[TestCase (StringType.UTF8)]
83+
public void TestAlbum (StringType stringType)
7084
{
71-
var tag = new TagLib.Riff.InfoTag ();
85+
InfoTag tag;
86+
if (stringType == StringType.UTF8)
87+
tag = new TagLib.Riff.InfoTag ();
88+
else
89+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
7290

7391
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
7492
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -91,10 +109,15 @@ public void TestAlbum ()
91109
}
92110

93111

94-
[Test]
95-
public void TestConductor ()
112+
[TestCase (StringType.Latin1)]
113+
[TestCase (StringType.UTF8)]
114+
public void TestConductor (StringType stringType)
96115
{
97-
var tag = new TagLib.Riff.InfoTag ();
116+
InfoTag tag;
117+
if (stringType == StringType.UTF8)
118+
tag = new TagLib.Riff.InfoTag ();
119+
else
120+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
98121

99122
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
100123
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -117,10 +140,15 @@ public void TestConductor ()
117140
}
118141

119142

120-
[Test]
121-
public void TestPerformers ()
143+
[TestCase (StringType.Latin1)]
144+
[TestCase (StringType.UTF8)]
145+
public void TestPerformers (StringType stringType)
122146
{
123-
var tag = new TagLib.Riff.InfoTag ();
147+
InfoTag tag;
148+
if (stringType == StringType.UTF8)
149+
tag = new TagLib.Riff.InfoTag ();
150+
else
151+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
124152

125153
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
126154
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -145,10 +173,15 @@ public void TestPerformers ()
145173
});
146174
}
147175

148-
[Test]
149-
public void TestAlbumArtists ()
176+
[TestCase (StringType.Latin1)]
177+
[TestCase (StringType.UTF8)]
178+
public void TestAlbumArtists (StringType stringType)
150179
{
151-
var tag = new TagLib.Riff.InfoTag ();
180+
InfoTag tag;
181+
if (stringType == StringType.UTF8)
182+
tag = new TagLib.Riff.InfoTag ();
183+
else
184+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
152185

153186
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
154187
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -173,10 +206,15 @@ public void TestAlbumArtists ()
173206
});
174207
}
175208

176-
[Test]
177-
public void TestComposers ()
209+
[TestCase (StringType.Latin1)]
210+
[TestCase (StringType.UTF8)]
211+
public void TestComposers (StringType stringType)
178212
{
179-
var tag = new TagLib.Riff.InfoTag ();
213+
InfoTag tag;
214+
if (stringType == StringType.UTF8)
215+
tag = new TagLib.Riff.InfoTag ();
216+
else
217+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
180218

181219
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
182220
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -201,10 +239,15 @@ public void TestComposers ()
201239
});
202240
}
203241

204-
[Test]
205-
public void TestComment ()
242+
[TestCase (StringType.Latin1)]
243+
[TestCase (StringType.UTF8)]
244+
public void TestComments (StringType stringType)
206245
{
207-
var tag = new TagLib.Riff.InfoTag ();
246+
InfoTag tag;
247+
if (stringType == StringType.UTF8)
248+
tag = new TagLib.Riff.InfoTag ();
249+
else
250+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
208251

209252
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
210253
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -226,10 +269,15 @@ public void TestComment ()
226269
});
227270
}
228271

229-
[Test]
230-
public void TestGenres ()
272+
[TestCase (StringType.Latin1)]
273+
[TestCase (StringType.UTF8)]
274+
public void TestGenres (StringType stringType)
231275
{
232-
var tag = new TagLib.Riff.InfoTag ();
276+
InfoTag tag;
277+
if (stringType == StringType.UTF8)
278+
tag = new TagLib.Riff.InfoTag ();
279+
else
280+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
233281

234282
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
235283
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -264,10 +312,15 @@ public void TestGenres ()
264312
});
265313
}
266314

267-
[Test]
268-
public void TestYear ()
315+
[TestCase (StringType.Latin1)]
316+
[TestCase (StringType.UTF8)]
317+
public void TestYear (StringType stringType)
269318
{
270-
var tag = new TagLib.Riff.InfoTag ();
319+
InfoTag tag;
320+
if (stringType == StringType.UTF8)
321+
tag = new TagLib.Riff.InfoTag ();
322+
else
323+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
271324

272325
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
273326
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -289,10 +342,15 @@ public void TestYear ()
289342
});
290343
}
291344

292-
[Test]
293-
public void TestTrack ()
345+
[TestCase (StringType.Latin1)]
346+
[TestCase (StringType.UTF8)]
347+
public void TestTrack (StringType stringType)
294348
{
295-
var tag = new TagLib.Riff.InfoTag ();
349+
InfoTag tag;
350+
if (stringType == StringType.UTF8)
351+
tag = new TagLib.Riff.InfoTag ();
352+
else
353+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
296354

297355
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
298356
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -314,10 +372,15 @@ public void TestTrack ()
314372
});
315373
}
316374

317-
[Test]
318-
public void TestTrackCount ()
375+
[TestCase (StringType.Latin1)]
376+
[TestCase (StringType.UTF8)]
377+
public void TestTrackCount (StringType stringType)
319378
{
320-
var tag = new TagLib.Riff.InfoTag ();
379+
InfoTag tag;
380+
if (stringType == StringType.UTF8)
381+
tag = new TagLib.Riff.InfoTag ();
382+
else
383+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
321384

322385
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
323386
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
@@ -339,10 +402,15 @@ public void TestTrackCount ()
339402
});
340403
}
341404

342-
[Test]
343-
public void TestCopyright ()
405+
[TestCase (StringType.Latin1)]
406+
[TestCase (StringType.UTF8)]
407+
public void TestCopyright (StringType stringType)
344408
{
345-
var tag = new TagLib.Riff.InfoTag ();
409+
InfoTag tag;
410+
if (stringType == StringType.UTF8)
411+
tag = new TagLib.Riff.InfoTag ();
412+
else
413+
tag = new TagLib.Riff.InfoTag { StringType = stringType };
346414

347415
TagTestWithSave (ref tag, delegate (TagLib.Riff.InfoTag t, string m) {
348416
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);

src/TaglibSharp/Riff/List.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ namespace TagLib.Riff
3838
[ComVisible (false)]
3939
public class List : Dictionary<ByteVector, ByteVectorCollection>
4040
{
41+
42+
#region Private Fields
43+
44+
/// <summary>
45+
/// Contains the <see cref="StringType"/> value used for parsing
46+
/// and rendering the contents of this list.
47+
/// </summary>
48+
StringType string_type = StringType.UTF8;
49+
50+
#endregion
51+
4152
#region Constructors
4253

4354
/// <summary>
@@ -127,8 +138,29 @@ protected List (SerializationInfo info, StreamingContext context)
127138
: base (info, context)
128139
{
129140
}
141+
130142
#endregion
131143

144+
#region Public Properties
145+
146+
/// <summary>
147+
/// Gets or sets the <see cref="StringType"/> value used for parsing
148+
/// and rendering the contents of this list.
149+
/// </summary>
150+
/// <remarks>
151+
/// The value must be StringType.Latin1 or StringType.UTF8.
152+
/// </remarks>
153+
public StringType StringType {
154+
get => string_type;
155+
set {
156+
if (value != StringType.Latin1 && value != StringType.UTF8)
157+
throw new ArgumentException ("Must be Latin1 or UTF8.", nameof(value));
158+
159+
string_type = value;
160+
}
161+
}
162+
163+
#endregion
132164

133165

134166
#region Public Methods
@@ -273,7 +305,7 @@ public string[] GetValuesAsStrings (ByteVector id)
273305
while (length > 0 && data[length - 1] == 0)
274306
length--;
275307

276-
result[i] = data.ToString (StringType.UTF8, 0, length);
308+
result[i] = data.ToString (StringType, 0, length);
277309
}
278310

279311
return result;
@@ -483,7 +515,7 @@ public void SetValue (ByteVector id, IEnumerable<string> values)
483515
if (string.IsNullOrEmpty (value))
484516
continue;
485517

486-
ByteVector data = ByteVector.FromString (value, StringType.UTF8);
518+
ByteVector data = ByteVector.FromString (value, StringType);
487519
data.Add (0);
488520
l.Add (data);
489521
}

src/TaglibSharp/Riff/ListTag.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ protected ListTag (TagLib.File file, long position, int length)
127127

128128
#endregion
129129

130+
#region Public Properties
131+
132+
/// <summary>
133+
/// Gets or sets the <see cref="StringType"/> value used for parsing
134+
/// and rendering the contents of this tag.
135+
/// </summary>
136+
/// <remarks>
137+
/// The value must be StringType.Latin1 or StringType.UTF8.
138+
/// </remarks>
139+
public StringType StringType {
140+
get => fields.StringType;
141+
set => fields.StringType = value;
142+
}
143+
144+
#endregion
145+
130146

131147

132148
#region Public Methods

0 commit comments

Comments
 (0)