Skip to content

Commit

Permalink
Move endsWith to TextValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Oct 5, 2018
1 parent 32ffcae commit 2940f03
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,23 @@ void shouldTrim()
}

@Test
void shouldStartsWith()
void shouldHandleStartsWithAndEndsWith()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
String value = random.nextString();
String prefix;
String other;
if ( random.nextBoolean() )
{
prefix = value;
other = value;
}
else
{
prefix = random.nextString();
other = random.nextString();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public boolean startsWith( TextValue other )
return other.length() == 1 && other.stringValue().charAt( 0 ) == value;
}

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

public char value()
{
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public boolean startsWith( TextValue other )
return other.length() == 0;
}

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

@Override
public TextValue toLower()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public boolean startsWith( TextValue other )
return value.startsWith( other.stringValue() );
}

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

@Override
Matcher matcher( Pattern pattern )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public TextValue substring( int start )

public abstract boolean startsWith( TextValue other );

public abstract boolean endsWith( TextValue other );

public abstract int compareTo( TextValue other );

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,26 +343,44 @@ public boolean startsWith( TextValue other )

if ( other instanceof UTF8StringValue )
{
int thisOffset = offset;
UTF8StringValue otherUtf8Value = (UTF8StringValue) other;
int prefixOffset = otherUtf8Value.offset;
int prefixCount = otherUtf8Value.byteLength;
if (prefixCount > byteLength)
{
return false;
}
UTF8StringValue suffix = (UTF8StringValue) other;
return startsWith( suffix, 0 );
}

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

@Override
public boolean endsWith( TextValue other )
{

if ( other instanceof UTF8StringValue )
{
UTF8StringValue suffix = (UTF8StringValue) other;
return startsWith( suffix, byteLength - suffix.byteLength );
}

while ( --prefixCount >= 0 )
return value().endsWith( other.stringValue() );
}

private boolean startsWith(UTF8StringValue prefix, int startPos)
{
int thisOffset = offset+ startPos;
int prefixOffset = prefix.offset;
int prefixCount = prefix.byteLength;
if (startPos < 0 || prefixCount > byteLength)
{
return false;
}

while ( --prefixCount >= 0 )
{
if ( bytes[thisOffset++] != prefix.bytes[prefixOffset++] )
{
if ( bytes[thisOffset++] != otherUtf8Value.bytes[prefixOffset++] )
{
return false;
}
return false;
}
return true;
}

return value().startsWith( other.stringValue() );
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ void shouldThrowOnNegativeLength()
assertThrows( IndexOutOfBoundsException.class, () -> value.substring( 4, -3 ) );
}


@Test
void shouldHandleStartsWithAndOffset()
void shouldHandleStartsWithAndEndsWithWhenOffset()
{
// Given
byte[] bytes = "abcdefghijklmnoprstuvxyzABCDEFGHIJKLMNOPRSTUVXYZ".getBytes( UTF_8 );
Expand All @@ -242,13 +241,15 @@ void shouldHandleStartsWithAndOffset()
{
TextValue value = utf8Value( bytes, offset, length );

for ( int prefixOffset = 0; prefixOffset <= bytes.length; prefixOffset++ )
for ( int otherOffset = 0; otherOffset <= bytes.length; otherOffset++ )
{
for ( int prefixLength = 0; prefixLength < bytes.length - prefixOffset; prefixLength++ )
for ( int otherLength = 0; otherLength < bytes.length - otherOffset; otherLength++ )
{
TextValue prefix = utf8Value( bytes, prefixOffset, prefixLength );
assertThat( value.startsWith( prefix ),
equalTo( prefixLength == 0 || prefixOffset == offset && prefixLength <= length ) );
TextValue other = utf8Value( bytes, otherOffset, otherLength );
assertThat( value.startsWith( other ),
equalTo( otherLength == 0 || otherOffset == offset && otherLength <= length ) );
assertThat( value.endsWith( other ),
equalTo( otherLength == 0 || otherOffset >= offset && otherLength == length + offset - otherOffset ) );
}
}

Expand Down

0 comments on commit 2940f03

Please sign in to comment.