Skip to content

Commit

Permalink
Move trim to TextValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Nov 2, 2017
1 parent f00e94e commit 920c77e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ case class RTrimFunction(argument: Expression) extends StringFunction(argument)

case class TrimFunction(argument: Expression) extends StringFunction(argument) {

override def compute(value: AnyValue, m: ExecutionContext, state: QueryState): AnyValue =
Values.stringValue(asString(argument(m, state)).trim)
override def compute(value: AnyValue, m: ExecutionContext, state: QueryState): AnyValue = value match {
case NO_VALUE => NO_VALUE
case t: TextValue => t.trim()
case _ => StringFunction.notAString(value)
}

override def rewrite(f: (Expression) => Expression) = f(TrimFunction(argument.rewrite(f)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public TextValue substring( int start, int end )
return this;
}

@Override
public TextValue trim()
{
if (Character.isWhitespace( value ))
{
return StringValue.EMTPY;
}
else
{
return this;
}
}

public char value()
{
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public TextValue substring( int start, int end )
return Values.stringValue( value.substring( codePointStart, codePointEnd ) );
}

@Override
public TextValue trim()
{
return Values.stringValue( value().trim() );
}

@Override
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
{
Expand Down Expand Up @@ -119,5 +125,25 @@ public String prettyPrint()
return format( "'%s'", value() );
}

static TextValue EMTPY = new StringValue()
{
@Override
public int length()
{
return 0;
}

@Override
public TextValue trim()
{
return this;
}

@Override
String value()
{
return "";
}
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public abstract class TextValue extends ScalarValue

public abstract TextValue substring(int start, int end);

public abstract TextValue trim();

abstract int compareTo( TextValue other );

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.neo4j.internal.cypher.acceptance

import java.nio.charset.StandardCharsets

import org.neo4j.values.storable.TextValue
import org.neo4j.values.storable.Values.{stringValue, utf8Value}
import org.scalacheck.{Properties, _}

Expand All @@ -47,12 +48,16 @@ object TextValueSpecification extends Properties("TextValue") {
stringValue(x).hashCode() == utf8Value(x.getBytes(StandardCharsets.UTF_8)).hashCode()
}

property("trim") = forAll { (x: String) =>
equivalent(stringValue(x).trim(), utf8Value(x.getBytes(StandardCharsets.UTF_8)).trim())
}

property("substring") = forAll(substringGen) {
case (string, start, end) =>
val stringSubstring = stringValue(string).substring(start, end)
val utf8SubString = utf8Value(string.getBytes(StandardCharsets.UTF_8)).substring(start, end)
stringSubstring == utf8SubString &&
stringSubstring.length() == utf8SubString.length() &&
stringSubstring.hashCode() == utf8SubString.hashCode()
equivalent(stringValue(string).substring(start, end),
utf8Value(string.getBytes(StandardCharsets.UTF_8)).substring(start, end))
}

private def equivalent(t1: TextValue, t2: TextValue) =
t1.length() == t2.length() && t1 == t2 && t1.hashCode() == t2.hashCode()
}

0 comments on commit 920c77e

Please sign in to comment.