aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/weighted_set_parser.hpp
blob: 7a3abbc7bc59ffd9bd63f11d4c6efe6ef74641fb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "weighted_set_parser.h"
#include <vespa/vespalib/util/issue.h>

using vespalib::Issue;

namespace search::features {

template <typename OutputType>
void
WeightedSetParser::parse(const vespalib::string &input, OutputType &output)
{
    size_t len = input.size();
    // Note that we still handle '(' and ')' for backward compatibility.
    if (len >= 2 && ((input[0] == '{' && input[len - 1] == '}') ||
                     (input[0] == '(' && input[len - 1] == ')')) ) {
        vespalib::stringref s(input.c_str()+1, len - 2);
        while ( ! s.empty() ) {
            vespalib::string::size_type commaPos(s.find(','));
            vespalib::stringref item(s.substr(0, commaPos));
            vespalib::string::size_type colonPos(item.find(':'));
            if (colonPos != vespalib::string::npos) {
                vespalib::string tmpKey(item.substr(0, colonPos));
                vespalib::string::size_type start(tmpKey.find_first_not_of(' '));
                vespalib::stringref key(tmpKey.data() + start, colonPos - start);
                vespalib::stringref value(item.substr(colonPos+1));
                output.insert(key, value);
            } else {
                Issue::report("weighted set parser: Could not parse item '%s' in input string '%s', skipping. "
                              "Expected ':' between key and weight.", vespalib::string(item).c_str(), input.c_str());
            }
            if (commaPos != vespalib::string::npos) {
                s = s.substr(commaPos+1);
            } else {
                s = vespalib::stringref();
            }
        }
    } else {
        Issue::report("weighted set parser: Could not parse input string '%s'. "
                      "Expected surrounding '(' and ')' or '{' and '}'.", input.c_str());
    }
}

}