summaryrefslogtreecommitdiff
path: root/src/libtipidee/tipidee_util_htmlescape.c
blob: 1dfd55266152cbc621922623908c4b9ec8561a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ISC license. */

#include <stdlib.h>

#include <tipidee/util.h>

struct htmlescape_s
{
  char c ;
  char const *code ;
} ;

static struct htmlescape_s const table[] =
{
  { .c = '\"', .code = "&quot;" },
  { .c = '&', .code = "&amp;" },
  { .c = '\'', .code = "&#39;" },
  { .c = '<', .code = "&lt;" },
  { .c = '>', .code = "&gt;" },
} ;

static int htmlescape_cmp (void const *a, void const *b)
{
  char aa = *((char const *)a) ;
  char bb = ((struct htmlescape_s const *)b)->c ;
  return aa < bb ? -1 : aa > bb ;
}

char const *tipidee_util_htmlescape (char const *s)
{
  struct htmlescape_s const *p = bsearch(
    s,
    table,
    sizeof(table) / sizeof(struct htmlescape_s),
    sizeof(struct htmlescape_s),
    &htmlescape_cmp) ;
  return p ? p->code : s ;
}