From 2cb8cddc82e545a58185ab3889bba3221965bb0f Mon Sep 17 00:00:00 2001 From: Underground78 Date: Sun, 19 Apr 2015 00:14:17 -0700 Subject: [PATCH] Text subtitles: Prefer wcstol/wcstod to swscanf_s for simple conversions. wcstol/wcstod are 4 or 5 times faster than swscanf_s when parsing just one number. Around 10% faster loading overall on my ASS test file. --- src/subtitles/STS.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/subtitles/STS.cpp b/src/subtitles/STS.cpp index 2dcf94f3c..aacc38b19 100644 --- a/src/subtitles/STS.cpp +++ b/src/subtitles/STS.cpp @@ -1280,25 +1280,32 @@ static inline CStringW::PCXSTR TryNextStr(CStringW::PXSTR * buff, WCHAR sep = L' static inline int NextInt(CStringW::PXSTR * buff, WCHAR sep = L',') //throw(...) { CStringW::PCXSTR str = TryNextStr(buff, sep); + CStringW::PXSTR strEnd; - const wchar_t *fmtstr = L"%d"; + int ret; if (str[0] == '&' && (str[1] == 'h' || str[1] == 'H') || str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { - fmtstr = L"%x"; str += 2; + ret = (int)wcstoul(str, &strEnd, 16); + } else { + ret = wcstol(str, &strEnd, 10); } - int ret; - if(swscanf(str, fmtstr, &ret) != 1) throw 1; + if (str == strEnd) { // Ensure something was parsed + throw 1; + } return(ret); } static inline double NextFloat(CStringW::PXSTR * buff, WCHAR sep = L',') //throw(...) { - CStringW str; - str = TryNextStr(buff, sep); - str.MakeLower(); + if (sep == L'.') { // Parsing a float with '.' as separator doesn't make much sense... + ASSERT(FALSE); + return NextInt(buff, sep); + } + + CStringW::PCXSTR str = TryNextStr(buff, sep); float ret; if(swscanf(str, L"%f", &ret) != 1) throw 1;