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
|
#include "rust-cfg-parser.h"
#include "selftest.h"
namespace Rust {
bool
parse_cfg_option (const std::string &input, std::string &key,
std::string &value)
{
key.clear ();
value.clear ();
auto equal = input.find ('=');
// If there is no equal sign, it means there is no value. Clean up the key
// and return
if (equal == std::string::npos)
{
key = input;
// FIXME: Make sure key is a proper identifier
return true;
}
key = input.substr (0, equal);
auto remaining_input = input.substr (equal + 1);
if (remaining_input[0] != '"' || remaining_input.back () != '"')
return false;
// Remove the quotes around the value, by advancing one character
value = remaining_input.substr (1);
// And trimming the rightmost character. This is fine since we've already
// checked that both the first and last characters were quotes.
value.resize (value.size () - 1);
// FIXME: We need to sanitize here and make sure that both key and value
// are proper identifiers
return true;
}
} // namespace Rust
#if CHECKING_P
namespace selftest {
void
rust_cfg_parser_test (void)
{
std::string key;
std::string value;
ASSERT_TRUE (Rust::parse_cfg_option ("key-no-value", key, value));
ASSERT_EQ (key, "key-no-value");
ASSERT_TRUE (value.empty ());
ASSERT_TRUE (Rust::parse_cfg_option ("k=\"v\"", key, value));
ASSERT_EQ (key, "k");
ASSERT_EQ (value, "v");
// values should be between double quotes
ASSERT_FALSE (Rust::parse_cfg_option ("k=v", key, value));
// No value is an error if there is an equal sign
ASSERT_FALSE (Rust::parse_cfg_option ("k=", key, value));
// No key is an error
ASSERT_FALSE (Rust::parse_cfg_option ("=", key, value));
ASSERT_FALSE (Rust::parse_cfg_option ("=value", key, value));
}
} // namespace selftest
#endif // CHECKING_P
|