BizHawk/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs

179 lines
4.3 KiB
C#
Raw Normal View History

2014-01-20 15:26:44 +00:00
using System;
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
{
[Description("A library exposing standard .NET string methods")]
public sealed class StringLuaLibrary : LuaLibraryBase
2014-01-20 15:26:44 +00:00
{
public override string Name => "bizstring";
2017-04-15 20:37:30 +00:00
public StringLuaLibrary(Lua lua)
: base(lua) { }
public StringLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback) { }
2014-05-18 21:06:16 +00:00
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"hex", "Converts the number to a string representation of the hexadecimal value of the given number")]
public static string Hex(long num)
2014-01-20 15:26:44 +00:00
{
var hex = $"{num:X}";
if (hex.Length == 1)
{
hex = "0" + hex;
}
2014-01-20 15:26:44 +00:00
return hex;
}
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"binary", "Converts the number to a string representation of the binary value of the given number")]
public static string Binary(long num)
2014-01-20 15:26:44 +00:00
{
var binary = Convert.ToString(num, 2);
2014-01-20 15:26:44 +00:00
binary = binary.TrimStart('0');
return binary;
}
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"octal", "Converts the number to a string representation of the octal value of the given number")]
public static string Octal(long num)
{
var octal = Convert.ToString(num, 8);
if (octal.Length == 1)
{
octal = "0" + octal;
}
return octal;
}
2014-01-20 15:26:44 +00:00
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"trim", "returns a string that trims whitespace on the left and right ends of the string")]
public static string Trim(string str)
2014-01-20 17:06:09 +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
}
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"replace", "Returns a string that replaces all occurances of str2 in str1 with the value of replace")]
public static string Replace(string str, string str2, string replace)
2014-01-20 17:06:09 +00:00
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Replace(str2, replace);
2014-01-20 17:06:09 +00:00
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("toupper", "Returns an uppercase version of the given string")]
public static string ToUpper(string str)
2014-01-20 17:06:09 +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
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("tolower", "Returns an lowercase version of the given string")]
public static string ToLower(string str)
2014-01-20 17:06:09 +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
}
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"substring", "Returns a string that represents a substring of str starting at position for the specified length")]
public static string SubString(string str, int position, int length)
2014-01-20 17:06:09 +00:00
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Substring(position, length);
2014-01-20 17:06:09 +00:00
}
[LuaMethodAttributes(
2017-05-09 18:19:55 +00:00
"remove", "Returns a string that represents str with the given position and count removed")]
public static string Remove(string str, int position, int count)
2014-01-20 17:06:09 +00:00
{
if (string.IsNullOrEmpty(str))
{
return null;
}
return str.Remove(position, count);
2014-01-20 17:06:09 +00:00
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("contains", "Returns whether or not str contains str2")]
public static bool Contains(string str, string str2)
2014-01-20 17:06:09 +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
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("startswith", "Returns whether str starts with str2")]
public static bool StartsWith(string str, string str2)
2014-01-20 17:06:09 +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
}
2017-05-09 18:19:55 +00:00
[LuaMethodAttributes("endswith", "Returns whether str ends wth str2")]
public static bool EndsWith(string str, string str2)
2014-01-20 17:06:09 +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(
2017-05-09 18:19:55 +00:00
"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)
{
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
}
}