Skip to content

Commit

Permalink
Various compatibility fixes (#7)
Browse files Browse the repository at this point in the history
Substring length can be longer than the end of string, when that happens, go to the end of the string.

TREATASCONTENT may have empty values and that's OK.

IF statements on empty values should return false.

IF statements without clauses should check for equal to true
  • Loading branch information
lbuesching authored Sep 19, 2023
1 parent a933a92 commit f9ed42f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Sage.Engine.Tests/Corpus/Function/substring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ set @RESULT = Substring('Logan',1.5)
%%=v(@RESULT)=%%
++++++++++
!
==========
Substring('Logan',1,50)
==========
%%[
set @RESULT = Substring('Logan',1,50)
]%%
%%=v(@RESULT)=%%
++++++++++
Logan
7 changes: 6 additions & 1 deletion src/Sage.Engine.Tests/Corpus/Function/treatascontent.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ TREATASCONTENTAREA
==========
%%= TREATASCONTENTAREA("Content",CONCAT("%", "%", "=ADD(1,2)=", "%", "%")) =%%
++++++++++
3
3
==========
Empty TREATASCONTENT
==========
%%=TREATASCONTENT("")=%%
++++++++++
15 changes: 15 additions & 0 deletions src/Sage.Engine.Tests/Corpus/Language/if.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
===========
NULL IF CHECK
===========
%%[
VAR @variable

IF @variable THEN
OUTPUT("1")
ENDIF
IF NOT @variable THEN
OUTPUT("2")
ENDIF
]%%
++++++++++
2
3 changes: 3 additions & 0 deletions src/Sage.Engine.Tests/Sage.Engine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<None Update="Corpus\Language\assignment.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Corpus\Language\if.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Corpus\Language\for.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
5 changes: 5 additions & 0 deletions src/Sage.Engine/Runtime/Functions/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public string CONTENTBLOCKBYKEY(object contentBlockKey, object? impressionRegion
public string TREATASCONTENT(object? content)
{
string contentString = content?.ToString() ?? string.Empty;

if (string.IsNullOrWhiteSpace(contentString))
{
return contentString;
}

return CompileAndExecuteEmbeddedCodeAsync($"treatascontent__{_stackFrame.Peek().CurrentLineNumber}", contentString) ?? string.Empty;
}
Expand Down
14 changes: 13 additions & 1 deletion src/Sage.Engine/Runtime/Functions/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,19 @@ public string SUBSTRING(object subject, object start, object? length = null)

int lengthInt = SageValue.ToInt(length);

return subject?.ToString()?.Substring(startInt - 1, lengthInt) ?? string.Empty;
string subjectToCheck = subject?.ToString() ?? string.Empty;

if (string.IsNullOrEmpty(subjectToCheck))
{
return string.Empty;
}

if (lengthInt > subjectToCheck.Length - startInt)
{
lengthInt = (subjectToCheck.Length - startInt + 1);
}

return subjectToCheck.Substring(startInt - 1, lengthInt);
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Sage.Engine/Runtime/SageValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ public static DateTimeOffset ToDateTime(object? inputObj, IFormatProvider? forma
public static UnboxResult TryToBoolean(object? inputObj, out bool result)
{
inputObj = UnboxVariable(inputObj);

if (inputObj == null)
{
result = false;
return UnboxResult.Succeed;
}

if (inputObj is bool boolObj)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Sage.Engine/Runtime/SageVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public SageVariable(string name)
this.Name = name;
}

public static implicit operator bool(SageVariable other)
{
return SageValue.EqualTo(true, other);
}

public override string? ToString()
{
return Value?.ToString();
Expand Down

0 comments on commit f9ed42f

Please sign in to comment.