Skip to content

Commit

Permalink
Move contains to TextValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Oct 5, 2018
1 parent 2940f03 commit f88006b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
Expand Up @@ -98,7 +98,7 @@ void shouldTrim()
}

@Test
void shouldHandleStartsWithAndEndsWith()
void shouldHandleStringPredicates()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
Expand All @@ -115,6 +115,7 @@ void shouldHandleStartsWithAndEndsWith()

assertConsistent( value, other, TextValue::startsWith );
assertConsistent( value, other, TextValue::endsWith );
assertConsistent( value, other, TextValue::contains );
}
}

Expand Down
Expand Up @@ -211,6 +211,12 @@ public boolean endsWith( TextValue other )
return startsWith( other );
}

@Override
public boolean contains( TextValue other )
{
return startsWith( other );
}

public char value()
{
return value;
Expand Down
Expand Up @@ -245,6 +245,12 @@ public boolean endsWith( TextValue other )
return other.length() == 0;
}

@Override
public boolean contains( TextValue other )
{
return other.length() == 0;
}

@Override
public TextValue toLower()
{
Expand Down
Expand Up @@ -153,6 +153,12 @@ public boolean endsWith( TextValue other )
return value.endsWith( other.stringValue() );
}

@Override
public boolean contains( TextValue other )
{
return value.contains( other.stringValue() );
}

@Override
Matcher matcher( Pattern pattern )
{
Expand Down
Expand Up @@ -74,6 +74,8 @@ public TextValue substring( int start )

public abstract boolean endsWith( TextValue other );

public abstract boolean contains( TextValue other );

public abstract int compareTo( TextValue other );

@Override
Expand Down
Expand Up @@ -363,6 +363,55 @@ public boolean endsWith( TextValue other )
return value().endsWith( other.stringValue() );
}

@Override
public boolean contains( TextValue other )
{

if ( other instanceof UTF8StringValue )
{
final UTF8StringValue substring = (UTF8StringValue) other;
if ( byteLength == 0 )
{
return substring.byteLength == 0;
}
if ( substring.byteLength == 0 )
{
return true;
}
if ( substring.byteLength > byteLength )
{
return false;
}

final byte first = substring.bytes[substring.offset];
int max = offset + byteLength - substring.byteLength;
for ( int pos = offset; pos <= max; pos++ )
{
//find first byte
if ( bytes[pos] != first )
{
while ( ++pos <= max && bytes[pos] != first );
}

//Now we have the first byte match, look at the rest
if ( pos <= max )
{
int i = pos + 1;
final int end = pos + substring.byteLength;
for ( int j = substring.offset + 1; i < end && bytes[i] == substring.bytes[j]; j++, i++);

if ( i == end )
{
return true;
}
}
}
return false;
}

return value().contains( other.stringValue() );
}

private boolean startsWith(UTF8StringValue prefix, int startPos)
{
int thisOffset = offset+ startPos;
Expand Down
Expand Up @@ -230,7 +230,7 @@ void shouldThrowOnNegativeLength()
}

@Test
void shouldHandleStartsWithAndEndsWithWhenOffset()
void shouldHandleStringPredicatesWithOffset()
{
// Given
byte[] bytes = "abcdefghijklmnoprstuvxyzABCDEFGHIJKLMNOPRSTUVXYZ".getBytes( UTF_8 );
Expand All @@ -249,11 +249,15 @@ void shouldHandleStartsWithAndEndsWithWhenOffset()
assertThat( value.startsWith( other ),
equalTo( otherLength == 0 || otherOffset == offset && otherLength <= length ) );
assertThat( value.endsWith( other ),
equalTo( otherLength == 0 || otherOffset >= offset && otherLength == length + offset - otherOffset ) );
equalTo( otherLength == 0 ||
otherOffset >= offset && otherLength == length + offset - otherOffset ) );
assertThat( value.contains( other ),
equalTo( otherLength == 0 ||
otherOffset >= offset && otherLength <= length + offset - otherOffset ) );

}
}


}
}
}
Expand Down

0 comments on commit f88006b

Please sign in to comment.