[入门] 代码高度精炼的tango.sys.win32.CodePage

tuja 2007-10-03
就一个简单的主函数,实现了
1 MBS to UTF-8
2 UTF-8 to MBS
3 toMBSz ? result.ptr就可以了。
4 toUTF-8z? result.ptr就可以了。

     /**********************************************************************

                Internal conversion routine; we avoid heap activity for
                strings of short and medium length. A zero is appended 
                to the dst array in order to simplify C API conversions

        **********************************************************************/

        private static char[] convert (char[] src, char[] dst, uint from, uint into)
        {       
                uint len = 0;

                // sanity check
                assert (dst.length);

                // got some input?
                if (src.length > 0)
                   {    
                   wchar[2000] tmp = void;
                   wchar[] wide = (src.length <= tmp.length) ? tmp : new wchar[src.length];

                   len = MultiByteToWideChar (from, 0, src.ptr, src.length, 
                                              wide.ptr, wide.length);
                   if (len)
                       len = WideCharToMultiByte (into, 0, wide.ptr, len, 
                                                  dst.ptr, dst.length-1, null, null);
                   if (len is 0)
                       throw new IllegalArgumentException ("CodePage.convert :: "~SysError.lastMsg);
                   }

                // append a null terminator
                dst[len] = 0;
                return dst [0 .. len];
        }


你可以对照Phobos的实现对比看看。
Global site tag (gtag.js) - Google Analytics