Selectable encodings
John C
johnch_atms at hotmail.com
Thu Apr 6 01:21:55 PDT 2006
I know of three ways to support a user-selected char encoding in a library,
but each has its drawbacks.
1) Method overloading
Introduces conflicts with string literals (forcing a c/w/d suffix to be
used) and you can't overload by return type.
2) Parameterising all types that use strings
Making every class a template just to get this functionality seems over the
top.
class SomeClassT(TChar) {
TChar[] getSomeString() {}
}
alias SomeClassT!(char) SomeClass; // in library module
alias SomeClassT!(wchar) SomeClass; // in user module
3) A compiler version condition with aliases.
The version condition approach is the most attractive to me, but some people
aren't fond of it.
version (utf8) alias mlchar char;
else version (utf16) alias mlchar wchar;
else version (utf32) alias mlchar dchar;
There's a fourth way - encoding conversion, but there's a runtime cost.
So does anyone use an alternative way to enable users to select which char
encoding they want to use at compile time?
More information about the Digitalmars-d
mailing list