Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some additional utility methods and tests

  • Loading branch information...
commit c6d121450b3f7b8a60b811b1fb887ebb554b5092 1 parent 2d9ce2f
@tabish121 tabish121 authored
View
44 activemq-cpp/src/main/decaf/lang/String.cpp
@@ -785,6 +785,50 @@ bool String::equalsIgnoreCase(const char* string) const {
}
////////////////////////////////////////////////////////////////////////////////
+int String::findFirstOf(const String& chars) const {
+ return findFirstOf(chars, 0);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int String::findFirstOf(const String& chars, int start) const {
+ if (start < contents->length) {
+ if (start < 0) {
+ start = 0;
+ }
+
+ for (int i = contents->offset + start; i < contents->offset + contents->length; i++) {
+ char c = contents->value[i];
+ if (chars.indexOf(c) != -1) {
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int String::findFirstNotOf(const String& chars) const {
+ return findFirstNotOf(chars, 0);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int String::findFirstNotOf(const String& chars, int start) const {
+ if (start < contents->length) {
+ if (start < 0) {
+ start = 0;
+ }
+
+ for (int i = contents->offset + start; i < contents->offset + contents->length; i++) {
+ char c = contents->value[i];
+ if (chars.indexOf(c) == -1) {
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
int String::hashCode() const {
if (contents->hashCode == 0) {
View
56 activemq-cpp/src/main/decaf/lang/String.h
@@ -534,6 +534,62 @@ namespace lang {
bool equalsIgnoreCase(const char* string) const;
/**
+ * Searches in this string for the first index of any character in the specified
+ * String. The search for the matching characters starts at the beginning and moves
+ * towards the end of this string.
+ *
+ * @param string
+ * the characters to find the first of within this String.
+ *
+ * @return the index of the first character of the specified string in this
+ * string, -1 if none of the characters in the String exist in this String.
+ */
+ int findFirstOf(const String& chars) const;
+
+ /**
+ * Searches in this string for the first index of any character in the specified
+ * String. The search for the matching characters starts at the given index and moves
+ * towards the end of this string.
+ *
+ * @param chars
+ * the characters to find the first of within this String.
+ * @param start
+ * the starting offset.
+ *
+ * @return the index of the first character of the specified string in this
+ * string, -1 if none of the characters in the String exist in this String.
+ */
+ int findFirstOf(const String& chars, int start) const;
+
+ /**
+ * Searches in this string for the first index of any character that is not in the
+ * specified String. The search for the non-matching characters starts at the beginning
+ * and moves towards the end of this string.
+ *
+ * @param chars
+ * the characters to find the first non-matching index of within this String.
+ *
+ * @return the index of the first character not in the specified string in this
+ * string, -1 if all of the characters in the given String exist in this String.
+ */
+ int findFirstNotOf(const String& chars) const;
+
+ /**
+ * Searches in this string for the first index of any character that is not in the
+ * specified String. The search for the non-matching characters starts at the given index
+ * and moves towards the end of this string.
+ *
+ * @param chars
+ * the characters to find the first non-matching index of within this String.
+ * @param start
+ * the starting offset.
+ *
+ * @return the index of the first character not in the specified string in this
+ * string, -1 if all of the characters in the given String exist in this String.
+ */
+ int findFirstNotOf(const String& chars, int start) const;
+
+ /**
* Returns a hash code for this String instance, the hash code for an empty
* String will always be zero.
*
View
40 activemq-cpp/src/test/decaf/lang/StringTest.cpp
@@ -540,6 +540,46 @@ void StringTest::testEqualsIgnoreCaseStdString() {
}
////////////////////////////////////////////////////////////////////////////////
+void StringTest::testFindFirstOf() {
+
+ const String input("HelloWorld");
+
+ CPPUNIT_ASSERT_EQUAL(0, input.findFirstOf("H"));
+ CPPUNIT_ASSERT_EQUAL(-1, input.findFirstOf("z"));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void StringTest::testFindFirstOf2() {
+
+ const String input("HelloWorld");
+
+ CPPUNIT_ASSERT_EQUAL(0, input.findFirstOf("H", 0));
+ CPPUNIT_ASSERT_EQUAL(0, input.findFirstOf("H", -1));
+ CPPUNIT_ASSERT_EQUAL(-1, input.findFirstOf("H", 1));
+ CPPUNIT_ASSERT_EQUAL(-1, input.findFirstOf("H", 25));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void StringTest::testFindFirstNotOf() {
+
+ const String input("HelloWorld");
+
+ CPPUNIT_ASSERT_EQUAL(1, input.findFirstNotOf("H"));
+ CPPUNIT_ASSERT_EQUAL(0, input.findFirstNotOf("z"));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void StringTest::testFindFirstNotOf2() {
+
+ const String input("HelloWorld");
+
+ CPPUNIT_ASSERT_EQUAL(5, input.findFirstNotOf("Hello", 5));
+ CPPUNIT_ASSERT_EQUAL(0, input.findFirstNotOf("z", -1));
+ CPPUNIT_ASSERT_EQUAL(1, input.findFirstNotOf("H", 1));
+ CPPUNIT_ASSERT_EQUAL(-1, input.findFirstNotOf("H", 25));
+}
+
+////////////////////////////////////////////////////////////////////////////////
void StringTest::testIndexOfChar() {
const String input("HelloWorld");
View
8 activemq-cpp/src/test/decaf/lang/StringTest.h
@@ -97,6 +97,10 @@ namespace lang {
CPPUNIT_TEST( testOperatorPlusString );
CPPUNIT_TEST( testOperatorPlusStdString );
CPPUNIT_TEST( testOperatorPlusCString );
+ CPPUNIT_TEST( testFindFirstOf );
+ CPPUNIT_TEST( testFindFirstOf2 );
+ CPPUNIT_TEST( testFindFirstNotOf );
+ CPPUNIT_TEST( testFindFirstNotOf2 );
CPPUNIT_TEST_SUITE_END();
public:
@@ -137,6 +141,10 @@ namespace lang {
void testIndexOfChar2();
void testIndexOfString();
void testIndexOfString2();
+ void testFindFirstOf();
+ void testFindFirstOf2();
+ void testFindFirstNotOf();
+ void testFindFirstNotOf2();
void testIndexOfStdString();
void testIndexOfStdString2();
void testIndexOfCString();
Please sign in to comment.
Something went wrong with that request. Please try again.