2014-01-20 15:26:44 +00:00
using System ;
2014-06-03 02:19:13 +00:00
using System.ComponentModel ;
2014-05-18 21:06:16 +00:00
using System.Linq ;
using LuaInterface ;
2014-01-20 15:26:44 +00:00
namespace BizHawk.Client.Common
{
2014-06-03 02:19:13 +00:00
[Description("A library exposing standard .NET string methods")]
2014-06-01 22:02:59 +00:00
public sealed class StringLuaLibrary : LuaLibraryBase
2014-01-20 15:26:44 +00:00
{
2014-03-26 22:30:48 +00:00
public override string Name { get { return "bizstring" ; } }
2014-01-26 13:30:45 +00:00
2014-05-18 21:06:16 +00:00
public StringLuaLibrary ( Lua lua )
2014-05-21 00:17:35 +00:00
: base ( lua ) { }
public StringLuaLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2014-05-18 21:06:16 +00:00
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"hex" ,
2014-01-26 18:36:27 +00:00
"Converts the number to a string representation of the hexadecimal value of the given number"
2014-01-26 13:30:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public static string Hex ( long num )
2014-01-20 15:26:44 +00:00
{
2014-02-03 20:48:01 +00:00
var hex = string . Format ( "{0:X}" , num ) ;
2014-01-26 13:30:45 +00:00
if ( hex . Length = = 1 )
{
hex = "0" + hex ;
}
2014-01-20 15:26:44 +00:00
return hex ;
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"binary" ,
2014-01-26 18:36:27 +00:00
"Converts the number to a string representation of the binary value of the given number"
2014-01-26 13:30:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public static string Binary ( long num )
2014-01-20 15:26:44 +00:00
{
2014-01-27 03:16:05 +00:00
var binary = Convert . ToString ( num , 2 ) ;
2014-01-20 15:26:44 +00:00
binary = binary . TrimStart ( '0' ) ;
return binary ;
}
2014-01-20 17:38:31 +00:00
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"octal" ,
2014-01-26 18:36:27 +00:00
"Converts the number to a string representation of the octal value of the given number"
2014-01-26 13:30:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public static string Octal ( long num )
2014-01-20 17:38:31 +00:00
{
2014-01-27 03:16:05 +00:00
var octal = Convert . ToString ( num , 8 ) ;
2014-01-26 13:30:45 +00:00
if ( octal . Length = = 1 )
{
octal = "0" + octal ;
}
2014-01-20 17:38:31 +00:00
return octal ;
}
2014-01-20 15:26:44 +00:00
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"trim" ,
2014-01-26 18:36:27 +00:00
"returns a string that trims whitespace on the left and right ends of the string"
2014-01-26 13:30:45 +00:00
) ]
public static string Trim ( string str )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-20 17:15:30 +00:00
return str . Trim ( ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"replace" ,
2014-01-26 18:36:27 +00:00
"Returns a string that replaces all occurances of str2 in str1 with the value of replace"
2014-01-26 13:30:45 +00:00
) ]
public static string Replace ( string str , string str2 , string replace )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-26 13:30:45 +00:00
return str . Replace ( str2 , replace ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"toupper" ,
2014-01-26 18:36:27 +00:00
"Returns an uppercase version of the given string"
2014-01-26 13:30:45 +00:00
) ]
public static string ToUpper ( string str )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-20 17:15:30 +00:00
return str . ToUpper ( ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"tolower" ,
2014-01-26 18:36:27 +00:00
"Returns an lowercase version of the given string"
2014-01-26 13:30:45 +00:00
) ]
public static string ToLower ( string str )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-20 17:15:30 +00:00
return str . ToLower ( ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"substring" ,
2014-01-26 18:36:27 +00:00
"Returns a string that represents a substring of str starting at position for the specified length"
2014-01-26 13:30:45 +00:00
) ]
2014-01-27 01:15:56 +00:00
public static string SubString ( string str , int position , int length )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-27 01:15:56 +00:00
return str . Substring ( position , length ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"remove" ,
2014-01-26 18:36:27 +00:00
"Returns a string that represents str with the given position and count removed"
2014-01-26 13:30:45 +00:00
) ]
2014-01-27 01:15:56 +00:00
public static string Remove ( string str , int position , int count )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return null ;
}
2014-01-27 01:15:56 +00:00
return str . Remove ( position , count ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"contains" ,
2014-01-26 18:36:27 +00:00
"Returns whether or not str contains str2"
2014-01-26 13:30:45 +00:00
) ]
public static bool Contains ( string str , string str2 )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return false ;
}
2014-01-20 17:15:30 +00:00
return str . Contains ( str2 ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"startswith" ,
2014-01-26 18:36:27 +00:00
"Returns whether str starts with str2"
2014-01-26 13:30:45 +00:00
) ]
public static bool StartsWith ( string str , string str2 )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return false ;
}
2014-01-20 17:15:30 +00:00
return str . StartsWith ( str2 ) ;
2014-01-20 17:06:09 +00:00
}
2014-01-26 13:30:45 +00:00
[ LuaMethodAttributes (
"endswith" ,
2014-01-26 18:36:27 +00:00
"Returns whether str ends wth str2"
2014-01-26 13:30:45 +00:00
) ]
public static bool EndsWith ( string str , string str2 )
2014-01-20 17:06:09 +00:00
{
2014-05-21 02:12:27 +00:00
if ( string . IsNullOrEmpty ( str ) )
{
return false ;
}
2014-01-20 17:15:30 +00:00
return str . EndsWith ( str2 ) ;
2014-01-20 17:06:09 +00:00
}
2014-05-18 21:06:16 +00:00
[ LuaMethodAttributes (
"split" ,
"Splits str based on separator into a LuaTable. Separator must be one character!. Same functionality as .NET string.Split() using the RemoveEmptyEntries option"
) ]
public LuaTable Split ( string str , string separator )
{
2014-05-20 20:34:51 +00:00
var table = Lua . NewTable ( ) ;
2014-05-21 02:05:26 +00:00
if ( ! string . IsNullOrEmpty ( str ) )
2014-05-18 21:06:16 +00:00
{
2014-05-21 02:05:26 +00:00
var splitStr = str . Split (
new char [ ] { separator . FirstOrDefault ( ) } ,
StringSplitOptions . RemoveEmptyEntries ) ;
for ( int i = 0 ; i < splitStr . Length ; i + + )
{
table [ i ] = splitStr [ i ] ;
}
2014-05-18 21:06:16 +00:00
}
return table ;
}
2014-01-20 15:26:44 +00:00
}
}