Adds simple string library
This commit is contained in:
parent
8da35b4e71
commit
93e256218d
|
@ -4,6 +4,7 @@ using System.Reflection;
|
||||||
namespace Upsilon.StandardLibraries
|
namespace Upsilon.StandardLibraries
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||||
|
[JetBrains.Annotations.MeansImplicitUse]
|
||||||
public class ScriptFunctionAttribute : Attribute
|
public class ScriptFunctionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public ScriptFunctionAttribute(string name, string comment = null,
|
public ScriptFunctionAttribute(string name, string comment = null,
|
||||||
|
|
|
@ -81,6 +81,11 @@ namespace Upsilon.StandardLibraries
|
||||||
boundFuncs.Add("list",
|
boundFuncs.Add("list",
|
||||||
new UserDataVariableSymbol("list", BoundTypeHandler.GetTypeDefinition(typeof(ListLibrary)), true));
|
new UserDataVariableSymbol("list", BoundTypeHandler.GetTypeDefinition(typeof(ListLibrary)), true));
|
||||||
|
|
||||||
|
UserDataTypeHandler.LoadType<StringLibrary>();
|
||||||
|
funcs.Add("string", new StringLibrary().ToScriptType());
|
||||||
|
boundFuncs.Add("string",
|
||||||
|
new UserDataVariableSymbol("string", BoundTypeHandler.GetTypeDefinition(typeof(StringLibrary)), true));
|
||||||
|
|
||||||
var scope = new EvaluationScope(funcs);
|
var scope = new EvaluationScope(funcs);
|
||||||
var boundScope = new BoundScope(boundFuncs, null);
|
var boundScope = new BoundScope(boundFuncs, null);
|
||||||
return (scope, boundScope);
|
return (scope, boundScope);
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
using System;
|
||||||
|
using Upsilon.BaseTypes;
|
||||||
|
using Upsilon.BaseTypes.Number;
|
||||||
|
|
||||||
|
namespace Upsilon.StandardLibraries
|
||||||
|
{
|
||||||
|
internal class StringLibrary
|
||||||
|
{
|
||||||
|
[ScriptFunction("byte", "Get the byte value of the first character of a string", directScriptManipulation: true)]
|
||||||
|
public ScriptNumber Byte(ScriptString str)
|
||||||
|
{
|
||||||
|
return new ScriptNumberLong(str.Value[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("byte", "Get the byte value of the first character of a string", directScriptManipulation: true)]
|
||||||
|
public ScriptNumber Byte(ScriptString str, ScriptNumberLong index)
|
||||||
|
{
|
||||||
|
if (index < 1 || index > str.Value.Length)
|
||||||
|
return null;
|
||||||
|
var c = str.Value[(int) (index - 1)];
|
||||||
|
return new ScriptNumberLong(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("len", "Return the length of the string passed.", directScriptManipulation: true)]
|
||||||
|
public ScriptNumber Length(ScriptString str)
|
||||||
|
{
|
||||||
|
return str.Length();
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("lower", "Make uppercase characters lower case.", directScriptManipulation: true)]
|
||||||
|
public ScriptString Lower(ScriptString str)
|
||||||
|
{
|
||||||
|
return new ScriptString(str.Value.ToLowerInvariant());
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("reverse", "Reverses a string.", directScriptManipulation: true)]
|
||||||
|
public ScriptString Reverse(ScriptString str)
|
||||||
|
{
|
||||||
|
var cArr = str.Value.ToCharArray();
|
||||||
|
Array.Reverse(cArr);
|
||||||
|
return new ScriptString(new string(cArr));
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("sub", "Make all the lower case characters upper case.", directScriptManipulation: true)]
|
||||||
|
public ScriptString Substring(ScriptString str, ScriptNumberLong startIndex, ScriptNumberLong endIndex = null)
|
||||||
|
{
|
||||||
|
var s = str.Value;
|
||||||
|
var start = startIndex.Value;
|
||||||
|
if (start < 0)
|
||||||
|
start = s.Length + start;
|
||||||
|
if (endIndex == null)
|
||||||
|
{
|
||||||
|
return new ScriptString(str.Value.Substring((int) start));
|
||||||
|
}
|
||||||
|
var end = endIndex.Value;
|
||||||
|
if (end < 0)
|
||||||
|
end = s.Length + end;
|
||||||
|
var length = end - start;
|
||||||
|
return new ScriptString(str.Value.Substring((int) start, (int) length));
|
||||||
|
}
|
||||||
|
|
||||||
|
[ScriptFunction("upper", "Make all the lower case characters upper case.", directScriptManipulation: true)]
|
||||||
|
public ScriptString Upper(ScriptString str)
|
||||||
|
{
|
||||||
|
return new ScriptString(str.Value.ToUpperInvariant());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
|
||||||
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
|
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using Upsilon;
|
using Upsilon;
|
||||||
using Upsilon.Evaluator;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace UpsilonTests.StandardLibraryTests
|
namespace UpsilonTests.StandardLibraryTests
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
using Upsilon;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace UpsilonTests.StandardLibraryTests
|
||||||
|
{
|
||||||
|
public class StringLibraryTests : TestClass
|
||||||
|
{
|
||||||
|
public StringLibraryTests(StaticScriptFixture fix) : base(fix)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ByteUse()
|
||||||
|
{
|
||||||
|
var result = Executor.EvaluateScript<long>(@"string.byte(""a"")", Options);
|
||||||
|
Assert.Equal('a', result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ByteUseIndex()
|
||||||
|
{
|
||||||
|
var result = Executor.EvaluateScript<long>(@"string.byte(""abcd"", 3)", Options);
|
||||||
|
Assert.Equal('c', result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue