]> gitweb.ps.run Git - ps-cgit/blobdiff - parsing.c
Use split_ident_line() in parse_user()
[ps-cgit] / parsing.c
index 5b4b1f4e58d034878831b19076a95e25683c748d..53c29bb23e678a058e1df03d3cb045c2d36478bd 100644 (file)
--- a/parsing.c
+++ b/parsing.c
@@ -69,38 +69,27 @@ static char *substr(const char *head, const char *tail)
        return buf;
 }
 
-static char *parse_user(char *t, char **name, char **email, unsigned long *date)
+static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
 {
-       char *p = t;
-       int mode = 1;
+       const char *line_end = strchrnul(t, '\n');
+       struct ident_split ident;
+       unsigned email_len;
 
-       while (p && *p) {
-               if (mode == 1 && *p == '<') {
-                       *name = substr(t, p - 1);
-                       t = p;
-                       mode++;
-               } else if (mode == 1 && *p == '\n') {
-                       *name = substr(t, p);
-                       p++;
-                       break;
-               } else if (mode == 2 && *p == '>') {
-                       *email = substr(t, p + 1);
-                       t = p;
-                       mode++;
-               } else if (mode == 2 && *p == '\n') {
-                       *email = substr(t, p);
-                       p++;
-                       break;
-               } else if (mode == 3 && isdigit(*p)) {
-                       *date = atol(p);
-                       mode++;
-               } else if (*p == '\n') {
-                       p++;
-                       break;
-               }
-               p++;
+       if (!split_ident_line(&ident, t, line_end - t)) {
+               *name = substr(ident.name_begin, ident.name_end);
+
+               email_len = ident.mail_end - ident.mail_begin;
+               *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
+               sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
+
+               if (ident.date_begin)
+                       *date = strtoul(ident.date_begin, NULL, 10);
        }
-       return p;
+
+       if (*line_end)
+               return line_end + 1;
+       else
+               return line_end;
 }
 
 #ifdef NO_ICONV
@@ -132,7 +121,8 @@ static const char *reencode(char **txt, const char *src_enc, const char *dst_enc
 struct commitinfo *cgit_parse_commit(struct commit *commit)
 {
        struct commitinfo *ret;
-       char *p = commit->buffer, *t;
+       const char *p = get_cached_commit_buffer(commit, NULL);
+       const char *t;
 
        ret = xmalloc(sizeof(*ret));
        ret->commit = commit;
@@ -147,25 +137,25 @@ struct commitinfo *cgit_parse_commit(struct commit *commit)
        if (p == NULL)
                return ret;
 
-       if (prefixcmp(p, "tree "))
+       if (!starts_with(p, "tree "))
                die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
        else
                p += 46; // "tree " + hex[40] + "\n"
 
-       while (!prefixcmp(p, "parent "))
+       while (starts_with(p, "parent "))
                p += 48; // "parent " + hex[40] + "\n"
 
-       if (p && !prefixcmp(p, "author ")) {
+       if (p && starts_with(p, "author ")) {
                p = parse_user(p + 7, &ret->author, &ret->author_email,
                        &ret->author_date);
        }
 
-       if (p && !prefixcmp(p, "committer ")) {
+       if (p && starts_with(p, "committer ")) {
                p = parse_user(p + 10, &ret->committer, &ret->committer_email,
                        &ret->committer_date);
        }
 
-       if (p && !prefixcmp(p, "encoding ")) {
+       if (p && starts_with(p, "encoding ")) {
                p += 9;
                t = strchr(p, '\n');
                if (t) {
@@ -223,7 +213,7 @@ struct taginfo *cgit_parse_tag(struct tag *tag)
        void *data;
        enum object_type type;
        unsigned long size;
-       char *p;
+       const char *p;
        struct taginfo *ret;
 
        data = read_sha1_file(tag->object.sha1, &type, &size);
@@ -244,7 +234,7 @@ struct taginfo *cgit_parse_tag(struct tag *tag)
                if (*p == '\n')
                        break;
 
-               if (!prefixcmp(p, "tagger ")) {
+               if (starts_with(p, "tagger ")) {
                        p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
                                &ret->tagger_date);
                } else {