Skip to content

Commit

Permalink
Added LastCharacter method to YString which returns a slice of ju…
Browse files Browse the repository at this point in the history
…st the last character of a string
  • Loading branch information
martindevans committed Feb 24, 2021
1 parent 7284605 commit 8863a67
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Yolol.Analysis/Yolol.Analysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>6.2.3</Version>
<Version>6.2.4</Version>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
20 changes: 18 additions & 2 deletions Yolol/Execution/Rope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private RopeSlice(Rope rope, int start, int length)
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "length < 0");
if (start + length > rope.Length)
throw new ArgumentOutOfRangeException(nameof(length), "start + length >= rope.Length");
throw new ArgumentOutOfRangeException(nameof(length), "start + length > rope.Length");
#endif
}

Expand Down Expand Up @@ -201,6 +201,16 @@ public int CompareTo(in Span<char> other)
return Length - other.Length;
}

public RopeSlice PopLast()
{
if (_rope == null)
throw new InvalidOperationException("Attempted to `PopLast` on null `RopeSlice`");
if (Length < 1)
throw new InvalidOperationException("Attempted to `PopLast` on empty `RopeSlice`");

return new RopeSlice(_rope, _start + Length - 1, 1);
}

public static RopeSlice Concat(in RopeSlice left, in RopeSlice right)
{
// If either part of the concat is an empty string (represented by a null rope) return the other half.
Expand Down Expand Up @@ -334,7 +344,13 @@ private static RopeSlice Remove(in RopeSlice haystack, int startIndex, int needl

// If left slice _starts_ with the right string we can just offset the start of the slice
if (startIndex == 0)
return new RopeSlice(haystack._rope, haystack._start + needleLength, haystack.Length - needleLength);
{
var start = haystack._start + needleLength;
var length = haystack.Length - needleLength;
if (length == 0)
start = 0;
return new RopeSlice(haystack._rope, start, length);
}

// We'll have to make a new rope and remove it
var rope = new Rope(haystack.Length);
Expand Down
1 change: 0 additions & 1 deletion Yolol/Execution/Value.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using Yolol.Execution.Attributes;
using Yolol.Grammar.AST.Expressions;

Expand Down
10 changes: 10 additions & 0 deletions Yolol/Execution/YString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,15 @@ public StaticError Sqrt()
{
throw new ExecutionException("Attempted to sqrt a string");
}


[ErrorMetadata(nameof(WillDecThrow))]
public YString LastCharacter()
{
if (Length == 0)
throw new ExecutionException("Attempted to decrement empty string");

return new YString(_span.PopLast());
}
}
}
2 changes: 1 addition & 1 deletion Yolol/Yolol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/martindevans/Yolol</PackageProjectUrl>
<RepositoryUrl>https://github.com/martindevans/Yolol</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>11.4.1</Version>
<Version>11.5.0</Version>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
22 changes: 22 additions & 0 deletions YololEmulator.Tests/Expressions/Str/Decrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,27 @@ public void Empty()
TestExecutor.Execute("a = \"\"", "b = --a");
});
}

[TestMethod]
public void Pop()
{
var result = TestExecutor.Execute("a = \"abcdefgh\"", "b = a---a");

var a = result.GetVariable("a");
var b = result.GetVariable("b");

Assert.AreEqual("abcdefg", a.Value.String.ToString());
Assert.AreEqual("h", b.Value.String.ToString());
}

[TestMethod]
public void DecrementToEmpty()
{
var result = TestExecutor.Execute("a = \"a\" a--");

var a = result.GetVariable("a");

Assert.AreEqual("", a.Value.String.ToString());
}
}
}
12 changes: 12 additions & 0 deletions YololEmulator.Tests/Expressions/Str/Subtraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,17 @@ public void RemoveMidCharacter()

Assert.AreEqual("ac", a.Value.String.ToString());
}

[TestMethod]
public void SubtractAll()
{
var result = TestExecutor.Execute("a = \"abcdefgh\"", "b = a-a");

var a = result.GetVariable("a");
var b = result.GetVariable("b");

Assert.AreEqual("abcdefgh", a.Value.String.ToString());
Assert.AreEqual("", b.Value.String.ToString());
}
}
}
27 changes: 27 additions & 0 deletions YololEmulator.Tests/Scripts/LadderRepros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,32 @@ public void ZijkhalStrings()
}
}
}

[TestMethod]
public void EvilStrings()
{
var ms = TestExecutor.Execute(
"a=\"abcdef\"",
"a -= \"a\"",
"b = a---a"
);

Assert.AreEqual("bcde", ms.GetVariable("a").Value.ToString());
Assert.AreEqual("f", ms.GetVariable("b").Value.ToString());
}

[TestMethod]
public void EvilStrings2()
{
var ms = TestExecutor.Execute(
"a=\"abcxabc\"",
"b=a b-- b-- b-- b--",
"c=a-b"
);

Assert.AreEqual("abcxabc", ms.GetVariable("a").Value.ToString());
Assert.AreEqual("abc", ms.GetVariable("b").Value.ToString());
Assert.AreEqual("abcx", ms.GetVariable("c").Value.ToString());
}
}
}
7 changes: 7 additions & 0 deletions YololEmulator.Tests/YStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,12 @@ public void EmptyStringEquals()
var a = new YString("");
Assert.IsTrue(a.Equals(""));
}

[TestMethod]
public void PopLastCharacter()
{
var str = new YString("Hello");
Assert.AreEqual("o", str.LastCharacter().ToString());
}
}
}

0 comments on commit 8863a67

Please sign in to comment.