Exposed commify function to lua and made it handle decimal numbers.

This commit is contained in:
Kyzentun
2015-04-02 13:36:41 -06:00
parent da59f1d3b0
commit d789ef7c6b
6 changed files with 70 additions and 14 deletions
+8
View File
@@ -4,6 +4,14 @@ The StepMania 5 Changelog covers all post-sm-ssc changes. For a list of changes
from StepMania 4 alpha 5 to sm-ssc v1.2.5, see Changelog_sm-ssc.txt. from StepMania 4 alpha 5 to sm-ssc v1.2.5, see Changelog_sm-ssc.txt.
________________________________________________________________________________ ________________________________________________________________________________
2015/04/02
----------
* [global] commify function exposed to lua. [kyzentun]
================================================================================
StepMania 5.0.7 | 20150401
--------------------------------------------------------------------------------
2015/03/30 2015/03/30
---------- ----------
* [Select Music] Ctrl+Shift+R mapped to reload the current song. [kyzentun] * [Select Music] Ctrl+Shift+R mapped to reload the current song. [kyzentun]
+1
View File
@@ -209,6 +209,7 @@
<Function name='class'/> <Function name='class'/>
<Function name='collectgarbage'/> <Function name='collectgarbage'/>
<Function name='color'/> <Function name='color'/>
<Function name='commify'/>
<Function name='dofile'/> <Function name='dofile'/>
<Function name='error'/> <Function name='error'/>
<Function name='fapproach'/> <Function name='fapproach'/>
+9
View File
@@ -109,6 +109,15 @@ save yourself some time, copy this for undocumented things:
<Function name='ComboUnderField' theme='_fallback' return='bool' arguments=''> <Function name='ComboUnderField' theme='_fallback' return='bool' arguments=''>
[03 Gameplay.lua] Returns the <code>UserPrefComboUnderField</code> user preference value. [03 Gameplay.lua] Returns the <code>UserPrefComboUnderField</code> user preference value.
</Function> </Function>
<Function name='commify' return='string' arguments='string number, string comma, string dot'>
This will take the number and insert a comma every three digits, as normal in English for writing large numbers.<br />
<code>number</code> can be a string, an integer, or a float.<br />
<code>comma</code> is an optional argument that is used instead of a comma.<br />
"commify(1234, 'cat')" will result in "1cat234".<br />
<code>dot</code> is an optional argument that is used instead of a dot to find the end of the part that should be commified.<br />
"commify('1234cat5678', ',', 'cat')" will result in "1,234cat5678", but "commify('1234cat5678')" will result in "12,34c,at5,678".<br />
The <code>comma</code> and <code>dot</code> arguments are provided to ease compliance with locales or languages that do not use comma and dot in numbers the way English does.
</Function>
<Function name='ConnectToServer' return='bool' arguments='string sAddress'> <Function name='ConnectToServer' return='bool' arguments='string sAddress'>
Tries to connect to the server at <code>sAddress</code>. Tries to connect to the server at <code>sAddress</code>.
</Function> </Function>
+1 -5
View File
@@ -60,11 +60,7 @@ function split(delimiter, text)
end end
function join(delimiter, list) function join(delimiter, list)
local ret = list[1] return table.concat(list, delimiter)
for i = 2,table.getn(list) do
ret = ret .. delimiter .. list[i]
end
return ret or ""
end end
-- (c) 2006 Glenn Maynard -- (c) 2006 Glenn Maynard
+50 -8
View File
@@ -332,17 +332,39 @@ RString Commify( int iNum )
return Commify( sNum ); return Commify( sNum );
} }
RString Commify( RString sNum, RString sSeperator ) RString Commify(const RString& num, const RString& sep, const RString& dot)
{ {
RString sReturn; size_t num_end= num.size();
for( unsigned i=0; i<sNum.length(); i++ ) size_t dot_pos= num.find(dot);
if(dot_pos != string::npos)
{ {
char cDigit = sNum[sNum.length()-1-i]; num_end= dot_pos;
if( i!=0 && i%3 == 0 )
sReturn = sSeperator + sReturn;
sReturn = cDigit + sReturn;
} }
return sReturn; size_t commies= (num_end / 3) - (!(num_end % 3));
if(commies < 1)
{
return num;
}
size_t commified_len= num.size() + (commies * sep.size());
RString ret;
ret.resize(commified_len);
size_t dest= 0;
size_t next_comma= (num_end % 3) + (3 * (!(num_end % 3)));
for(size_t c= 0; c < num.size(); ++c)
{
if(c == next_comma && c < num_end)
{
for(size_t s= 0; s < sep.size(); ++s)
{
ret[dest]= sep[s];
++dest;
}
next_comma+= 3;
}
ret[dest]= num[c];
++dest;
}
return ret;
} }
static LocalizedString NUM_PREFIX ( "RageUtil", "NumPrefix" ); static LocalizedString NUM_PREFIX ( "RageUtil", "NumPrefix" );
@@ -2396,6 +2418,26 @@ static bool UndocumentedFeature( RString s ){ sm_crash(s); return true; }
LuaFunction( UndocumentedFeature, UndocumentedFeature(SArg(1)) ); LuaFunction( UndocumentedFeature, UndocumentedFeature(SArg(1)) );
LuaFunction( lerp, lerp(FArg(1), FArg(2), FArg(3)) ); LuaFunction( lerp, lerp(FArg(1), FArg(2), FArg(3)) );
int LuaFunc_commify(lua_State* L);
int LuaFunc_commify(lua_State* L)
{
RString num= SArg(1);
RString sep= ",";
RString dot= ".";
if(!lua_isnoneornil(L, 2))
{
sep= lua_tostring(L, 2);
}
if(!lua_isnoneornil(L, 3))
{
dot= lua_tostring(L, 3);
}
RString ret= Commify(num, sep, dot);
LuaHelpers::Push(L, ret);
return 1;
}
LUAFUNC_REGISTER_COMMON(commify);
void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind); void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind);
void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind, int process_index) void luafunc_approach_internal(lua_State* L, int valind, int goalind, int speedind, int process_index)
{ {
+1 -1
View File
@@ -360,7 +360,7 @@ RString SecondsToMMSS( float fSecs );
RString PrettyPercent( float fNumerator, float fDenominator ); RString PrettyPercent( float fNumerator, float fDenominator );
inline RString PrettyPercent( int fNumerator, int fDenominator ) { return PrettyPercent( float(fNumerator), float(fDenominator) ); } inline RString PrettyPercent( int fNumerator, int fDenominator ) { return PrettyPercent( float(fNumerator), float(fDenominator) ); }
RString Commify( int iNum ); RString Commify( int iNum );
RString Commify( RString sNum, RString sSeperator = "," ); RString Commify(const RString& num, const RString& sep= ",", const RString& dot= ".");
RString FormatNumberAndSuffix( int i ); RString FormatNumberAndSuffix( int i );