From 6b798d2c0d028fc4200af1d2b529e5bf58e40716 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 31 May 2014 15:47:25 +1000 Subject: [PATCH] mdcode: normalise text_cmp and export it. 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 --- csrc/mdcode.mdc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/csrc/mdcode.mdc b/csrc/mdcode.mdc index ac34a54..e3a258e 100644 --- a/csrc/mdcode.mdc +++ b/csrc/mdcode.mdc @@ -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) -- 2.43.0