Implement String.contains and associated test. Also fix typo in StringTest.
Issue: 1729
Patch by: rovrevik (minor touchups by jat)
Review by: jat, tobyr (desk review)
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1733 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/String.java b/user/super/com/google/gwt/emul/java/lang/String.java
index f238537..5ab0153 100644
--- a/user/super/com/google/gwt/emul/java/lang/String.java
+++ b/user/super/com/google/gwt/emul/java/lang/String.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -83,34 +83,6 @@
// CHECKSTYLE_OFF: This class has special needs.
/**
- * @skip
- */
- static String _String() {
- return "";
- }
-
- /**
- * @skip
- */
- static String _String(char value[]) {
- return valueOf(value);
- }
-
- /**
- * @skip
- */
- static String _String(char value[], int offset, int count) {
- return valueOf(value, offset, count);
- }
-
- /**
- * @skip
- */
- static String _String(String other) {
- return other;
- }
-
- /**
* Checks that bounds are correct.
*
* @param legalCount the end of the legal range
@@ -158,6 +130,34 @@
return replaceStr;
}
+ /**
+ * @skip
+ */
+ static String _String() {
+ return "";
+ }
+
+ /**
+ * @skip
+ */
+ static String _String(char value[]) {
+ return valueOf(value);
+ }
+
+ /**
+ * @skip
+ */
+ static String _String(char value[], int offset, int count) {
+ return valueOf(value, offset, count);
+ }
+
+ /**
+ * @skip
+ */
+ static String _String(String other) {
+ return other;
+ }
+
private static native boolean __equals(String me, Object other) /*-{
// Coerce me to a primitive string to force string comparison
return String(me) == other;
@@ -217,6 +217,10 @@
return this + str;
}-*/;
+ public boolean contains(CharSequence s) {
+ return indexOf(s.toString()) != -1;
+ }
+
public native boolean endsWith(String suffix) /*-{
return (this.lastIndexOf(suffix) != -1)
&& (this.lastIndexOf(suffix) == (this.length - suffix.length));
diff --git a/user/test/com/google/gwt/emultest/java/lang/StringTest.java b/user/test/com/google/gwt/emultest/java/lang/StringTest.java
index d765091..67d874d 100644
--- a/user/test/com/google/gwt/emultest/java/lang/StringTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/StringTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -45,7 +45,7 @@
assertEquals("abcd", s);
}
- public void testContructor() {
+ public void testConstructor() {
char[] chars = {'a', 'b', 'c', 'd', 'e', 'f'};
String constant = "abcdef";
String shortString = "cde";
@@ -57,6 +57,24 @@
assertEquals(new String(new char[] {}), "");
}
+ public void testContains() {
+ // at the beginning
+ assertTrue("abcdef".contains("ab"));
+ assertTrue("abcdef".contains(new StringBuffer("ab")));
+ // at the end
+ assertTrue("abcdef".contains("ef"));
+ assertTrue("abcdef".contains(new StringBuffer("ef")));
+ // in the middle
+ assertTrue("abcdef".contains("cd"));
+ assertTrue("abcdef".contains(new StringBuffer("cd")));
+ // the same
+ assertTrue("abcdef".contains("abcdef"));
+ assertTrue("abcdef".contains(new StringBuffer("abcdef")));
+ // not present
+ assertFalse("abcdef".contains("z"));
+ assertFalse("abcdef".contains(new StringBuffer("z")));
+ }
+
public void testEndsWith() {
String haystack = "abcdefghi";
assertTrue("a", haystack.endsWith("defghi"));