Skip to content

Commit

Permalink
Merge pull request #8 from moheb2000/main
Browse files Browse the repository at this point in the history
Fixed windows-1256 problems
  • Loading branch information
robert-virkus authored Aug 18, 2021
2 parents eff08cd + 1561c0e commit e439bab
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 60 deletions.
126 changes: 66 additions & 60 deletions lib/windows/windows1256.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import 'dart:convert' as cnvrt;
import 'package:enough_convert/windows/windows.dart';

const String _cp1256Symbols =
'€?‚ƒ„…†‡ˆ‰ٹ‹Œ?ژ??‘’“”•–—ک™ڑ›œ?‍ں\u{00A0}،¢£¤¥¦§¨©ھ«¬\u{00AD}®¯°±²³´µ¶·¸¹؛»¼½¾؟ہءآأؤإئابةتثجحخدذرزسشصض×طظعغـفقكàلâمنهوçèéêëىيîïًٌٍَôُِ÷ّùْûü‎‏ے';
'€پ‚ƒ„…†‡ˆ‰ٹ‹Œچژڈگ‘’“”•–—ک™ڑ›œ?‍ں\u{00A0}،¢£¤¥¦§¨©ھ«¬\u{00AD}®¯°±²³´µ¶·¸¹؛»¼½¾؟ہءآأؤإئابةتثجحخدذرزسشصض×طظعغـفقكàلâمنهوçèéêëىيîïًٌٍَôُِ÷ّùْûü‎‏ے';
const Map<int, int> _cp1256Map = {
8364: 128,
1662: 129,
8218: 130,
402: 131,
8222: 132,
Expand All @@ -14,26 +15,30 @@ const Map<int, int> _cp1256Map = {
8225: 135,
710: 136,
8240: 137,
352: 138,
1657: 138,
8249: 139,
338: 140,
381: 142,
1670: 141,
1688: 142,
1672: 143,
1711: 144,
8216: 145,
8217: 146,
8220: 147,
8221: 148,
8226: 149,
8211: 150,
8212: 151,
732: 152,
1705: 152,
8482: 153,
353: 154,
1681: 154,
8250: 155,
339: 156,
382: 158,
376: 159,
8204: 157,
8205: 158,
1722: 159,
160: 160,
161: 161,
1548: 161,
162: 162,
163: 163,
164: 164,
Expand All @@ -42,7 +47,7 @@ const Map<int, int> _cp1256Map = {
167: 167,
168: 168,
169: 169,
170: 170,
1726: 170,
171: 171,
172: 172,
173: 173,
Expand All @@ -58,76 +63,77 @@ const Map<int, int> _cp1256Map = {
183: 183,
184: 184,
185: 185,
186: 186,
1563: 186,
187: 187,
188: 188,
189: 189,
190: 190,
191: 191,
192: 192,
193: 193,
194: 194,
195: 195,
196: 196,
197: 197,
198: 198,
199: 199,
200: 200,
201: 201,
202: 202,
203: 203,
204: 204,
205: 205,
206: 206,
207: 207,
208: 208,
209: 209,
210: 210,
211: 211,
212: 212,
213: 213,
214: 214,
1567: 191,
1729: 192,
1569: 193,
1570: 194,
1571: 195,
1572: 196,
1573: 197,
1574: 198,
1575: 199,
1576: 200,
1577: 201,
1578: 202,
1579: 203,
1580: 204,
1581: 205,
1582: 206,
1583: 207,
1584: 208,
1585: 209,
1586: 210,
1587: 211,
1588: 212,
1589: 213,
1590: 214,
215: 215,
216: 216,
217: 217,
218: 218,
219: 219,
220: 220,
221: 221,
222: 222,
223: 223,
1591: 216,
1592: 217,
1593: 218,
1594: 219,
1600: 220,
1601: 221,
1602: 222,
1603: 223,
224: 224,
225: 225,
1604: 225,
226: 226,
227: 227,
228: 228,
229: 229,
230: 230,
1605: 227,
1606: 228,
1607: 229,
1608: 230,
231: 231,
232: 232,
233: 233,
234: 234,
235: 235,
236: 236,
237: 237,
1609: 236,
1740: 237,
1610: 237,
238: 238,
239: 239,
240: 240,
241: 241,
242: 242,
243: 243,
1611: 240,
1612: 241,
1613: 242,
1614: 243,
244: 244,
245: 245,
246: 246,
1615: 245,
1616: 246,
247: 247,
248: 248,
1617: 248,
249: 249,
250: 250,
1618: 250,
251: 251,
252: 252,
253: 253,
254: 254,
255: 255,
8206: 253,
8207: 254,
1746: 255,
};

/// Provides a windows 1256 / cp1256 codec for easy encoding and decoding.
Expand Down
88 changes: 88 additions & 0 deletions test/windows/windows1256_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'dart:convert' as convert;

// import 'package:enough_convert/base.dart';
import 'package:enough_convert/windows/windows1256.dart';
import 'package:test/test.dart';

void main() {
group('Codec tests', () {
test('name', () {
expect(Windows1256Codec().name, 'windows-1256');
// BaseEncoder.createEncodingMap(Windows1254Decoder().symbols, 0x7f);
});
test('Decoder/encoder classes', () {
expect(Windows1256Codec().encoder, isA<Windows1256Encoder>());
expect(Windows1256Codec().decoder, isA<Windows1256Decoder>());
});
});

group('Decoder tests', () {
test('Decode ascii', () {
final bytes = convert.ascii.encode('hello world');
expect(Windows1256Decoder().convert(bytes), 'hello world');
});

test('Decode Windows1256', () {
expect(Windows1256Decoder().convert([0xC4, 0xD6, 0xFC]), 'ؤضü');
final bytes = Windows1256Encoder().convert('سلام اين يک متن تست است!');
expect(
Windows1256Decoder().convert(bytes), 'سلام اين يک متن تست است!');
});

test('Decode Windows1256 with invalid value when invalid input is allowed',
() {
expect(
Windows1256Decoder(allowInvalid: true)
.convert([0xC4, 0xD6, 0xFC, 0xFF1]),
'ؤضü�');
});

test(
'Decode Windows1256 with invalid value when invalid input is not allowed',
() {
expect(() => Windows1256Decoder().convert([0xC4, 0xD6, 0xFC, 0xFF1]),
throwsA(isA<FormatException>()));
});
});

group('Encoder tests', () {
test('encode ascii', () {
final bytes = Windows1256Encoder().convert('hello world');
expect(bytes, convert.latin1.encode('hello world'));
});

test('encode Windows1256', () {
var bytes = Windows1256Encoder().convert('ؤضü');
expect(bytes, [0xC4, 0xD6, 0xFC]);
bytes = Windows1256Encoder().convert('سلام اين يک متن تست است!');
expect(bytes.any((element) => element > 0xFF), false);
});

test('encode more Windows1256 ', () {
final encoder = Windows1256Encoder();
final decoder = Windows1256Decoder();
var bytes = encoder.convert(decoder.symbols);
var expected = List.generate(0xFF - 0x7F, (index) => index + 0x80);
for (var i = 0; i < decoder.symbols.length; i++) {
if (decoder.symbols.codeUnitAt(i) == 0x3F) {
// ?
expected[i] = 0x3F;
}
}
expect(bytes, expected);
});

test('encode Windows1256 with invalid value when invalid input is allowed',
() {
var bytes = Windows1256Encoder(allowInvalid: true).convert('ؤضü�');
expect(Windows1256Decoder().convert(bytes), 'ؤضü?');
});

test(
'encode Windows1256 with invalid value when invalid input is not allowed',
() {
expect(() => Windows1256Encoder().convert('ؤضü�'),
throwsA(isA<FormatException>()));
});
});
}

0 comments on commit e439bab

Please sign in to comment.