Mercurial > projects > mde
diff 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 |
line wrap: on
line diff
--- a/mde/file/deserialize.d Fri Aug 29 11:59:43 2008 +0100 +++ b/mde/file/deserialize.d Sat Aug 30 09:37:35 2008 +0100 @@ -17,7 +17,8 @@ * Generic deserialization templated function. * * Supports: - * Associative arrays, arrays (inc. strings), structs, char types, bool, int types, float types. + * Associative arrays, dynamic arrays (with usual formatting of strings), structs, char types, + * bool, int types, float types. * * There are also some public utility functions with their own documentation. * @@ -204,12 +205,25 @@ throw new ParseException ("Invalid char: '\\'"); } // Basic unicode convertions for wide-chars. -// Assumes value is <= 127 as does deserialize!(char). T deserialize(T : wchar) (char[] src) { - return cast(T) deserialize!(char) (src); + src = Util.trim(src); + if (src.length < 3 || src[0] != '\'' || src[$-1] != '\'') + throw new ParseException ("Invalid char: not 'x' or '\\x'"); + T[] t = Utf.toString16 (src[1..$-1]); + if (t.length == 1) + return t[0]; + else + throw new ParseException ("Invalid char: not one character"); } T deserialize(T : dchar) (char[] src) { - return cast(T) deserialize!(char) (src); + src = Util.trim(src); + if (src.length < 3 || src[0] != '\'' || src[$-1] != '\'') + throw new ParseException ("Invalid char: not 'x' or '\\x'"); + T[] t = Utf.toString32 (src[1..$-1]); + if (t.length == 1) + return t[0]; + else + throw new ParseException ("Invalid char: not one character"); } // Bool @@ -496,12 +510,12 @@ } //END Utility funcs -debug (UnitTest) { +debug (mdeUnitTest) { import tango.util.log.Log : Log, Logger; private Logger logger; static this() { - logger = Log.getLogger ("text.deserialize"); + logger = Log.getLogger ("mde.file.deserialize"); } unittest { // Utility @@ -511,7 +525,7 @@ dg(); } catch (Exception e) { r = true; - logger.info ("Exception caught: "~e.msg); + logger.trace ("Exception caught: "~e.msg); } return r; } @@ -566,6 +580,8 @@ assert (deserialize!(char) ("'\\\''") == '\''); assert (deserialize!(wchar) ("'X'") == 'X'); assert (deserialize!(dchar) ("'X'") == 'X'); + assert (deserialize!(wchar) ("'£'") == '£'); + assert (deserialize!(dchar) ("'£'") == '£'); assert (throws ({ deserialize!(char) ("'\\'"); })); assert (throws ({ deserialize!(char) ("'£'"); })); // non-ascii assert (throws ({ deserialize!(char) ("''"); }));