Skip to content

Commit

Permalink
Text subtitles: Prefer wcstol/wcstod to swscanf_s for simple conversi…
Browse files Browse the repository at this point in the history
…ons.

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.
  • Loading branch information
Underground78 authored and Cyberbeing committed Oct 10, 2015
1 parent 50b815a commit 2cb8cdd
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/subtitles/STS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2cb8cdd

Please sign in to comment.