File size: 1,035 Bytes
4a28d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) OpenMMLab. All rights reserved.

#include <iostream>
#include <regex>
#include <string>
#include <vector>

namespace turbomind {

std::vector<std::pair<std::string, std::string>> ParseArgsList(const std::string& str)
{
    const std::regex regex(R"((\w+)=([^,\[\(]+|\[.*\]|\(.*\)))");

    std::sregex_iterator beg(str.begin(), str.end(), regex);
    std::sregex_iterator end{};

    std::vector<std::pair<std::string, std::string>> ret;
    for (auto it = beg; it != end; ++it) {
        std::smatch match = *it;
        ret.emplace_back(match[1], match[2]);
    }

    return ret;
}

std::vector<std::string> ParseListOrTuple(const std::string& str)
{
    const std::regex regex(R"([,\[\]\(\)]+)");

    std::vector<std::string> ret;
    std::copy_if(std::sregex_token_iterator(str.begin(), str.end(), regex, -1),
                 std::sregex_token_iterator{},
                 std::back_inserter(ret),
                 [](const std::string& s) { return !s.empty(); });

    return ret;
}

}  // namespace turbomind