aboutsummaryrefslogtreecommitdiff
path: root/src/references.c
blob: 1738de13faf06d3a6c6439438b1a03f6828139a1 (plain)
  1. #include "cmark.h"
  2. #include "utf8.h"
  3. #include "parser.h"
  4. #include "references.h"
  5. #include "inlines.h"
  6. #include "chunk.h"
  7. static unsigned int
  8. refhash(const unsigned char *link_ref)
  9. {
  10. unsigned int hash = 0;
  11. while (*link_ref)
  12. hash = (*link_ref++) + (hash << 6) + (hash << 16) - hash;
  13. return hash;
  14. }
  15. static void reference_free(reference *ref)
  16. {
  17. if(ref != NULL) {
  18. free(ref->label);
  19. free(ref->url);
  20. free(ref->title);
  21. free(ref);
  22. }
  23. }
  24. // normalize reference: collapse internal whitespace to single space,
  25. // remove leading/trailing whitespace, case fold
  26. // Return NULL if the reference name is actually empty (i.e. composed
  27. // solely from whitespace)
  28. static unsigned char *normalize_reference(chunk *ref)
  29. {
  30. strbuf normalized = GH_BUF_INIT;
  31. unsigned char *result;
  32. if(ref == NULL)
  33. return NULL;
  34. if (ref->len == 0)
  35. return NULL;
  36. utf8proc_case_fold(&normalized, ref->data, ref->len);
  37. strbuf_trim(&normalized);
  38. strbuf_normalize_whitespace(&normalized);
  39. result = strbuf_detach(&normalized);
  40. assert(result);
  41. if (result[0] == '\0') {
  42. free(result);
  43. return NULL;
  44. }
  45. return result;
  46. }
  47. static void add_reference(reference_map *map, reference* ref)
  48. {
  49. reference *t = ref->next = map->table[ref->hash % REFMAP_SIZE];
  50. while (t) {
  51. if (t->hash == ref->hash &&
  52. !strcmp((char *)t->label, (char *)ref->label)) {
  53. reference_free(ref);
  54. return;
  55. }
  56. t = t->next;
  57. }
  58. map->table[ref->hash % REFMAP_SIZE] = ref;
  59. }
  60. extern void reference_create(reference_map *map, chunk *label, chunk *url, chunk *title)
  61. {
  62. reference *ref;
  63. unsigned char *reflabel = normalize_reference(label);
  64. /* empty reference name, or composed from only whitespace */
  65. if (reflabel == NULL)
  66. return;
  67. ref = (reference *)calloc(1, sizeof(*ref));
  68. if(ref != NULL) {
  69. ref->label = reflabel;
  70. ref->hash = refhash(ref->label);
  71. ref->url = clean_url(url);
  72. ref->title = clean_title(title);
  73. ref->next = NULL;
  74. add_reference(map, ref);
  75. }
  76. }
  77. // Returns reference if refmap contains a reference with matching
  78. // label, otherwise NULL.
  79. reference* reference_lookup(reference_map *map, chunk *label)
  80. {
  81. reference *ref = NULL;
  82. unsigned char *norm;
  83. unsigned int hash;
  84. if (label->len > MAX_LINK_LABEL_LENGTH)
  85. return NULL;
  86. if (map == NULL)
  87. return NULL;
  88. norm = normalize_reference(label);
  89. if (norm == NULL)
  90. return NULL;
  91. hash = refhash(norm);
  92. ref = map->table[hash % REFMAP_SIZE];
  93. while (ref) {
  94. if (ref->hash == hash &&
  95. !strcmp((char *)ref->label, (char *)norm))
  96. break;
  97. ref = ref->next;
  98. }
  99. free(norm);
  100. return ref;
  101. }
  102. void reference_map_free(reference_map *map)
  103. {
  104. unsigned int i;
  105. if(map == NULL)
  106. return;
  107. for (i = 0; i < REFMAP_SIZE; ++i) {
  108. reference *ref = map->table[i];
  109. reference *next;
  110. while (ref) {
  111. next = ref->next;
  112. reference_free(ref);
  113. ref = next;
  114. }
  115. }
  116. free(map);
  117. }
  118. reference_map *reference_map_new(void)
  119. {
  120. return (reference_map *)calloc(1, sizeof(reference_map));
  121. }