Detecting at compile time if a string is zero terminated

Johannes Pfau spam at example.com
Wed Jan 19 07:42:30 PST 2011


Hi,
I'm currently writing a small wrapper layer for gettext. My current
getText template looks like this:

----------------------------------------------------
string getText()(string msgid, string domain = null, Category cat =
Category.Messages) {
    auto cmsg = toStringz(msgid);
    auto nmsg = dcgettext(domain ? toStringz(domain) : null,
              cmsg, cat);
    if(cmsg == nmsg)
        return msgid;
    else
    {
        string nstr = cast(string)nmsg[0 .. strlen(nmsg)];
        return nstr;
    }
}

string getText(string msgid, string domain = null, Category cat =
Category.Messages)() {
    auto nmsg = dcgettext(domain ? domain.ptr : null,
              msgid.ptr, cat);
    if(msgid.ptr == nmsg)
        return msgid;
    else
    {
        string nstr = cast(string)nmsg[0 .. strlen(nmsg)];
        return nstr;
    }
}
----------------------------------------------------

As string literals in D are zero terminated, there's no need
for the toStringz overhead. The overload taking compile time
parameters takes advantage of that. The code works and can be used like
this:
----------------------------------------------------
    writeln(getText!"Hello World!"); //no toStringz
    writeln(getText("Hello World!")); //toStringz
----------------------------------------------------

But if somehow possible I'd like to merge the templates so that there
is only one way to call getText and the fastest way is chosen
automatically.

Does anyone know how to do that?
-- 
Johannes Pfau
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110119/a7b16c68/attachment.pgp>


More information about the Digitalmars-d-learn mailing list