2014-01-20 15:26:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
namespace BizHawk.Client.Common
|
|
|
|
|
{
|
|
|
|
|
public class StringLuaLibrary : LuaLibraryBase
|
|
|
|
|
{
|
|
|
|
|
public override string Name { get { return "string"; } }
|
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-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-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-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-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-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-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-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-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-01-20 17:15:30 +00:00
|
|
|
|
return str.EndsWith(str2);
|
2014-01-20 17:06:09 +00:00
|
|
|
|
}
|
2014-01-20 15:26:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|