Skip to content

Commit

Permalink
Merge 68cd345 on remote branch
Browse files Browse the repository at this point in the history
Change-Id: I8596bd5db357e61a44d68f314582009ccdff380f
  • Loading branch information
Linux Build Service Account committed Aug 15, 2023
2 parents be824c1 + 68cd345 commit e79bd01
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

import static java.util.Arrays.asList;

import android.compat.Compatibility;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

public class PatternTest extends TestCaseWithRules {
@Rule
public TestRule switchTargetSdkVersionRule = SwitchTargetSdkVersionRule.getInstance();
Expand Down Expand Up @@ -2199,21 +2204,37 @@ public void testSplitAsStream() {

pat = Pattern.compile("b");
s = pat.splitAsStream("").toArray(String[]::new);
// The length is 1 because the javadoc says "If this pattern does not match any subsequence
// of the input then the resulting stream has just one element, namely the input sequence
// in string form.
assertEquals(1, s.length);
assertEquals(s[0], "");
assertEquals(getExpectedEmptyStringSplitLength(), s.length);
checkContainsOnlyEmptyStrings(s);

pat = Pattern.compile("");
s = pat.splitAsStream("").toArray(String[]::new);
assertEquals(1, s.length);
assertEquals(s[0], "");
assertEquals(getExpectedEmptyStringSplitLength(), s.length);
checkContainsOnlyEmptyStrings(s);

pat = Pattern.compile("");
s = pat.splitAsStream("abccbadfe").toArray(String[]::new);
assertEquals(9, s.length);
assertEquals(s[0], "a");
assertEquals(s[8], "e");
}

private int getExpectedEmptyStringSplitLength() {
if (VMRuntime.getSdkVersion() >= VersionCodes.UPSIDE_DOWN_CAKE
&& Compatibility.isChangeEnabled(
Pattern.SPLIT_AS_STREAM_RETURNS_SINGLE_EMPTY_STRING)) {
// The length is 1 because the javadoc says "If this pattern does not match any
// subsequence of the input then the resulting stream has just one element,
// namely the input sequence in string form.
return 1;
} else {
return 0;
}
}

private void checkContainsOnlyEmptyStrings(String[] sArray) {
for (String s : sArray) {
assertEquals(s, "");
}
}
}
32 changes: 31 additions & 1 deletion ojluni/src/main/java/java/util/regex/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

package java.util.regex;

import android.compat.Compatibility;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;

import com.android.icu.util.regex.PatternNative;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -5731,7 +5737,16 @@ public boolean hasNext() {
// If the input is an empty string then the result can only be a
// stream of the input. Induce that by setting the empty
// element count to 1
emptyElementCount = input.length() == 0 ? 1 : 0;
// Android-changed: Keep old behavior on Android 13 or below. http://b/286499139
// emptyElementCount = input.length() == 0 ? 1 : 0;
if (input.length() == 0
&& VMRuntime.getSdkVersion() >= VersionCodes.UPSIDE_DOWN_CAKE
&& Compatibility.isChangeEnabled(
SPLIT_AS_STREAM_RETURNS_SINGLE_EMPTY_STRING)) {
emptyElementCount = 1;
} else {
emptyElementCount = 0;
}
}
if (nextElement != null || emptyElementCount > 0)
return true;
Expand Down Expand Up @@ -5768,4 +5783,19 @@ public boolean hasNext() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
new MatcherIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false);
}

// Android-added: Backward-compatible flag for splitAsStream() API.
/**
* Since Android 14, {@link Pattern#splitAsStream(CharSequence)} return a stream of a single
* empty String as described in the API documentation. Previously, given an empty string input,
* the method returns an empty stream.
*
* This flag is enabled for apps targeting Android 14+.
*
* @hide
*/
@ChangeId
@EnabledSince(targetSdkVersion = VersionCodes.UPSIDE_DOWN_CAKE)
public static final long SPLIT_AS_STREAM_RETURNS_SINGLE_EMPTY_STRING = 288845345L;

}
16 changes: 16 additions & 0 deletions ojluni/src/test/java/util/regex/PatternStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -52,6 +53,13 @@

import static org.testng.Assert.*;

import static java.util.regex.Pattern.SPLIT_AS_STREAM_RETURNS_SINGLE_EMPTY_STRING;

import android.compat.Compatibility;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

@Test
public class PatternStreamTest extends OpTestCase {

Expand Down Expand Up @@ -139,6 +147,14 @@ public void testPatternSplitAsStream(String description, String input, Pattern p
// Derive expected result from pattern.split
List<String> expected = Arrays.asList(pattern.split(input));

// Android-added: Keep old behavior on Android 13 or below. http://b/286499139
if(input.isEmpty()
&& !(VMRuntime.getSdkVersion() >= VersionCodes.UPSIDE_DOWN_CAKE
&& Compatibility.isChangeEnabled(
Pattern.SPLIT_AS_STREAM_RETURNS_SINGLE_EMPTY_STRING))) {
expected = Collections.emptyList();
}

Supplier<Stream<String>> ss = () -> pattern.splitAsStream(input);
withData(TestData.Factory.ofSupplier(description, ss))
.stream(LambdaTestHelpers.identity())
Expand Down

0 comments on commit e79bd01

Please sign in to comment.