lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [14/20] git commit: refs/heads/cfish-string-prep1 - Convert Util::IndexFileNames to StringIterator
Date Sat, 07 Sep 2013 16:53:35 GMT
Convert Util::IndexFileNames to StringIterator


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/c0534877
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/c0534877
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/c0534877

Branch: refs/heads/cfish-string-prep1
Commit: c0534877a17453b81eb52941968a78fd7d623c2f
Parents: 430ba96
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 7 17:51:04 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sat Sep 7 18:14:05 2013 +0200

----------------------------------------------------------------------
 core/Lucy/Store/CompoundFileReader.c     |  8 ++---
 core/Lucy/Store/Folder.c                 | 47 +++++++++++++++------------
 core/Lucy/Store/RAMFolder.c              | 38 ++++++++++++----------
 core/Lucy/Test/Util/TestIndexFileNames.c | 43 ++++++++++--------------
 core/Lucy/Util/IndexFileNames.c          | 46 ++++++++++++++------------
 core/Lucy/Util/IndexFileNames.cfh        | 12 +++----
 6 files changed, 98 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Store/CompoundFileReader.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/CompoundFileReader.c b/core/Lucy/Store/CompoundFileReader.c
index cc08f68..2ecf45c 100644
--- a/core/Lucy/Store/CompoundFileReader.c
+++ b/core/Lucy/Store/CompoundFileReader.c
@@ -87,13 +87,12 @@ CFReader_do_open(CompoundFileReader *self, Folder *folder) {
     // Strip directory name from filepaths for old format.
     if (ivars->format == 1) {
         VArray *files = Hash_Keys(ivars->records);
-        StackString *folder_name
-            = IxFileNames_local_part(Folder_Get_Path(folder), SStr_BLANK());
-        size_t folder_name_len = SStr_Length(folder_name);
+        String *folder_name = IxFileNames_local_part(Folder_Get_Path(folder));
+        size_t folder_name_len = Str_Length(folder_name);
 
         for (uint32_t i = 0, max = VA_Get_Size(files); i < max; i++) {
             String *orig = (String*)VA_Fetch(files, i);
-            if (Str_Starts_With(orig, (String*)folder_name)) {
+            if (Str_Starts_With(orig, folder_name)) {
                 Obj *record = Hash_Delete(ivars->records, (Obj*)orig);
                 size_t offset = folder_name_len + sizeof(DIR_SEP) - 1;
                 size_t len    = Str_Length(orig) - offset;
@@ -103,6 +102,7 @@ CFReader_do_open(CompoundFileReader *self, Folder *folder) {
             }
         }
 
+        DECREF(folder_name);
         DECREF(files);
     }
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Store/Folder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/Folder.c b/core/Lucy/Store/Folder.c
index 066d20e..371dcc6 100644
--- a/core/Lucy/Store/Folder.c
+++ b/core/Lucy/Store/Folder.c
@@ -67,11 +67,12 @@ Folder_Open_In_IMP(Folder *self, const String *path) {
     InStream *instream = NULL;
 
     if (enclosing_folder) {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        instream = Folder_Local_Open_In(enclosing_folder, (String*)name);
+        String *name = IxFileNames_local_part(path);
+        instream = Folder_Local_Open_In(enclosing_folder, name);
         if (!instream) {
             ERR_ADD_FRAME(Err_get_error());
         }
+        DECREF(name);
     }
     else {
         Err_set_error(Err_new(Str_newf("Invalid path: '%o'", path)));
@@ -125,12 +126,12 @@ Folder_Open_FileHandle_IMP(Folder *self, const String *path,
     FileHandle *fh = NULL;
 
     if (enclosing_folder) {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        fh = Folder_Local_Open_FileHandle(enclosing_folder,
-                                          (String*)name, flags);
+        String *name = IxFileNames_local_part(path);
+        fh = Folder_Local_Open_FileHandle(enclosing_folder, name, flags);
         if (!fh) {
             ERR_ADD_FRAME(Err_get_error());
         }
+        DECREF(name);
     }
     else {
         Err_set_error(Err_new(Str_newf("Invalid path: '%o'", path)));
@@ -143,8 +144,9 @@ bool
 Folder_Delete_IMP(Folder *self, const String *path) {
     Folder *enclosing_folder = Folder_Enclosing_Folder(self, path);
     if (enclosing_folder) {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        bool result = Folder_Local_Delete(enclosing_folder, (String*)name);
+        String *name = IxFileNames_local_part(path);
+        bool result = Folder_Local_Delete(enclosing_folder, name);
+        DECREF(name);
         return result;
     }
     else {
@@ -160,10 +162,10 @@ Folder_Delete_Tree_IMP(Folder *self, const String *path) {
     if (!path || !Str_Get_Size(path)) { return false; }
 
     if (enclosing_folder) {
-        StackString *local = IxFileNames_local_part(path, SStr_BLANK());
-        if (Folder_Local_Is_Directory(enclosing_folder, (String*)local)) {
+        String *local = IxFileNames_local_part(path);
+        if (Folder_Local_Is_Directory(enclosing_folder, local)) {
             Folder *inner_folder
-                = Folder_Local_Find_Folder(enclosing_folder, (String*)local);
+                = Folder_Local_Find_Folder(enclosing_folder, local);
             DirHandle *dh = Folder_Local_Open_Dir(inner_folder);
             if (dh) {
                 VArray *files = VA_new(20);
@@ -195,7 +197,9 @@ Folder_Delete_Tree_IMP(Folder *self, const String *path) {
                 DECREF(dh);
             }
         }
-        return Folder_Local_Delete(enclosing_folder, (String*)local);
+        bool retval = Folder_Local_Delete(enclosing_folder, local);
+        DECREF(local);
+        return retval;
     }
     else {
         // Return failure if the entry wasn't there in the first place.
@@ -286,11 +290,12 @@ Folder_MkDir_IMP(Folder *self, const String *path) {
                                        path)));
     }
     else {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        result = Folder_Local_MkDir(enclosing_folder, (String*)name);
+        String *name = IxFileNames_local_part(path);
+        result = Folder_Local_MkDir(enclosing_folder, name);
         if (!result) {
             ERR_ADD_FRAME(Err_get_error());
         }
+        DECREF(name);
     }
 
     return result;
@@ -301,10 +306,11 @@ Folder_Exists_IMP(Folder *self, const String *path) {
     Folder *enclosing_folder = Folder_Enclosing_Folder(self, path);
     bool retval = false;
     if (enclosing_folder) {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        if (Folder_Local_Exists(enclosing_folder, (String*)name)) {
+        String *name = IxFileNames_local_part(path);
+        if (Folder_Local_Exists(enclosing_folder, name)) {
             retval = true;
         }
+        DECREF(name);
     }
     return retval;
 }
@@ -314,10 +320,11 @@ Folder_Is_Directory_IMP(Folder *self, const String *path) {
     Folder *enclosing_folder = Folder_Enclosing_Folder(self, path);
     bool retval = false;
     if (enclosing_folder) {
-        StackString *name = IxFileNames_local_part(path, SStr_BLANK());
-        if (Folder_Local_Is_Directory(enclosing_folder, (String*)name)) {
+        String *name = IxFileNames_local_part(path);
+        if (Folder_Local_Is_Directory(enclosing_folder, name)) {
             retval = true;
         }
+        DECREF(name);
     }
     return retval;
 }
@@ -412,12 +419,12 @@ Folder_Consolidate_IMP(Folder *self, const String *path) {
         CFWriter_Consolidate(cf_writer);
         DECREF(cf_writer);
         if (Str_Get_Size(path)) {
-            StackString *name = IxFileNames_local_part(path, SStr_BLANK());
             CompoundFileReader *cf_reader = CFReader_open(folder);
             if (!cf_reader) { RETHROW(INCREF(Err_get_error())); }
             Hash *entries = Folder_IVARS(enclosing_folder)->entries;
-            Hash_Store(entries, (Obj*)name,
-                       (Obj*)cf_reader);
+            String *name = IxFileNames_local_part(path);
+            Hash_Store(entries, (Obj*)name, (Obj*)cf_reader);
+            DECREF(name);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Store/RAMFolder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/RAMFolder.c b/core/Lucy/Store/RAMFolder.c
index f2b2dd4..4a42484 100644
--- a/core/Lucy/Store/RAMFolder.c
+++ b/core/Lucy/Store/RAMFolder.c
@@ -141,7 +141,7 @@ RAMFolder_Local_Is_Directory_IMP(RAMFolder *self, const String *name)
{
 static bool
 S_rename_or_hard_link(RAMFolder *self, const String* from, const String *to,
                       Folder *from_folder, Folder *to_folder,
-                      StackString *from_name, StackString *to_name,
+                      String *from_name, String *to_name,
                       int op) {
     Obj       *elem              = NULL;
     RAMFolder *inner_from_folder = NULL;
@@ -190,7 +190,7 @@ S_rename_or_hard_link(RAMFolder *self, const String* from, const String
*to,
                       (Obj*)from_name);
     if (!elem) {
         if (Folder_Is_A(from_folder, COMPOUNDFILEREADER)
-            && Folder_Local_Exists(from_folder, (String*)from_name)
+            && Folder_Local_Exists(from_folder, from_name)
            ) {
             Err_set_error(Err_new(Str_newf("Source file '%o' is virtual",
                                            from)));
@@ -210,7 +210,7 @@ S_rename_or_hard_link(RAMFolder *self, const String* from, const String
*to,
 
             // Return success fast if file is copied on top of itself.
             if (inner_from_folder == inner_to_folder
-                && SStr_Equals(from_name, (Obj*)to_name)
+                && Str_Equals(from_name, (Obj*)to_name)
                ) {
                 return true;
             }
@@ -241,7 +241,7 @@ S_rename_or_hard_link(RAMFolder *self, const String* from, const String
*to,
         DECREF(Hash_Delete(RAMFolder_IVARS(inner_from_folder)->entries,
                            (Obj*)from_name));
         if (Obj_Is_A(elem, FOLDER)) {
-            String *newpath = S_fullpath(inner_to_folder, (String*)to_name);
+            String *newpath = S_fullpath(inner_to_folder, to_name);
             Folder_Set_Path((Folder*)elem, newpath);
             DECREF(newpath);
         }
@@ -276,28 +276,30 @@ S_rename_or_hard_link(RAMFolder *self, const String* from, const String
*to,
 bool
 RAMFolder_Rename_IMP(RAMFolder *self, const String* from,
                      const String *to) {
-    Folder        *from_folder = RAMFolder_Enclosing_Folder(self, from);
-    Folder        *to_folder   = RAMFolder_Enclosing_Folder(self, to);
-    StackString *from_name   = IxFileNames_local_part(from, SStr_BLANK());
-    StackString *to_name     = IxFileNames_local_part(to, SStr_BLANK());
-    bool result = S_rename_or_hard_link(self, from, to, from_folder,
-                                          to_folder, from_name, to_name,
-                                          OP_RENAME);
+    Folder *from_folder = RAMFolder_Enclosing_Folder(self, from);
+    Folder *to_folder   = RAMFolder_Enclosing_Folder(self, to);
+    String *from_name   = IxFileNames_local_part(from);
+    String *to_name     = IxFileNames_local_part(to);
+    bool result = S_rename_or_hard_link(self, from, to, from_folder, to_folder,
+                                        from_name, to_name, OP_RENAME);
     if (!result) { ERR_ADD_FRAME(Err_get_error()); }
+    DECREF(to_name);
+    DECREF(from_name);
     return result;
 }
 
 bool
 RAMFolder_Hard_Link_IMP(RAMFolder *self, const String *from,
                         const String *to) {
-    Folder        *from_folder = RAMFolder_Enclosing_Folder(self, from);
-    Folder        *to_folder   = RAMFolder_Enclosing_Folder(self, to);
-    StackString *from_name   = IxFileNames_local_part(from, SStr_BLANK());
-    StackString *to_name     = IxFileNames_local_part(to, SStr_BLANK());
-    bool result = S_rename_or_hard_link(self, from, to, from_folder,
-                                          to_folder, from_name, to_name,
-                                          OP_HARD_LINK);
+    Folder *from_folder = RAMFolder_Enclosing_Folder(self, from);
+    Folder *to_folder   = RAMFolder_Enclosing_Folder(self, to);
+    String *from_name   = IxFileNames_local_part(from);
+    String *to_name     = IxFileNames_local_part(to);
+    bool result = S_rename_or_hard_link(self, from, to, from_folder, to_folder,
+                                        from_name, to_name, OP_HARD_LINK);
     if (!result) { ERR_ADD_FRAME(Err_get_error()); }
+    DECREF(to_name);
+    DECREF(from_name);
     return result;
 }
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Test/Util/TestIndexFileNames.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Util/TestIndexFileNames.c b/core/Lucy/Test/Util/TestIndexFileNames.c
index 8dbbb7a..6ba675d 100644
--- a/core/Lucy/Test/Util/TestIndexFileNames.c
+++ b/core/Lucy/Test/Util/TestIndexFileNames.c
@@ -28,33 +28,24 @@ TestIxFileNames_new() {
 }
 
 static void
-test_local_part(TestBatchRunner *runner) {
-    StackString *source = SStr_BLANK();
-    StackString *got    = SStr_BLANK();
-
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals(got, (Obj*)source), "simple name");
-
-    SStr_Assign_Str(source, "foo.txt", 7);
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals(got, (Obj*)source), "name with extension");
-
-    SStr_Assign_Str(source, "/foo", 4);
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals_Str(got, "foo", 3), "strip leading slash");
-
-    SStr_Assign_Str(source, "/foo/", 5);
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals_Str(got, "foo", 3), "strip trailing slash");
-
-    SStr_Assign_Str(source, "foo/bar\\ ", 9);
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals_Str(got, "bar\\ ", 5),
-              "Include garbage like backslashes and spaces");
+S_test_local_part(TestBatchRunner *runner, const char *source,
+                  const char *wanted, const char *test_name) {
+    StackString *source_str = SSTR_WRAP_STR(source, strlen(source));
+    String *got = IxFileNames_local_part((String*)source_str);
+    TEST_TRUE(runner, Str_Equals_Str(got, wanted, strlen(wanted)), test_name);
+    DECREF(got);
+}
 
-    SStr_Assign_Str(source, "foo/bar/baz.txt", 15);
-    got = IxFileNames_local_part((String*)source, got);
-    TEST_TRUE(runner, SStr_Equals_Str(got, "baz.txt", 7), "find last component");
+static void
+test_local_part(TestBatchRunner *runner) {
+    S_test_local_part(runner, "", "", "simple name");
+    S_test_local_part(runner, "foo.txt", "foo.txt", "name with extension");
+    S_test_local_part(runner, "/foo", "foo", "strip leading slash");
+    S_test_local_part(runner, "/foo/", "foo", "strip trailing slash");
+    S_test_local_part(runner, "foo/bar\\ ", "bar\\ ",
+                      "Include garbage like backslashes and spaces");
+    S_test_local_part(runner, "foo/bar/baz.txt", "baz.txt",
+                      "find last component");
 }
 
 static void

http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Util/IndexFileNames.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/IndexFileNames.c b/core/Lucy/Util/IndexFileNames.c
index 9a938db..e3b7bf7 100644
--- a/core/Lucy/Util/IndexFileNames.c
+++ b/core/Lucy/Util/IndexFileNames.c
@@ -51,45 +51,51 @@ IxFileNames_latest_snapshot(Folder *folder) {
 
 uint64_t
 IxFileNames_extract_gen(const String *name) {
-    StackString *num_string = SSTR_WRAP(name);
+    StringIterator *iter = Str_Top(name);
 
     // Advance past first underscore.  Bail if we run out of string or if we
     // encounter a NULL.
     while (1) {
-        uint32_t code_point = SStr_Nibble(num_string);
-        if (code_point == 0) { return 0; }
+        uint32_t code_point = StrIter_Next(iter);
+        if (code_point == STRITER_DONE) { return 0; }
         else if (code_point == '_') { break; }
     }
 
-    return (uint64_t)SStr_BaseX_To_I64(num_string, 36);
-}
+    String *num_string = StrIter_substring(iter, NULL);
+    uint64_t retval = (uint64_t)Str_BaseX_To_I64(num_string, 36);
 
-StackString*
-IxFileNames_local_part(const String *path, StackString *target) {
-    StackString *scratch = SSTR_WRAP(path);
-    size_t local_part_start = Str_Length(path);
-    uint32_t code_point;
+    DECREF(num_string);
+    DECREF(iter);
+    return retval;
+}
 
-    SStr_Assign(target, path);
+String*
+IxFileNames_local_part(const String *path) {
+    StringIterator *top = Str_Tail(path);
+    uint32_t code_point = StrIter_Prev(top);
 
     // Trim trailing slash.
-    while (SStr_Code_Point_From(target, 1) == '/') {
-        SStr_Chop(target, 1);
-        SStr_Chop(scratch, 1);
-        local_part_start--;
+    while (code_point == '/') {
+        code_point = StrIter_Prev(top);
     }
 
+    StringIterator *tail = StrIter_Clone(top);
+    StrIter_Advance(tail, 1);
+
     // Substring should start after last slash.
-    while (0 != (code_point = SStr_Code_Point_From(scratch, 1))) {
+    while (code_point != STRITER_DONE) {
         if (code_point == '/') {
-            SStr_Nip(target, local_part_start);
+            StrIter_Advance(top, 1);
             break;
         }
-        SStr_Chop(scratch, 1);
-        local_part_start--;
+        code_point = StrIter_Prev(top);
     }
 
-    return target;
+    String *retval = StrIter_substring(top, tail);
+
+    DECREF(tail);
+    DECREF(top);
+    return retval;
 }
 
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/c0534877/core/Lucy/Util/IndexFileNames.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Util/IndexFileNames.cfh b/core/Lucy/Util/IndexFileNames.cfh
index 872efb2..b82f857 100644
--- a/core/Lucy/Util/IndexFileNames.cfh
+++ b/core/Lucy/Util/IndexFileNames.cfh
@@ -35,15 +35,11 @@ inert class Lucy::Util::IndexFileNames cnick IxFileNames {
     inert incremented nullable String*
     latest_snapshot(Folder *folder);
 
-    /** Split the <code>path</code> on '/' and assign the last component to
-     * <code>target</code>, which will remain valid only as long as
-     * <code>path</code> is unmodified.  Trailing slashes will be stripped.
-     *
-     * @param target The target string to assign to.
-     * @return target, allowing an assignment idiom.
+    /** Split the <code>path</code> on '/' and return the last component.
+     * Trailing slashes will be stripped.
      */
-    inert incremented StackString*
-    local_part(const String *path, StackString *target);
+    inert incremented String*
+    local_part(const String *path);
 }
 
 


Mime
View raw message