Skip to content

Commit

Permalink
feat: Add Java 11 String methods to JPF String class (#512)
Browse files Browse the repository at this point in the history
* feat: add java 11 string methods to JPF string class

* feat: added lines method to string class

* fix: test case fix

* fix: remove stream import

---------

Co-authored-by: sjain39 <suyash_jain@intuit.com>
  • Loading branch information
gitkaarma and sjain39 authored Jan 30, 2025
1 parent e8429a3 commit 9a1c3c8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/classes/modules/java.base/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,5 +501,60 @@ static void checkIndex(int index, int length) {
}
}

public boolean isBlank() {
return isEmpty() || strip().isEmpty();
}

public String strip() {
return stripLeading().stripTrailing();
}

public String stripLeading() {
int start = 0;
while (start < value.length && Character.isWhitespace(charAt(start))) {
start++;
}
return substring(start);
}

public String stripTrailing() {
int end = value.length;
while (end > 0 && Character.isWhitespace(charAt(end - 1))) {
end--;
}
return substring(0, end);
}

public String repeat(int count) {
if (count < 0) {
throw new IllegalArgumentException("count is negative: " + count);
}
if (count == 1 || isEmpty()) {
return this;
}
final int len = value.length;
if (len == 0 || count == 0) {
return "";
}
if (len == 1) {
final byte[] single = new byte[count];
Arrays.fill(single, value[0]);
return new String(single, coder);
}
final int limit = Integer.MAX_VALUE / count;
if (len > limit) {
throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
" times will produce a String exceeding maximum size.");
}
final int newLength = len * count;
final byte[] multiple = new byte[newLength];
System.arraycopy(value, 0, multiple, 0, len);
int copied = len;
for (; copied < newLength - copied; copied <<= 1) {
System.arraycopy(multiple, 0, multiple, copied, copied);
}
System.arraycopy(multiple, 0, multiple, copied, newLength - copied);
return new String(multiple, coder);
}
}

50 changes: 50 additions & 0 deletions src/tests/gov/nasa/jpf/test/java/lang/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.junit.Test;

Expand Down Expand Up @@ -310,4 +312,52 @@ public void testConcat() {
assert out.equals("Hello, World!");
}
}

@Test
public void testIsBlank() {
if (verifyNoPropertyViolation()) {
assertTrue("".isBlank());
assertTrue(" ".isBlank());
assertTrue("\t\n\r".isBlank());
assertFalse(" a ".isBlank());
assertFalse("abc".isBlank());
}
}

@Test
public void testStrip() {
if (verifyNoPropertyViolation()) {
assertEquals("abc", " abc ".strip());
assertEquals("abc", "abc".strip());
assertEquals("", " ".strip());
}
}

@Test
public void testStripLeading() {
if (verifyNoPropertyViolation()) {
assertEquals("abc ", " abc ".stripLeading());
assertEquals("abc", "abc".stripLeading());
assertEquals("", " ".stripLeading());
}
}

@Test
public void testStripTrailing() {
if (verifyNoPropertyViolation()) {
assertEquals(" abc", " abc ".stripTrailing());
assertEquals("abc", "abc".stripTrailing());
assertEquals("", " ".stripTrailing());
}
}

@Test
public void testRepeat() {
if (verifyNoPropertyViolation()) {
assertEquals("", "abc".repeat(0));
assertEquals("abc", "abc".repeat(1));
assertEquals("abcabc", "abc".repeat(2));
assertEquals(" ", " ".repeat(2));
}
}
}

0 comments on commit 9a1c3c8

Please sign in to comment.