blob: 41ae92e5b76b1ebf9a2ace35972b5d5d87e31be9 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "replace_variable.h"
#include "asciistream.h"
#include <ctype.h>
namespace vespalib {
vespalib::string
replace_variable(const vespalib::string &input,
const vespalib::string &variable,
const vespalib::string &replacement)
{
vespalib::asciistream result;
bool is_in_word = false;
size_t last_word_start = 0;
size_t last_word_size = 0;
for (size_t i = 0; i < input.size(); ++i) {
char c = input[i];
if (isalnum(c) || (c == '_')) {
if (! is_in_word) {
last_word_start = i;
last_word_size = 0;
is_in_word = true;
}
++last_word_size;
} else {
if (is_in_word) {
vespalib::string last_word = input.substr(last_word_start, last_word_size);
if (last_word == variable) {
result << replacement;
} else {
result << last_word;
}
is_in_word = false;
}
result << c;
}
}
if (is_in_word) {
vespalib::string last_word = input.substr(last_word_start, last_word_size);
if (last_word == variable) {
result << replacement;
} else {
result << last_word;
}
}
return result.str();
}
} // namespace
|