utf 8 - Convert a text file to UTF8 in D -
i'm attempting use phobos standard library functions read in valid utf file (utf-8, utf-16, or utf-32) , utf-8 string (aka d's string). after looking through docs, concise function think of is
using std.file, std.utf; string readtoutf8(in string filename) { try { return readtext(filename); } catch (utfexception e) { try { return toutf8(readtext!wstring(filename)); } catch (utfexception e) { return toutf8(readtext!dstring(filename)); } } } however, catching cascading series of exceptions seems extremely hackish. there "cleaner" way go without relying on catching series of exceptions?
additionally, above function seems return one-byte bom in resulting string if source file utf-16 or utf-32, omit given it's utf-8. there way omit besides explicitly stripping it?
one of questions answers other: bom allows identify exact utf encoding used in file.
ideally, readtext you. currently, doesn't, you'd have implement yourself.
i'd recommend using std.file.read, casting returned void[] ubyte[], looking @ first few bytes see if start bom, cast result appropriate string type , convert string (using toutf8 or to!string).
Comments
Post a Comment