aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/juniper/SummaryConfig.cpp
blob: efd4dc1d1a8146af355c01612e83d73f611125d2 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#define _NEED_SUMMARY_CONFIG_IMPL 1
#include "SummaryConfig.h"
#include <string>
#include <cstring>

SummaryConfig* CreateSummaryConfig(const char* highlight_on,
				   const char* highlight_off,
				   const char* dots,
				   const char* separators,
				   const unsigned char* connectors,
				   const ConfigFlag escape_markup,
				   const ConfigFlag preserve_white_space)
{
    return new SummaryConfig(highlight_on, highlight_off, dots, separators,
                             connectors, escape_markup, preserve_white_space);
}


void DeleteSummaryConfig(SummaryConfig*& sumconf)
{
    delete sumconf;
    sumconf = NULL;
}


inline char hexchar(const char* s)
{
    const char* str = s;
    unsigned char c = 0;
    for (int i = 0; i < 2; i++)
    {
        if (*str <= 'F')
            c |= (*str - '0');
        else
            c |= (*str - 'a' + 10);
        c = c << ((1 - i)*4);
        str++;
    }
    return (char)c;
}


SummaryConfig::SummaryConfig(const char* hi_on, const char* hi_off,
                             const char* usedots, const char* separators,
                             const unsigned char* connectors,
                             ConfigFlag esc_markup,
                             ConfigFlag preserve_white_space_)
    : _highlight_on(""),
      _highlight_off(""),
      _dots(""),
      _separator(),
      _connector(),
      _escape_markup(esc_markup),
      _preserve_white_space(preserve_white_space_)
{
    init(_highlight_on, hi_on);
    init(_highlight_off, hi_off);
    init(_dots, usedots);

    for (const char* c = separators; *c != '\0'; c++) {
        if (*c > 0) _separator.set(*c, 1);
    }
    for (const unsigned char* uc = connectors; *uc != '\0'; uc++) {
        if (*uc > 0) _connector.set(*uc, 1);
    }
}

void SummaryConfig::init(std::string& cf, const char* str)
{
    bool escape = false;
    for (;str && *str != '\0'; str++) {
        if (!escape && *str == '\\') {
            escape = true;
        } else {
            if (escape) {
                // Allow space to be encoded as \_ (fsearchrc does not accept spaces..)
                if (*str == '_') {
                    cf += ' ';
                    escape = false;
                    continue;
                } else if (isxdigit(*str) && isxdigit(*(str+1))) {
                    cf += hexchar(str);
                    str++;
                    escape = false;
                    continue;
                } else {
                    escape = false;
                }
            }
            cf += *str;
        }
    }
}


ConfigFlag StringToConfigFlag(const char* confstring)
{
    if (strcmp(confstring, "off") == 0)
        return CF_OFF;
    if (strcmp(confstring, "on") == 0)
        return CF_ON;
    // default:
    return CF_AUTO;
}