Skip to content

Commit c6ddad3

Browse files
committed
Fix variation of #33 "The start position cannot exceed initial string length"
1 parent 3f09892 commit c6ddad3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/main/php/lang/types/String.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function startsWith($arg) {
273273
public function endsWith($arg) {
274274
$bytes= $this->asIntern($arg);
275275
$l= strlen($bytes);
276-
return $l > 0 && 0 === substr_compare($this->buffer, $bytes, -$l, $l);
276+
return $l > 0 && $l <= $this->length && 0 === substr_compare($this->buffer, $bytes, -$l, $l);
277277
}
278278

279279
/**

src/test/php/net/xp_framework/unittest/core/types/StringTest.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public function does_not_start_with_empty_string() {
158158
$this->assertFalse((new String('test'))->startsWith(''));
159159
}
160160

161+
#[@test, @values(['', 'test'])]
162+
public function empty_string_does_not_start_with_anything($value) {
163+
$this->assertFalse(String::$EMPTY->startsWith($value));
164+
}
165+
161166
#[@test]
162167
public function endsWith() {
163168
$str= new String('www.müller.com');
@@ -172,6 +177,11 @@ public function does_not_end_with_empty_string() {
172177
$this->assertFalse((new String('test'))->endsWith(''));
173178
}
174179

180+
#[@test, @values(['', 'test'])]
181+
public function empty_string_does_not_end_with_anything($value) {
182+
$this->assertFalse(String::$EMPTY->endsWith($value));
183+
}
184+
175185
#[@test]
176186
public function concat() {
177187
$this->assertEquals(new String('www.müller.com'), (new String('www'))

0 commit comments

Comments
 (0)