开发者

Cross platform _wtoi() implementation?

Hey everyone, I'm writing a piece of software that, due to my choice to use Qt, required me to rewrite a few hundred lines of code to be unicod开发者_如何学运维e compliant (qt forced my entire project _UNICODE). After nearly killing myself writing all this code and making it work I ran into a problem with _wtoi() - it's not cross platform!

Can anyone help me with a fast implementation? I've heard of using stringstreams to do it and remain cross platform but I've never had to use them before and using it as an unicode atoi() seems to allude me. I'd much prefer someone explaining it to me vs. just copying code from readily available websites and forums with stringstreams examples.

Thank you!!


try this:

inline int wtoi(const wchar_t *str)
{
  return (int)wcstol(str, 0, 10);
}


If you want sheer runtime speed , I recommend Boost.Spirit.Qi:

#include <cwchar>
#include <boost/spirit/include/qi_int.hpp>
#include <boost/spirit/include/qi_parse.hpp>

int portable_wtoi(wchar_t const* const str)
{
    using boost::spirit::int_;
    using boost::spirit::qi::parse;

    wchar_t const* p = str;
    int i;
    return parse(p, str + std::wcslen(str), int_, i) && p != str ? i : 0;
}

Note, however, that you may notice increased compile-times (using precompiled headers is recommended).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜