[BACK]Return to chars.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/chars.c between version 1.1 and 1.67

version 1.1, 2009/09/17 07:41:28 version 1.67, 2015/10/06 18:32:19
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011, 2014 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 14 
Line 15 
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
   #include "config.h"
   
   #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <ctype.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
 #include "chars.h"  #include "mandoc.h"
   #include "mandoc_aux.h"
   #include "libmandoc.h"
   
 #define ASCII_PRINT_HI   126  #define PRINT_HI         126
 #define ASCII_PRINT_LO   32  #define PRINT_LO         32
   
 struct  ln {  struct  ln {
         struct ln        *next;          struct ln        *next;
         const char       *code;          const char       *code;
         const char       *out;          const char       *ascii;
         size_t            codesz;          int               unicode;
         size_t            outsz;  
         int               type;  
 #define CHARS_CHAR       (1 << 0)  
 #define CHARS_STRING     (1 << 1)  
 #define CHARS_BOTH       (0x03)  
 };  };
   
 #define LINES_MAX         266  #define LINES_MAX         332
   
 #define CHAR(w, x, y, z) \  #define CHAR(in, ch, code) \
         { NULL, (w), (y), (x), (z), CHARS_CHAR },          { NULL, (in), (ch), (code) },
 #define STRING(w, x, y, z) \  
         { NULL, (w), (y), (x), (z), CHARS_STRING },  
 #define BOTH(w, x, y, z) \  
         { NULL, (w), (y), (x), (z), CHARS_BOTH },  
   
 static  struct ln lines[LINES_MAX] = {  #define CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
   #define CHAR_TBL_END      };
   
 #include "chars.in"  #include "chars.in"
 };  
   
 struct  tbl {  struct  mchars {
         struct ln       **htab;          struct ln       **htab;
 };  };
   
 static  inline int        match(const struct ln *,  static  const struct ln  *find(const struct mchars *,
                                 const char *, size_t, int);                                  const char *, size_t);
 static  const char       *find(struct tbl *, const char *,  
                                 size_t, size_t *, int);  
   
   
 void  void
 chars_free(void *arg)  mchars_free(struct mchars *arg)
 {  {
         struct tbl      *tab;  
   
         tab = (struct tbl *)arg;          free(arg->htab);
           free(arg);
         free(tab->htab);  
         free(tab);  
 }  }
   
   struct mchars *
 /* ARGSUSED */  mchars_alloc(void)
 void *  
 chars_init(enum chars type)  
 {  {
         struct tbl       *tab;          struct mchars    *tab;
         struct ln       **htab;          struct ln       **htab;
         struct ln        *pp;          struct ln        *pp;
         int               i, hash;          int               i, hash;
Line 83  chars_init(enum chars type)
Line 75  chars_init(enum chars type)
         /*          /*
          * Constructs a very basic chaining hashtable.  The hash routine           * Constructs a very basic chaining hashtable.  The hash routine
          * is simply the integral value of the first character.           * is simply the integral value of the first character.
          * Subsequent entries are chained in the order they're processed           * Subsequent entries are chained in the order they're processed.
          * (they're in-line re-ordered during lookup).  
          */           */
   
         if (NULL == (tab = malloc(sizeof(struct tbl))))          tab = mandoc_malloc(sizeof(struct mchars));
                 err(1, "malloc");          htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *));
   
         htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1,  
                         sizeof(struct ln **));  
   
         if (NULL == htab)  
                 err(1, "malloc");  
   
         for (i = 0; i < LINES_MAX; i++) {          for (i = 0; i < LINES_MAX; i++) {
                 assert(lines[i].codesz > 0);                  hash = (int)lines[i].code[0] - PRINT_LO;
                 assert(lines[i].code);  
                 assert(lines[i].out);  
   
                 hash = (int)lines[i].code[0] - ASCII_PRINT_LO;  
   
                 if (NULL == (pp = htab[hash])) {                  if (NULL == (pp = htab[hash])) {
                         htab[hash] = &lines[i];                          htab[hash] = &lines[i];
                         continue;                          continue;
Line 110  chars_init(enum chars type)
Line 91  chars_init(enum chars type)
   
                 for ( ; pp->next; pp = pp->next)                  for ( ; pp->next; pp = pp->next)
                         /* Scan ahead. */ ;                          /* Scan ahead. */ ;
   
                 pp->next = &lines[i];                  pp->next = &lines[i];
         }          }
   
         tab->htab = htab;          tab->htab = htab;
         return(tab);          return tab;
 }  }
   
   int
 const char *  mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz)
 chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz)  
 {  {
           const struct ln *ln;
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR));          ln = find(arg, p, sz);
           return ln != NULL ? ln->unicode : sz == 1 ? (unsigned char)*p : -1;
 }  }
   
   int
 const char *  mchars_num2char(const char *p, size_t sz)
 chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz)  
 {  {
           int       i;
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING));          i = mandoc_strntoi(p, sz, 10);
           return i >= 0 && i < 256 ? i : -1;
 }  }
   
   int
 static const char *  mchars_num2uc(const char *p, size_t sz)
 find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type)  
 {  {
         struct ln        *pp, *prev;          int      i;
         struct ln       **htab;  
         int               hash;  
   
         assert(p);          i = mandoc_strntoi(p, sz, 16);
         assert(sz > 0);          assert(i >= 0 && i <= 0x10FFFF);
           return i;
   }
   
         if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI)  const char *
                 return(NULL);  mchars_spec2str(const struct mchars *arg,
                   const char *p, size_t sz, size_t *rsz)
   {
           const struct ln *ln;
   
         /*          ln = find(arg, p, sz);
          * Lookup the symbol in the symbol hash.  See ascii2htab for the          if (ln == NULL) {
          * hashtable specs.  This dynamically re-orders the hash chain                  *rsz = 1;
          * to optimise for repeat hits.                  return sz == 1 ? p : NULL;
          */  
   
         hash = (int)p[0] - ASCII_PRINT_LO;  
         htab = tab->htab;  
   
         if (NULL == (pp = htab[hash]))  
                 return(NULL);  
   
         if (NULL == pp->next) {  
                 if ( ! match(pp, p, sz, type))  
                         return(NULL);  
                 *rsz = pp->outsz;  
                 return(pp->out);  
         }          }
   
         for (prev = NULL; pp; pp = pp->next) {          *rsz = strlen(ln->ascii);
                 if ( ! match(pp, p, sz, type)) {          return ln->ascii;
                         prev = pp;  }
                         continue;  
                 }  
   
                 /* Re-order the hash chain. */  const char *
   mchars_uc2str(int uc)
   {
           int      i;
   
                 if (prev) {          for (i = 0; i < LINES_MAX; i++)
                         prev->next = pp->next;                  if (uc == lines[i].unicode)
                         pp->next = htab[hash];                          return lines[i].ascii;
                         htab[hash] = pp;          return "<?>";
                 }  }
   
                 *rsz = pp->outsz;  static const struct ln *
                 return(pp->out);  find(const struct mchars *tab, const char *p, size_t sz)
         }  {
           const struct ln  *pp;
           int               hash;
   
         return(NULL);          assert(p);
 }  
   
           if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
                   return NULL;
   
 static inline int          hash = (int)p[0] - PRINT_LO;
 match(const struct ln *ln, const char *p, size_t sz, int type)  
 {  
   
         if ( ! (ln->type & type))          for (pp = tab->htab[hash]; pp; pp = pp->next)
                 return(0);                  if (0 == strncmp(pp->code, p, sz) &&
         if (ln->codesz != sz)                      '\0' == pp->code[(int)sz])
                 return(0);                          return pp;
         return(0 == strncmp(ln->code, p, sz));  
           return NULL;
 }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.67

CVSweb