StringModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.CoreLib.StringLib;
  6. using MoonSharp.Interpreter.Execution;
  7. namespace MoonSharp.Interpreter.CoreLib
  8. {
  9. [MoonSharpModule(Namespace="string")]
  10. public class StringModule
  11. {
  12. public static void MoonSharpInit(Table globalTable, Table stringTable)
  13. {
  14. Table stringMetatable = new Table(globalTable.OwnerScript);
  15. stringMetatable.Set("__index", DynValue.NewTable(stringTable));
  16. globalTable.OwnerScript.SetTypeMetatable(DataType.String, stringMetatable);
  17. }
  18. [MoonSharpMethod]
  19. public static DynValue @char(ScriptExecutionContext executionContext, CallbackArguments args)
  20. {
  21. StringBuilder sb = new StringBuilder(args.Count);
  22. for (int i = 0; i < args.Count; i++)
  23. {
  24. DynValue v = args[i];
  25. double d = 0d;
  26. if (v.Type == DataType.String)
  27. {
  28. double? nd = v.CastToNumber();
  29. if (nd == null)
  30. args.AsType(i, "char", DataType.Number, false);
  31. else
  32. d = nd.Value;
  33. }
  34. else
  35. {
  36. args.AsType(i, "char", DataType.Number, false);
  37. d = v.Number;
  38. }
  39. sb.Append((char)(d));
  40. }
  41. return DynValue.NewString(sb.ToString());
  42. }
  43. [MoonSharpMethod]
  44. public static DynValue @byte(ScriptExecutionContext executionContext, CallbackArguments args)
  45. {
  46. DynValue vs = args.AsType(0, "byte", DataType.String, false);
  47. DynValue vi = args.AsType(1, "byte", DataType.Number, true);
  48. DynValue vj = args.AsType(2, "byte", DataType.Number, true);
  49. return PerformByteLike(vs, vi, vj,
  50. i => Unicode2Ascii(i));
  51. }
  52. [MoonSharpMethod]
  53. public static DynValue unicode(ScriptExecutionContext executionContext, CallbackArguments args)
  54. {
  55. DynValue vs = args.AsType(0, "unicode", DataType.String, false);
  56. DynValue vi = args.AsType(1, "unicode", DataType.Number, true);
  57. DynValue vj = args.AsType(2, "unicode", DataType.Number, true);
  58. return PerformByteLike(vs, vi, vj, i => i);
  59. }
  60. private static int Unicode2Ascii(int i)
  61. {
  62. if (i >= 0 && i < 255)
  63. return i;
  64. return (int)'?';
  65. }
  66. private static DynValue PerformByteLike(DynValue vs, DynValue vi, DynValue vj, Func<int, int> filter)
  67. {
  68. StringRange range = StringRange.FromLuaRange(vi, vj, null);
  69. string s = range.ApplyToString(vs.String);
  70. int length = s.Length;
  71. DynValue[] rets = new DynValue[length];
  72. for (int i = 0; i < length; ++i)
  73. {
  74. rets[i] = DynValue.NewNumber(filter((int)s[i]));
  75. }
  76. return DynValue.NewTuple(rets);
  77. }
  78. private static int? AdjustIndex(string s, DynValue vi, int defval)
  79. {
  80. if (vi.IsNil())
  81. return defval;
  82. int i = (int)Math.Round(vi.Number, 0);
  83. if (i == 0)
  84. return null;
  85. if (i > 0)
  86. return i - 1;
  87. return s.Length - i;
  88. }
  89. [MoonSharpMethod]
  90. public static DynValue len(ScriptExecutionContext executionContext, CallbackArguments args)
  91. {
  92. DynValue vs = args.AsType(0, "len", DataType.String, false);
  93. return DynValue.NewNumber(vs.String.Length);
  94. }
  95. [MoonSharpMethod]
  96. public static DynValue match(ScriptExecutionContext executionContext, CallbackArguments args)
  97. {
  98. return executionContext.EmulateClassicCall(args, "match", KopiLua_StringLib.str_match);
  99. }
  100. [MoonSharpMethod]
  101. public static DynValue gmatch(ScriptExecutionContext executionContext, CallbackArguments args)
  102. {
  103. return executionContext.EmulateClassicCall(args, "gmatch", KopiLua_StringLib.str_gmatch);
  104. }
  105. [MoonSharpMethod]
  106. public static DynValue gsub(ScriptExecutionContext executionContext, CallbackArguments args)
  107. {
  108. return executionContext.EmulateClassicCall(args, "gsub", KopiLua_StringLib.str_gsub);
  109. }
  110. [MoonSharpMethod]
  111. public static DynValue find(ScriptExecutionContext executionContext, CallbackArguments args)
  112. {
  113. return executionContext.EmulateClassicCall(args, "find",
  114. KopiLua_StringLib.str_find);
  115. }
  116. [MoonSharpMethod]
  117. public static DynValue lower(ScriptExecutionContext executionContext, CallbackArguments args)
  118. {
  119. DynValue arg_s = args.AsType(0, "lower", DataType.String, false);
  120. return DynValue.NewString(arg_s.String.ToLower());
  121. }
  122. [MoonSharpMethod]
  123. public static DynValue upper(ScriptExecutionContext executionContext, CallbackArguments args)
  124. {
  125. DynValue arg_s = args.AsType(0, "upper", DataType.String, false);
  126. return DynValue.NewString(arg_s.String.ToUpper());
  127. }
  128. [MoonSharpMethod]
  129. public static DynValue rep(ScriptExecutionContext executionContext, CallbackArguments args)
  130. {
  131. DynValue arg_s = args.AsType(0, "rep", DataType.String, false);
  132. DynValue arg_n = args.AsType(1, "rep", DataType.Number, false);
  133. DynValue arg_sep = args.AsType(2, "rep", DataType.String, true);
  134. if (String.IsNullOrEmpty(arg_s.String) || (arg_n.Number < 1))
  135. {
  136. return DynValue.NewString("");
  137. }
  138. string sep = (arg_sep.IsNotNil()) ? arg_sep.String : null;
  139. int count = (int)arg_n.Number;
  140. StringBuilder result = new StringBuilder(arg_s.String.Length * count);
  141. for (int i = 0; i < count; ++i)
  142. {
  143. if (i != 0 && sep != null)
  144. result.Append(sep);
  145. result.Append(arg_s.String);
  146. }
  147. return DynValue.NewString(result.ToString());
  148. }
  149. [MoonSharpMethod]
  150. public static DynValue format(ScriptExecutionContext executionContext, CallbackArguments args)
  151. {
  152. return executionContext.EmulateClassicCall(args, "format", KopiLua_StringLib.str_format);
  153. }
  154. [MoonSharpMethod]
  155. public static DynValue reverse(ScriptExecutionContext executionContext, CallbackArguments args)
  156. {
  157. DynValue arg_s = args.AsType(0, "reverse", DataType.String, false);
  158. if (String.IsNullOrEmpty(arg_s.String))
  159. {
  160. return DynValue.NewString("");
  161. }
  162. char[] elements = arg_s.String.ToCharArray();
  163. Array.Reverse(elements);
  164. return DynValue.NewString(new String(elements));
  165. }
  166. [MoonSharpMethod]
  167. public static DynValue sub(ScriptExecutionContext executionContext, CallbackArguments args)
  168. {
  169. DynValue arg_s = args.AsType(0, "sub", DataType.String, false);
  170. DynValue arg_i = args.AsType(1, "sub", DataType.Number, true);
  171. DynValue arg_j = args.AsType(2, "sub", DataType.Number, true);
  172. StringRange range = StringRange.FromLuaRange(arg_i, arg_j, -1);
  173. string s = range.ApplyToString(arg_s.String);
  174. return DynValue.NewString(s);
  175. }
  176. }
  177. }