aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/string_stuff.h
blob: bb18298a02cefd8e34ba6d2f27aa692624e3e699 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "tensor_spec.h"
#include <vespa/vespalib/stllike/string.h>

namespace vespalib::eval {

/**
 * Helper class used to insert commas on the appropriate places in
 * comma-separated textual lists. Can also be used to figure out when
 * to expect commas when parsing text.
 **/
struct CommaTracker {
    bool first;
    CommaTracker() noexcept : first(true) {}
    CommaTracker(bool first_in) noexcept : first(first_in) {}
    bool maybe_add_comma(vespalib::string &dst) {
        if (first) {
            first = false;
            return false;
        } else {
            dst.push_back(',');
            return true;
        }
    }
    template <typename T>
    bool maybe_eat_comma(T &ctx) {
        if (first) {
            first = false;
            return false;
        } else {
            ctx.eat(',');
            return true;
        }
    }
};

/**
 * Convert the given string to a quoted string with escaped special characters.
 **/
vespalib::string as_quoted_string(const vespalib::string &str);

/**
 * Is this string a positive integer (dimension index)
 **/
bool is_number(const vespalib::string &str);

/**
 * Convert this string to a positive integer (dimension index)
 **/
size_t as_number(const vespalib::string &str);

/**
 * Convert a tensor spec address into a string on the form:
 * '{dim1:label,dim2:index, ...}'
 **/
vespalib::string as_string(const TensorSpec::Address &address);

}