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 ;
2017-07-10 04:51:02 +00:00
using NLua ;
2014-05-18 21:06:16 +00:00
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
{
2017-04-14 19:59:01 +00:00
public override string Name = > "bizstring" ;
2014-01-26 13:30:45 +00:00
2017-04-15 20:37:30 +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
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizhex = bizstring.hex( -12345 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("hex", "Converts the number to a string representation of the hexadecimal value of the given number")]
2014-01-27 03:16:05 +00:00
public static string Hex ( long num )
2014-01-20 15:26:44 +00:00
{
2017-04-14 19:59:01 +00:00
var hex = $"{num:X}" ;
2014-01-26 13:30:45 +00:00
if ( hex . Length = = 1 )
{
2019-03-20 05:01:12 +00:00
hex = $"0{hex}" ;
2014-01-26 13:30:45 +00:00
}
2014-01-20 15:26:44 +00:00
return hex ;
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizbin = bizstring.binary( -12345 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("binary", "Converts the number to a string representation of the binary value of the given number")]
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
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizoct = bizstring.octal( -12345 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("octal", "Converts the number to a string representation of the octal value of the given number")]
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 )
{
2019-03-20 05:01:12 +00:00
octal = $"0{octal}" ;
2014-01-26 13:30:45 +00:00
}
2014-01-20 17:38:31 +00:00
return octal ;
}
2014-01-20 15:26:44 +00:00
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbiztri = bizstring.trim( \"Some trim string\t \" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("trim", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizrep = bizstring.replace( \"Some string\", \"Some\", \"Replaced\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("replace", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbiztou = bizstring.toupper( \"Some string\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("toupper", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbiztol = bizstring.tolower( \"Some string\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("tolower", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizsub = bizstring.substring( \"Some string\", 6, 3 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("substring", "Returns a string that represents a substring of str starting at position for the specified length")]
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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local stbizrem = bizstring.remove( \"Some string\", 4, 5 );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("remove", "Returns a string that represents str with the given position and count removed")]
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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("if ( bizstring.contains( \"Some string\", \"Some\") ) then\r\n\tconsole.log( \"Returns whether or not str contains str2\" );\r\nend;")]
2017-07-10 19:02:00 +00:00
[LuaMethod("contains", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("if ( bizstring.startswith( \"Some string\", \"Some\") ) then\r\n\tconsole.log( \"Returns whether str starts with str2\" );\r\nend;")]
2017-07-10 19:02:00 +00:00
[LuaMethod("startswith", "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
}
2018-03-14 01:05:30 +00:00
[LuaMethodExample("if ( bizstring.endswith( \"Some string\", \"string\") ) then\r\n\tconsole.log( \"Returns whether str ends wth str2\" );\r\nend;")]
2017-07-10 19:02:00 +00:00
[LuaMethod("endswith", "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
2018-03-14 01:05:30 +00:00
[LuaMethodExample("local nlbizspl = bizstring.split( \"Some, string\", \", \" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("split", "Splits str based on separator into a LuaTable. Separator must be one character!. Same functionality as .NET string.Split() using the RemoveEmptyEntries option")]
2014-05-18 21:06:16 +00:00
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 (
2017-05-09 18:19:55 +00:00
new [ ] { separator . FirstOrDefault ( ) } ,
2014-05-21 02:05:26 +00:00
StringSplitOptions . RemoveEmptyEntries ) ;
for ( int i = 0 ; i < splitStr . Length ; i + + )
{
2015-09-09 23:57:14 +00:00
table [ i + 1 ] = splitStr [ i ] ;
2014-05-21 02:05:26 +00:00
}
2014-05-18 21:06:16 +00:00
}
return table ;
}
2014-01-20 15:26:44 +00:00
}
}