]> ocean-lang.org Git - ocean/commitdiff
mdcode: normalise text_cmp and export it.
authorNeilBrown <neilb@suse.de>
Sat, 31 May 2014 05:47:25 +0000 (15:47 +1000)
committerNeilBrown <neilb@suse.de>
Sun, 15 Jun 2014 07:20:22 +0000 (17:20 +1000)
It is silly using a non-standard text order when it isn't really need
and other code might benefit from having this function available.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/mdcode.mdc

index ac34a54c027fb353ea42ffcb4feb4608d144d57c..e3a258efa7b9b4687bc0d287fd9d028667fc287e 100644 (file)
@@ -330,21 +330,31 @@ However when adding a link, we might be able to include it in the last
 Now we need a lookup table to be able to find sections by name.
 Something that provides an `n*log(N)` search time is probably
 justified, but for now I want a minimal stand-alone program so a
-linked list managed by insertion-sort will do.  As a comparison
-function it is easiest to sort based on length before content.  So
-sections won't be in standard lexical order, but that isn't important.
+linked list managed by insertion-sort will do.
+
+The text compare function will likely be useful for any clients of our
+library, so we may as well export it.
 
 If we cannot find a section, we simply want to create it.  This allows
 sections and references to be created in any order.  Sections with
 no references or no content will cause a warning eventually.
 
+#### exported functions
+
+       int text_cmp(struct text a, struct text b);
+
 #### internal functions
 
-       static int text_cmp(struct text a, struct text b)
+       int text_cmp(struct text a, struct text b)
        {
-               if (a.len != b.len)
+               int len = a.len;
+               if (len > b.len)
+                       len = b.len;
+               int cmp = strncmp(a.txt, b.txt, len);
+               if (cmp)
+                       return cmp;
+               else
                        return a.len - b.len;
-               return strncmp(a.txt, b.txt, a.len);
        }
 
        static struct psection *section_find(struct psection **list, struct text name)