aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-searching-tokenizer.c
blob: 6d34b6d8f0b6688574cee715f095cb6f361a7c7b (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 * Developed by Jon Trowbridge <trow@ximian.com>
 * Rewritten significantly to handle multiple strings and improve performance
 * by Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "e-searching-tokenizer.h"

#include "libedataserver/e-memory.h"

#define d(x)

enum {
    MATCH_SIGNAL,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (
    ESearchingTokenizer,
    e_searching_tokenizer,
    HTML_TYPE_TOKENIZER)

/* Utility functions */

/* This is faster and safer than glib2's utf8 abomination,
 * but isn't exported from camel as yet */
static inline guint32
camel_utf8_getc (const guchar **ptr)
{
    register guchar *p = (guchar *)*ptr;
    register guchar c, r;
    register guint32 v, m;

again:
    r = *p++;
loop:
    if (r < 0x80) {
        *ptr = p;
        v = r;
    } else if (r < 0xfe) { /* valid start char? */
        v = r;
        m = 0x7f80; /* used to mask out the length bits */
        do {
            c = *p++;
            if ((c & 0xc0) != 0x80) {
                r = c;
                goto loop;
            }
            v = (v<<6) | (c & 0x3f);
            r<<=1;
            m<<=5;
        } while (r & 0x40);

        *ptr = p;

        v &= ~m;
    } else {
        goto again;
    }

    return v;
}

/* note: our tags of interest are 7 bit ascii
 * only no need to do any fancy utf8 stuff */
/* tags should be upper case
   if this list gets longer than 10 entries, consider binary search */
static const gchar *ignored_tags[] = {
    "B", "I", "FONT", "TT", "EM", /* and more? */};

static gint
ignore_tag (const gchar *tag)
{
    gchar *t = g_alloca (strlen (tag)+1), c, *out;
    const gchar *in;
    gint i;

    /* we could use a aho-corasick matcher here too ... but we wont */

    /* normalise tag into 't'.
       Note we use the property that the only tags we're interested in
       are 7 bit ascii to shortcut and simplify case insensitivity */
    in = tag+2;     /* skip: TAG_ESCAPE '<' */
    if (*in == '/')
        in++;
    out = t;
    while ((c = *in++)) {
        if (c >= 'A' && c <= 'Z')
            *out++ = c;
        else if (c >= 'a' && c <= 'z')
            *out++ = c & 0xdf; /* convert ASCII to upper case */
        else
            /* maybe should check for > or ' ' etc? */
            break;
    }
    *out = 0;

    for (i = 0; i < G_N_ELEMENTS (ignored_tags); i++) {
        if (strcmp (t, ignored_tags[i]) == 0)
            return 1;
    }

    return 0;
}

/* ********************************************************************** */

/* Aho-Corasick search tree implmeentation */

/* next state if we match a character */
struct _match {
    struct _match *next;
    guint32 ch;
    struct _state *match;
};

/* tree state node */
struct _state {
    struct _match *matches;
    guint final;        /* max no of chars we just matched */
    struct _state *fail;        /* where to try next if we fail */
    struct _state *next;        /* next on this level? */
};

/* base tree structure */
struct _trie {
    struct _state root;
    gint max_depth;

    EMemChunk *state_chunks;
    EMemChunk *match_chunks;
};

/* will be enabled only if debug is enabled */
#if d(1) -1 != -1
static void
dump_trie (struct _state *s, gint d)
{
    gchar *p = g_alloca (d*2+1);
    struct _match *m;

    memset (p, ' ', d*2);
    p[d*2]=0;

    printf("%s[state] %p: %d  fail->%p\n", p, s, s->final, s->fail);
    m = s->matches;
    while (m) {
        printf(" %s'%c' -> %p\n", p, m->ch, m->match);
        if (m->match)
            dump_trie (m->match, d+1);
        m = m->next;
    }
}
#endif

/* This builds an Aho-Corasick search trie for a set of utf8 words */
/* See
    http://www-sr.informatik.uni-tuebingen.de/~buehler/AC/AC.html
   for a neat demo */

static inline struct _match *
g (struct _state *q, guint32 c)
{
    struct _match *m = q->matches;

    while (m && m->ch != c)
        m = m->next;

    return m;
}

static struct _trie *
build_trie (gint nocase, gint len, guchar **words)
{
    struct _state *q, *qt, *r;
    const guchar *word;
    struct _match *m, *n = NULL;
    gint i, depth;
    guint32 c;
    struct _trie *trie;
    gint state_depth_max, state_depth_size;
    struct _state **state_depth;

    trie = g_malloc (sizeof (*trie));
    trie->root.matches = NULL;
    trie->root.final = 0;
    trie->root.fail = NULL;
    trie->root.next = NULL;

    trie->state_chunks = e_memchunk_new (8, sizeof (struct _state));
    trie->match_chunks = e_memchunk_new (8, sizeof (struct _match));

    /* This will correspond to the length of the longest pattern */
    state_depth_size = 0;
    state_depth_max = 64;
    state_depth = g_malloc (sizeof (*state_depth[0])*64);
    state_depth[0] = NULL;

    /* Step 1: Build trie */

    /* This just builds a tree that merges all common prefixes into the same branch */

    for (i=0;i<len;i++) {
        word = words[i];
        q = &trie->root;
        depth = 0;
        while ((c = camel_utf8_getc (&word))) {
            if (nocase)
                c = g_unichar_tolower (c);
            m = g (q, c);
            if (m == NULL) {
                m = e_memchunk_alloc (trie->match_chunks);
                m->ch = c;
                m->next = q->matches;
                q->matches = m;
                q = m->match = e_memchunk_alloc (trie->state_chunks);
                q->matches = NULL;
                q->fail = &trie->root;
                q->final = 0;
                if (state_depth_max < depth) {
                    state_depth_max += 64;
                    state_depth = g_realloc (
                        state_depth,
                        sizeof (*state_depth[0]) *
                        state_depth_max);
                }
                if (state_depth_size < depth) {
                    state_depth[depth] = NULL;
                    state_depth_size = depth;
                }
                q->next = state_depth[depth];
                state_depth[depth] = q;
            } else {
                q = m->match;
            }
            depth++;
        }
        q->final = depth;
    }

    d(printf("Dumping trie:\n"));
    d (dump_trie (&trie->root, 0));

    /* Step 2: Build failure graph */

    /* This searches for the longest substring which is a prefix of another string and
       builds a graph of failure links so you can find multiple substrings concurrently,
       using aho-corasick's algorithm */

    for (i=0;i<state_depth_size;i++) {
        q = state_depth[i];
        while (q) {
            m = q->matches;
            while (m) {
                c = m->ch;
                qt = m->match;
                r = q->fail;
                while (r && (n = g (r, c)) == NULL)
                    r = r->fail;
                if (r != NULL) {
                    qt->fail = n->match;
                    if (qt->fail->final > qt->final)
                        qt->final = qt->fail->final;
                } else {
                    if ((n = g (&trie->root, c)))
                        qt->fail = n->match;
                    else
                        qt->fail = &trie->root;
                }
                m = m->next;
            }
            q = q->next;
        }
    }

    d (printf("After failure analysis\n"));
    d (dump_trie (&trie->root, 0));

    g_free (state_depth);

    trie->max_depth = state_depth_size;

    return trie;
}

static void
free_trie (struct _trie *t)
{
    e_memchunk_destroy (t->match_chunks);
    e_memchunk_destroy (t->state_chunks);

    g_free (t);
}

/* ********************************************************************** */

/* html token searcher */

struct _token {
    struct _token *next;
    struct _token *prev;
    guint offset;
    /* we need to copy the token for memory management, so why not copy it whole */
    gchar tok[1];
};

/* stack of submatches currently being scanned, used for merging */
struct _submatch {
    guint offstart, offend; /* in bytes */
};

/* flags for new func */
#define SEARCH_CASE (1)
#define SEARCH_BOLD (2)

struct _searcher {
    struct _trie *t;

    gchar *(*next_token)(); /* callbacks for more tokens */
    gpointer next_data;

    gint words;     /* how many words */
    gchar *tags, *tage; /* the tag we used to highlight */

    gint flags;     /* case sensitive or not */

    struct _state *state;   /* state is the current trie state */

    gint matchcount;

    GQueue input;       /* pending 'input' tokens, processed but might match */
    GQueue output;      /* output tokens ready for source */

    struct _token *current; /* for token output memory management */

    guint32 offset;     /* current offset through searchable stream? */
    guint32 offout;     /* last output position */

    guint lastp;    /* current position in rotating last buffer */
    guint32 *last;      /* buffer that goes back last 'n' positions */
    guint32 last_mask;  /* bitmask for efficient rotation calculation */

    guint submatchp;    /* submatch stack */
    struct _submatch *submatches;
};

static void
searcher_set_tokenfunc (struct _searcher *s,
                        gchar *(*next)(),
                        gpointer data)
{
    s->next_token = next;
    s->next_data = data;
}

static struct _searcher *
searcher_new (gint flags,
              gint argc,
              guchar **argv,
              const gchar *tags,
              const gchar *tage)
{
    gint i, m;
    struct _searcher *s;

    s = g_malloc (sizeof (*s));

    s->t = build_trie ((flags&SEARCH_CASE) == 0, argc, argv);
    s->words = argc;
    s->tags = g_strdup (tags);
    s->tage = g_strdup (tage);
    s->flags = flags;
    s->state = &s->t->root;
    s->matchcount = 0;

    g_queue_init (&s->input);
    g_queue_init (&s->output);
    s->current = NULL;

    s->offset = 0;
    s->offout = 0;

    /* rotating queue of previous character positions */
    m = s->t->max_depth+1;
    i = 2;
    while (i<m)
        i<<=2;
    s->last = g_malloc (sizeof (s->last[0])*i);
    s->last_mask = i-1;
    s->lastp = 0;

    /* a stack of possible submatches */
    s->submatchp = 0;
    s->submatches = g_malloc (sizeof (s->submatches[0])*argc+1);

    return s;
}

static void
searcher_free (struct _searcher *s)
{
    struct _token *t;

    while ((t = g_queue_pop_head (&s->input)) != NULL)
        g_free (t);
    while ((t = g_queue_pop_head (&s->output)) != NULL)
        g_free (t);
    g_free (s->tags);
    g_free (s->tage);
    g_free (s->last);
    g_free (s->submatches);
    free_trie (s->t);
    g_free (s);
}

static struct _token *
append_token (GQueue *queue, const gchar *tok, gint len)
{
    struct _token *token;

    if (len == -1)
        len = strlen (tok);
    token = g_malloc (sizeof (*token) + len+1);
    token->offset = 0;  /* set by caller when required */
    memcpy (token->tok, tok, len);
    token->tok[len] = 0;
    g_queue_push_tail (queue, token);

    return token;
}

#define free_token(x) (g_free (x))

static void
output_token (struct _searcher *s, struct _token *token)
{
    gint offend;
    gint left, pre;

    if (token->tok[0] == TAG_ESCAPE) {
        if (token->offset >= s->offout) {
            g_queue_push_tail (&s->output, token);
        } else {
            free_token (token);
        }
    } else {
        offend = token->offset + strlen (token->tok);
        left = offend-s->offout;
        if (left > 0) {
            pre = s->offout - token->offset;
            if (pre>0)
                memmove (token->tok, token->tok+pre, left+1);
            s->offout = offend;
            g_queue_push_tail (&s->output, token);
        } else {
            free_token (token);
        }
    }
}

static struct _token *
find_token (struct _searcher *s, gint start)
{
    GList *link;

    /* find token which is start token, from end of list back */
    link = g_queue_peek_tail_link (&s->input);
    while (link != NULL) {
        struct _token *token = link->data;

        if (token->offset <= start)
            return token;

        link = g_list_previous (link);
    }

    return NULL;
}

static void
output_match (struct _searcher *s, guint start, guint end)
{
    register struct _token *token;
    struct _token *starttoken, *endtoken;
    gchar b[8];

    d (printf("output match: %d-%d  at %d\n", start, end, s->offout));

    starttoken = find_token (s, start);
    endtoken = find_token (s, end);

    if (starttoken == NULL || endtoken == NULL) {
        d (printf("Cannot find match history for match %d-%d\n", start, end));
        return;
    }

    /* output pending stuff that didn't match afterall */
    while ((struct _token *) g_queue_peek_head (&s->input) != starttoken) {
        token = g_queue_pop_head (&s->input);
        output_token (s, token);
    }

    /* output any pre-match text */
    if (s->offout < start) {
        token = append_token (
            &s->output, starttoken->tok +
            (s->offout-starttoken->offset),
            start-s->offout);
        s->offout = start;
    }

    /* output highlight/bold */
    if (s->flags & SEARCH_BOLD) {
        sprintf(b, "%c<b>", (gchar)TAG_ESCAPE);
        append_token (&s->output, b, -1);
    }
    if (s->tags)
        append_token (&s->output, s->tags, -1);

    /* output match node (s) */
    if (starttoken != endtoken) {
        while ((struct _token *) g_queue_peek_head (&s->input) != endtoken) {
            token = g_queue_pop_head (&s->input);
            output_token (s, token);
        }
    }

    /* any remaining partial content */
    if (s->offout < end) {
        token = append_token (
            &s->output, endtoken->tok +
            (s->offout-endtoken->offset),
            end-s->offout);
        s->offout = end;
    }

    /* end highlight */
    if (s->tage)
        append_token (&s->output, s->tage, -1);

    /* and close bold if we need to */
    if (s->flags & SEARCH_BOLD) {
        sprintf(b, "%c</b>", (gchar)TAG_ESCAPE);
        append_token (&s->output, b, -1);
    }
}

/* output any sub-pending blocks */
static void
output_subpending (struct _searcher *s)
{
    gint i;

    for (i=s->submatchp-1;i>=0;i--)
        output_match (s, s->submatches[i].offstart, s->submatches[i].offend);
    s->submatchp = 0;
}

/* returns true if a merge took place */
static gint
merge_subpending (struct _searcher *s, gint offstart, gint offend)
{
    gint i;

    /* merges overlapping or abutting match strings */
    if (s->submatchp &&
        s->submatches[s->submatchp-1].offend >= offstart) {

        /* go from end, any that match 'invalidate' follow-on ones too */
        for (i=s->submatchp-1;i>=0;i--) {
            if (s->submatches[i].offend >= offstart) {
                if (offstart < s->submatches[i].offstart)
                    s->submatches[i].offstart = offstart;
                s->submatches[i].offend = offend;
                if (s->submatchp > i)
                    s->submatchp = i+1;
            }
        }
        return 1;
    }

    return 0;
}

static void
push_subpending (struct _searcher *s, gint offstart, gint offend)
{
    /* This is really an assertion, we just ignore the
     * last pending match instead of crashing though. */
    if (s->submatchp >= s->words) {
        d (printf("ERROR: submatch pending stack overflow\n"));
        s->submatchp = s->words-1;
    }

    s->submatches[s->submatchp].offstart = offstart;
    s->submatches[s->submatchp].offend = offend;
    s->submatchp++;
}

/* move any (partial) tokens from input to output
 * if they are beyond the current output position */
static void
output_pending (struct _searcher *s)
{
    struct _token *token;

    while ((token = g_queue_pop_head (&s->input)) != NULL)
        output_token (s, token);
}

/* flushes any nodes we cannot possibly match anymore */
static void
flush_extra (struct _searcher *s)
{
    guint start;
    gint i;
    struct _token *starttoken, *token;

    /* find earliest gchar that can be in contention */
    start = s->offset - s->t->max_depth;
    for (i=0;i<s->submatchp;i++)
        if (s->submatches[i].offstart < start)
            start = s->submatches[i].offstart;

    /* now, flush out any tokens which are before this point */
    starttoken = find_token (s, start);
    if (starttoken == NULL)
        return;

    while ((struct _token *) g_queue_peek_head (&s->input) != starttoken) {
        token = g_queue_pop_head (&s->input);
        output_token (s, token);
    }
}

static gchar *
searcher_next_token (struct _searcher *s)
{
    struct _token *token;
    const guchar *tok, *stok, *pre_tok;
    struct _trie *t = s->t;
    struct _state *q = s->state;
    struct _match *m = NULL;
    gint offstart, offend;
    guint32 c;

    while (g_queue_is_empty (&s->output)) {
        /* get next token */
        tok = (guchar *) s->next_token (s->next_data);
        if (tok == NULL) {
            output_subpending (s);
            output_pending (s);
            break;
        }

        /* we dont always have to copy each token, e.g. if we dont match anything */
        token = append_token (&s->input, (gchar *) tok, -1);
        token->offset = s->offset;
        tok = (guchar *) token->tok;

        /* tag test, reset state on unknown tags */
        if (tok[0] == TAG_ESCAPE) {
            if (!ignore_tag ((gchar *) tok)) {
                /* force reset */
                output_subpending (s);
                output_pending (s);
                q = &t->root;
            }

            continue;
        }

        /* process whole token */
        pre_tok = stok = tok;
        while ((c = camel_utf8_getc (&tok))) {
            if ((s->flags & SEARCH_CASE) == 0)
                c = g_unichar_tolower (c);
            while (q && (m = g (q, c)) == NULL)
                q = q->fail;
            if (q == NULL) {
                /* mismatch ... reset state */
                output_subpending (s);
                q = &t->root;
            } else if (m != NULL) {
                /* keep track of previous offsets of utf8 chars, rotating buffer */
                s->last[s->lastp] = s->offset + (pre_tok-stok);
                s->lastp = (s->lastp+1)&s->last_mask;

                q = m->match;
                /* we have a match of q->final characters for a matching word */
                if (q->final) {
                    s->matchcount++;

                    /* use the last buffer to find the real offset of this gchar */
                    offstart = s->last[(s->lastp - q->final)&s->last_mask];
                    offend = s->offset + (tok - stok);

                    if (q->matches == NULL) {
                        if (s->submatchp == 0) {
                            /* nothing pending, always put something in so we can try merge */
                            push_subpending (s, offstart, offend);
                        } else if (!merge_subpending (s, offstart, offend)) {
                            /* can't merge, output what we have, and start againt */
                            output_subpending (s);
                            push_subpending (s, offstart, offend);
                            /*output_match(s, offstart, offend);*/
                        } else if (g_queue_get_length (&s->input) > 8) {
                            /* we're continuing to match and merge,
                             * but we have a lot of stuff waiting,
                             * so flush it out now since this is a
                             * safe point to do it */
                            output_subpending (s);
                        }
                    } else {
                        /* merge/add subpending */
                        if (!merge_subpending (s, offstart, offend))
                            push_subpending (s, offstart, offend);
                    }
                }
            }
            pre_tok = tok;
        }

        s->offset += (pre_tok-stok);

        flush_extra (s);
    }

    s->state = q;

    if (s->current)
        free_token (s->current);

    s->current = token = g_queue_pop_head (&s->output);

    return token ? g_strdup (token->tok) : NULL;
}

static gchar *
searcher_peek_token (struct _searcher *s)
{
    gchar *tok;

    /* we just get it and then put it back, it's fast enuf */
    tok = searcher_next_token (s);
    if (tok) {
        /* need to clear this so we dont free it while its still active */
        g_queue_push_head (&s->output, s->current);
        s->current = NULL;
    }

    return tok;
}

static gint
searcher_pending (struct _searcher *s)
{
    return !(g_queue_is_empty (&s->input) && g_queue_is_empty (&s->output));
}

/* ********************************************************************** */

struct _search_info {
    GPtrArray *strv;
    gchar *color;
    guint size:8;
    guint flags:8;
};

/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

static struct _search_info *
search_info_new (void)
{
    struct _search_info *s;

    s = g_malloc0 (sizeof (struct _search_info));
    s->strv = g_ptr_array_new ();

    return s;
}

static void
search_info_set_flags (struct _search_info *si, guint flags, guint mask)
{
    si->flags = (si->flags & ~mask) | (flags & mask);
}

static void
search_info_set_color (struct _search_info *si, const gchar *color)
{
    g_free (si->color);
    si->color = g_strdup (color);
}

static void
search_info_add_string (struct _search_info *si, const gchar *s)
{
    const guchar *start;
    guint32 c;

    if (s && s[0]) {
        const guchar *us = (guchar *) s;
        /* strip leading whitespace */
        start = us;
        while ((c = camel_utf8_getc (&us))) {
            if (!g_unichar_isspace (c)) {
                break;
            }
            start = us;
        }
        /* should probably also strip trailing, but i'm lazy today */
        if (start[0])
            g_ptr_array_add (si->strv, g_strdup ((gchar *) start));
    }
}

static void
search_info_clear (struct _search_info *si)
{
    gint i;

    for (i=0;i<si->strv->len;i++)
        g_free (si->strv->pdata[i]);

    g_ptr_array_set_size (si->strv, 0);
}

static void
search_info_free (struct _search_info *si)
{
    gint i;

    for (i=0;i<si->strv->len;i++)
        g_free (si->strv->pdata[i]);

    g_ptr_array_free (si->strv, TRUE);
    g_free (si->color);
    g_free (si);
}

static struct _search_info *
search_info_clone (struct _search_info *si)
{
    struct _search_info *out;
    gint i;

    out = search_info_new ();
    for (i=0;i<si->strv->len;i++)
        g_ptr_array_add (out->strv, g_strdup (si->strv->pdata[i]));
    out->color = g_strdup (si->color);
    out->flags = si->flags;
    out->size = si->size;

    return out;
}

static struct _searcher *
search_info_to_searcher (struct _search_info *si)
{
    gchar *tags, *tage;
    const gchar *col;

    if (si->strv->len == 0)
        return NULL;

    if (si->color == NULL)
        col = "red";
    else
        col = si->color;

    tags = g_alloca (20+strlen (col));
    sprintf(tags, "%c<font color=\"%s\">", TAG_ESCAPE, col);
    tage = g_alloca (20);
    sprintf(tage, "%c</font>", TAG_ESCAPE);

    return searcher_new (
        si->flags, si->strv->len,
        (guchar **) si->strv->pdata, tags, tage);
}

/* ********************************************************************** */

struct _ESearchingTokenizerPrivate {
    struct _search_info *primary, *secondary;
    struct _searcher *engine;
};

/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

/* blah blah the htmltokeniser doesn't like being asked
   for a token if it doens't hvae any! */
static gchar *
get_token (HTMLTokenizer *tokenizer)
{
    HTMLTokenizerClass *class;

    class = HTML_TOKENIZER_CLASS (e_searching_tokenizer_parent_class);

    if (class->has_more (tokenizer))
        return class->next_token (tokenizer);

    return NULL;
}

/* proxy matched event, not sure what its for otherwise */
static void
matched (ESearchingTokenizer *tokenizer)
{
    /*++tokenizer->priv->match_count;*/
    g_signal_emit (tokenizer, signals[MATCH_SIGNAL], 0);
}

/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */

static void
searching_tokenizer_finalize (GObject *object)
{
    ESearchingTokenizerPrivate *priv;

    priv = E_SEARCHING_TOKENIZER (object)->priv;

    search_info_free (priv->primary);
    search_info_free (priv->secondary);

    if (priv->engine != NULL)
        searcher_free (priv->engine);

    /* Chain up to parent's finalize () method. */
    G_OBJECT_CLASS (e_searching_tokenizer_parent_class)->finalize (object);
}

static void
searching_tokenizer_begin (HTMLTokenizer *tokenizer,
                           const gchar *content_type)
{
    ESearchingTokenizerPrivate *priv;

    priv = E_SEARCHING_TOKENIZER (tokenizer)->priv;

    /* reset search */
    if (priv->engine != NULL) {
        searcher_free (priv->engine);
        priv->engine = NULL;
    }

    if ((priv->engine = search_info_to_searcher (priv->primary))
        || (priv->engine = search_info_to_searcher (priv->secondary))) {
        searcher_set_tokenfunc (priv->engine, get_token, tokenizer);
    }
    /* else - no engine, no search active */

    /* Chain up to parent's begin() method. */
    HTML_TOKENIZER_CLASS (e_searching_tokenizer_parent_class)->
        begin (tokenizer, content_type);
}

static gchar *
searching_tokenizer_peek_token (HTMLTokenizer *tokenizer)
{
    ESearchingTokenizerPrivate *priv;

    priv = E_SEARCHING_TOKENIZER (tokenizer)->priv;

    if (priv->engine != NULL)
        return searcher_peek_token (priv->engine);

    /* Chain up to parent's peek_token() method. */
    return HTML_TOKENIZER_CLASS (e_searching_tokenizer_parent_class)->
        peek_token (tokenizer);
}

static gchar *
searching_tokenizer_next_token (HTMLTokenizer *tokenizer)
{
    ESearchingTokenizerPrivate *priv;
    gint oldmatched;
    gchar *token;

    priv = E_SEARCHING_TOKENIZER (tokenizer)->priv;

    /* If no search is active, just use the default method. */
    if (priv->engine == NULL)
        return HTML_TOKENIZER_CLASS (
            e_searching_tokenizer_parent_class)->
            next_token (tokenizer);

    oldmatched = priv->engine->matchcount;

    token = searcher_next_token (priv->engine);

    /* not sure if this has to be accurate or just say we had some matches */
    if (oldmatched != priv->engine->matchcount)
        g_signal_emit (tokenizer, signals[MATCH_SIGNAL], 0);

    return token;
}

static gboolean
searching_tokenizer_has_more (HTMLTokenizer *tokenizer)
{
    ESearchingTokenizerPrivate *priv;

    priv = E_SEARCHING_TOKENIZER (tokenizer)->priv;

    return (priv->engine != NULL && searcher_pending (priv->engine)) ||
        HTML_TOKENIZER_CLASS (e_searching_tokenizer_parent_class)->
            has_more (tokenizer);
}

static HTMLTokenizer *
searching_tokenizer_clone (HTMLTokenizer *tokenizer)
{
    ESearchingTokenizer *orig_st;
    ESearchingTokenizer *new_st;

    orig_st = E_SEARCHING_TOKENIZER (tokenizer);
    new_st = e_searching_tokenizer_new ();

    search_info_free (new_st->priv->primary);
    search_info_free (new_st->priv->secondary);

    new_st->priv->primary = search_info_clone (orig_st->priv->primary);
    new_st->priv->secondary = search_info_clone (orig_st->priv->secondary);

    g_signal_connect_swapped (
        new_st, "match", G_CALLBACK (matched), orig_st);

    return HTML_TOKENIZER (new_st);
}
static void
e_searching_tokenizer_class_init (ESearchingTokenizerClass *class)
{
    GObjectClass *object_class;
    HTMLTokenizerClass *tokenizer_class;

    g_type_class_add_private (class, sizeof (ESearchingTokenizerPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = searching_tokenizer_finalize;

    tokenizer_class = HTML_TOKENIZER_CLASS (class);
    tokenizer_class->begin = searching_tokenizer_begin;
    tokenizer_class->peek_token = searching_tokenizer_peek_token;
    tokenizer_class->next_token = searching_tokenizer_next_token;
    tokenizer_class->has_more = searching_tokenizer_has_more;
    tokenizer_class->clone = searching_tokenizer_clone;

    signals[MATCH_SIGNAL] = g_signal_new (
        "match",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ESearchingTokenizerClass, match),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);
}

static void
e_searching_tokenizer_init (ESearchingTokenizer *tokenizer)
{
    tokenizer->priv = G_TYPE_INSTANCE_GET_PRIVATE (
        tokenizer, E_TYPE_SEARCHING_TOKENIZER,
        ESearchingTokenizerPrivate);

    tokenizer->priv->primary = search_info_new ();
    search_info_set_flags (
        tokenizer->priv->primary,
        SEARCH_BOLD, SEARCH_CASE | SEARCH_BOLD);
    search_info_set_color (tokenizer->priv->primary, "red");

    tokenizer->priv->secondary = search_info_new ();
    search_info_set_flags (
        tokenizer->priv->secondary,
        SEARCH_BOLD, SEARCH_CASE | SEARCH_BOLD);
    search_info_set_color (tokenizer->priv->secondary, "purple");
}

ESearchingTokenizer *
e_searching_tokenizer_new (void)
{
    return g_object_new (E_TYPE_SEARCHING_TOKENIZER, NULL);
}

void
e_searching_tokenizer_set_primary_search_string (ESearchingTokenizer *tokenizer,
                                                 const gchar *primary_string)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_clear (tokenizer->priv->primary);
    search_info_add_string (tokenizer->priv->primary, primary_string);
}

void
e_searching_tokenizer_add_primary_search_string (ESearchingTokenizer *tokenizer,
                                                 const gchar *primary_string)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_add_string (tokenizer->priv->primary, primary_string);
}

void
e_searching_tokenizer_set_primary_case_sensitivity (ESearchingTokenizer *tokenizer,
                                                    gboolean case_sensitive)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_set_flags (
        tokenizer->priv->primary,
        case_sensitive ? SEARCH_CASE : 0, SEARCH_CASE);
}

void
e_searching_tokenizer_set_secondary_search_string (ESearchingTokenizer *tokenizer,
                                                   const gchar *secondary_string)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_clear (tokenizer->priv->secondary);
    search_info_add_string (tokenizer->priv->secondary, secondary_string);
}

void
e_searching_tokenizer_add_secondary_search_string (ESearchingTokenizer *tokenizer,
                                                   const gchar *secondary_string)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_add_string (tokenizer->priv->secondary, secondary_string);
}

void
e_searching_tokenizer_set_secondary_case_sensitivity (ESearchingTokenizer *tokenizer,
                                                      gboolean case_sensitive)
{
    g_return_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer));

    search_info_set_flags (
        tokenizer->priv->secondary,
        case_sensitive ? SEARCH_CASE : 0, SEARCH_CASE);
}

/* Note: only returns the primary search string count */
gint
e_searching_tokenizer_match_count (ESearchingTokenizer *tokenizer)
{
    g_return_val_if_fail (E_IS_SEARCHING_TOKENIZER (tokenizer), -1);

    if (tokenizer->priv->engine && tokenizer->priv->primary->strv->len)
        return tokenizer->priv->engine->matchcount;

    return 0;
}