Mercurial > projects > mde
comparison mde/file/deserialize.d @ 82:ac1e3fd07275
New ssi file format.
(De)serializer now supports non-ascii wide characters (encoded to UTF-8) and no longer supports non-ascii 8-bit chars which would result in bad UTF-8.
Moved/renamed a few things left over from the last commit.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Sat, 30 Aug 2008 09:37:35 +0100 |
parents | d8fccaa45d5f |
children | 79d816b3e2d2 |
comparison
equal
deleted
inserted
replaced
81:d8fccaa45d5f | 82:ac1e3fd07275 |
---|---|
15 | 15 |
16 /************************************************************************************************** | 16 /************************************************************************************************** |
17 * Generic deserialization templated function. | 17 * Generic deserialization templated function. |
18 * | 18 * |
19 * Supports: | 19 * Supports: |
20 * Associative arrays, arrays (inc. strings), structs, char types, bool, int types, float types. | 20 * Associative arrays, dynamic arrays (with usual formatting of strings), structs, char types, |
21 * bool, int types, float types. | |
21 * | 22 * |
22 * There are also some public utility functions with their own documentation. | 23 * There are also some public utility functions with their own documentation. |
23 * | 24 * |
24 * Examples: | 25 * Examples: |
25 * ------------------------------------------------------------------------------------------------ | 26 * ------------------------------------------------------------------------------------------------ |
202 return unEscapeChar (src[2]); // Or escaped | 203 return unEscapeChar (src[2]); // Or escaped |
203 | 204 |
204 throw new ParseException ("Invalid char: '\\'"); | 205 throw new ParseException ("Invalid char: '\\'"); |
205 } | 206 } |
206 // Basic unicode convertions for wide-chars. | 207 // Basic unicode convertions for wide-chars. |
207 // Assumes value is <= 127 as does deserialize!(char). | |
208 T deserialize(T : wchar) (char[] src) { | 208 T deserialize(T : wchar) (char[] src) { |
209 return cast(T) deserialize!(char) (src); | 209 src = Util.trim(src); |
210 if (src.length < 3 || src[0] != '\'' || src[$-1] != '\'') | |
211 throw new ParseException ("Invalid char: not 'x' or '\\x'"); | |
212 T[] t = Utf.toString16 (src[1..$-1]); | |
213 if (t.length == 1) | |
214 return t[0]; | |
215 else | |
216 throw new ParseException ("Invalid char: not one character"); | |
210 } | 217 } |
211 T deserialize(T : dchar) (char[] src) { | 218 T deserialize(T : dchar) (char[] src) { |
212 return cast(T) deserialize!(char) (src); | 219 src = Util.trim(src); |
220 if (src.length < 3 || src[0] != '\'' || src[$-1] != '\'') | |
221 throw new ParseException ("Invalid char: not 'x' or '\\x'"); | |
222 T[] t = Utf.toString32 (src[1..$-1]); | |
223 if (t.length == 1) | |
224 return t[0]; | |
225 else | |
226 throw new ParseException ("Invalid char: not one character"); | |
213 } | 227 } |
214 | 228 |
215 // Bool | 229 // Bool |
216 T deserialize(T : bool) (char[] src) { | 230 T deserialize(T : bool) (char[] src) { |
217 src = Util.trim(src); | 231 src = Util.trim(src); |
494 setStruct!(S, N, i+1) (s, src); | 508 setStruct!(S, N, i+1) (s, src); |
495 } | 509 } |
496 } | 510 } |
497 //END Utility funcs | 511 //END Utility funcs |
498 | 512 |
499 debug (UnitTest) { | 513 debug (mdeUnitTest) { |
500 import tango.util.log.Log : Log, Logger; | 514 import tango.util.log.Log : Log, Logger; |
501 | 515 |
502 private Logger logger; | 516 private Logger logger; |
503 static this() { | 517 static this() { |
504 logger = Log.getLogger ("text.deserialize"); | 518 logger = Log.getLogger ("mde.file.deserialize"); |
505 } | 519 } |
506 unittest { | 520 unittest { |
507 // Utility | 521 // Utility |
508 bool throws (void delegate() dg) { | 522 bool throws (void delegate() dg) { |
509 bool r = false; | 523 bool r = false; |
510 try { | 524 try { |
511 dg(); | 525 dg(); |
512 } catch (Exception e) { | 526 } catch (Exception e) { |
513 r = true; | 527 r = true; |
514 logger.info ("Exception caught: "~e.msg); | 528 logger.trace ("Exception caught: "~e.msg); |
515 } | 529 } |
516 return r; | 530 return r; |
517 } | 531 } |
518 assert (!throws ({ int i = 5; })); | 532 assert (!throws ({ int i = 5; })); |
519 assert (throws ({ throw new Exception ("Test - this exception should be caught"); })); | 533 assert (throws ({ throw new Exception ("Test - this exception should be caught"); })); |
564 | 578 |
565 // char types | 579 // char types |
566 assert (deserialize!(char) ("'\\\''") == '\''); | 580 assert (deserialize!(char) ("'\\\''") == '\''); |
567 assert (deserialize!(wchar) ("'X'") == 'X'); | 581 assert (deserialize!(wchar) ("'X'") == 'X'); |
568 assert (deserialize!(dchar) ("'X'") == 'X'); | 582 assert (deserialize!(dchar) ("'X'") == 'X'); |
583 assert (deserialize!(wchar) ("'£'") == '£'); | |
584 assert (deserialize!(dchar) ("'£'") == '£'); | |
569 assert (throws ({ deserialize!(char) ("'\\'"); })); | 585 assert (throws ({ deserialize!(char) ("'\\'"); })); |
570 assert (throws ({ deserialize!(char) ("'£'"); })); // non-ascii | 586 assert (throws ({ deserialize!(char) ("'£'"); })); // non-ascii |
571 assert (throws ({ deserialize!(char) ("''"); })); | 587 assert (throws ({ deserialize!(char) ("''"); })); |
572 assert (throws ({ deserialize!(char) ("'ab'"); })); | 588 assert (throws ({ deserialize!(char) ("'ab'"); })); |
573 assert (throws ({ deserialize!(wchar) ("''"); })); | 589 assert (throws ({ deserialize!(wchar) ("''"); })); |