Commit 48d28d7f authored by Rusty Russell's avatar Rusty Russell

ccanlint: use strreg for section extraction.

Makes it simpler and clearer.
parent d0093562
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <ctype.h> #include <ctype.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/str_talloc/str_talloc.h>
#include "doc_extract.h" #include "doc_extract.h"
#include "tools.h" #include "tools.h"
...@@ -50,28 +51,17 @@ static bool is_blank(const char *line) ...@@ -50,28 +51,17 @@ static bool is_blank(const char *line)
return line && line[strspn(line, " \t\n")] == '\0'; return line && line[strspn(line, " \t\n")] == '\0';
} }
static bool is_section(const char *line, bool one_liner) static char *is_section(const void *ctx, const char *line, char **value)
{ {
unsigned int len = 0; char *secname;
/* Any number of upper case words separated by spaces, ending in : */ /* Any number of upper case words separated by spaces, ending in : */
for (;;) { if (!strreg(ctx, line,
if (!isupper(line[len])) "^([A-Z][a-zA-Z0-9_]*( [A-Z][a-zA-Z0-9_]*)*):[ \t\n]*(.*)",
return false; &secname, NULL, value))
len += strspn(line+len, IDENT_CHARS); return NULL;
if (line[len] == ':')
break;
if (line[len] != ' ')
return false;
len++;
}
/* If it can be a one-liner, a space is sufficient.*/ return secname;
if (one_liner)
return (line[len+1] == ' ' || line[len+1] == '\t');
return line[len] == ':' && is_blank(line+len+1);
} }
/* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */ /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
...@@ -148,6 +138,7 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num) ...@@ -148,6 +138,7 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
for (i = 0; lines[i]; i++) { for (i = 0; lines[i]; i++) {
unsigned funclen; unsigned funclen;
char *type, *extra;
funclen = is_summary_line(lines[i]); funclen = is_summary_line(lines[i]);
if (funclen) { if (funclen) {
...@@ -155,20 +146,15 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num) ...@@ -155,20 +146,15 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
curr = new_section(list, function, "summary"); curr = new_section(list, function, "summary");
add_line(curr, lines[i] + funclen + 3); add_line(curr, lines[i] + funclen + 3);
curr = new_section(list, function, "description"); curr = new_section(list, function, "description");
} else if (is_section(lines[i], false)) { } else if ((type = is_section(list, lines[i], &extra)) != NULL){
char *type = talloc_strndup(curr, lines[i],
strcspn(lines[i], ":"));
curr = new_section(list, function, type);
} else if (is_section(lines[i], true)) {
unsigned int sectlen = strcspn(lines[i], ":");
char *type = talloc_strndup(curr, lines[i], sectlen);
curr = new_section(list, function, type); curr = new_section(list, function, type);
add_line(curr, lines[i] + sectlen + 1 if (!streq(extra, "")) {
+ strspn(lines[i] + sectlen + 1, " \t")); add_line(curr, extra);
curr = NULL;
}
} else { } else {
if (!curr) if (curr)
continue; add_line(curr, lines[i]);
add_line(curr, lines[i]);
} }
} }
talloc_free(lines); talloc_free(lines);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment