You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2010/12/29 22:23:52 UTC

[lucy-commits] svn commit: r1053744 - in /incubator/lucy/trunk/core/Lucy: Object/CharBuf.c Object/CharBuf.cfh Test/Object/TestCharBuf.c

Author: marvin
Date: Wed Dec 29 21:23:51 2010
New Revision: 1053744

URL: http://svn.apache.org/viewvc?rev=1053744&view=rev
Log:
Add Find() method to CharBuf.

Modified:
    incubator/lucy/trunk/core/Lucy/Object/CharBuf.c
    incubator/lucy/trunk/core/Lucy/Object/CharBuf.cfh
    incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c

Modified: incubator/lucy/trunk/core/Lucy/Object/CharBuf.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Object/CharBuf.c?rev=1053744&r1=1053743&r2=1053744&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Object/CharBuf.c (original)
+++ incubator/lucy/trunk/core/Lucy/Object/CharBuf.c Wed Dec 29 21:23:51 2010
@@ -624,6 +624,29 @@ CB_ends_with_str(CharBuf *self, const ch
     return false;
 }
 
+int64_t 
+CB_find(CharBuf *self, const CharBuf *substring)
+{  
+    return CB_Find_Str(self, substring->ptr, substring->size);
+}
+
+int64_t 
+CB_find_str(CharBuf *self, const char *ptr, size_t size)
+{
+    ZombieCharBuf *iterator = ZCB_WRAP(self);
+    int64_t location = 0;
+
+    while (iterator->size) {
+        if (ZCB_Starts_With_Str(iterator, ptr, size)) {
+            return location;
+        }
+        ZCB_Nip(iterator, 1);
+        location++;
+    }
+    
+    return -1;
+}
+
 uint32_t
 CB_trim(CharBuf *self)
 {

Modified: incubator/lucy/trunk/core/Lucy/Object/CharBuf.cfh
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Object/CharBuf.cfh?rev=1053744&r1=1053743&r2=1053744&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Object/CharBuf.cfh (original)
+++ incubator/lucy/trunk/core/Lucy/Object/CharBuf.cfh Wed Dec 29 21:23:51 2010
@@ -177,6 +177,15 @@ class Lucy::Object::CharBuf cnick CB
     bool_t
     Ends_With_Str(CharBuf *self, const char *postfix, size_t size);
 
+    /** Return the location of the substring within the CharBuf (measured in
+     * code points), or -1 if the substring does not match.
+     */
+    int64_t
+    Find(CharBuf *self, const CharBuf *substring);
+
+    int64_t
+    Find_Str(CharBuf *self, const char *ptr, size_t size);
+
     /** Test whether the CharBuf matches the passed-in string.
      */
     bool_t

Modified: incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c?rev=1053744&r1=1053743&r2=1053744&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c (original)
+++ incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c Wed Dec 29 21:23:51 2010
@@ -84,6 +84,26 @@ test_Mimic_and_Clone(TestBatch *batch)
 }
 
 static void
+test_Find(TestBatch *batch)
+{
+    CharBuf *string = CB_new(10);
+    CharBuf *substring = S_get_cb("foo");
+
+    TEST_TRUE(batch, CB_Find(string, substring) == -1, "Not in empty string");
+    CB_setf(string, "foo");
+    TEST_TRUE(batch, CB_Find(string, substring) == 0, "Find complete string");
+    CB_setf(string, "afoo");
+    TEST_TRUE(batch, CB_Find(string, substring) == 1, "Find after first");
+    CB_Set_Size(string, 3);
+    TEST_TRUE(batch, CB_Find(string, substring) == -1, "Don't overrun");
+    CB_setf(string, "afood");
+    TEST_TRUE(batch, CB_Find(string, substring) == 1, "Find in middle");
+
+    DECREF(substring);
+    DECREF(string);
+}
+
+static void
 test_Code_Point_At_and_From(TestBatch *batch)
 {
     uint32_t code_points[] = { 'a', 0x263A, 0x263A, 'b', 0x263A, 'c' }; 
@@ -401,7 +421,7 @@ test_serialization(TestBatch *batch)
 void
 TestCB_run_tests()
 {
-    TestBatch *batch = TestBatch_new(50);
+    TestBatch *batch = TestBatch_new(55);
     TestBatch_Plan(batch);
 
     test_vcatf_s(batch);
@@ -420,6 +440,7 @@ TestCB_run_tests()
     test_Cat(batch);
     test_Mimic_and_Clone(batch);
     test_Code_Point_At_and_From(batch);
+    test_Find(batch);
     test_SubString(batch);
     test_Nip_and_Chop(batch);
     test_Truncate(batch);