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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// license:MIT
// copyright-holders:Alfred Bratterud
// NOTE: Author allowed MAME project to distribute this file under MIT
// license. Other projects need to do it under Apache 2 license
//
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// https://github.com/pillarjs/path-to-regexp/blob/master/index.js
#ifndef PATH_TO_REGEX_HPP
#define PATH_TO_REGEX_HPP
#include <map>
#include <regex>
#include <vector>
namespace path2regex {
struct Token {
std::string name {}; // can be a string or an int (index)
std::string prefix {};
std::string delimiter {};
std::string pattern {};
bool optional {false};
bool repeat {false};
bool partial {false};
bool asterisk {false};
bool is_string {false}; // If it is a string we only put/have a string in the name-attribute (path in parse-method)
// So if this is true, we can ignore all attributes except name
void set_string_token(const std::string& name_) {
name = name_;
is_string = true;
}
}; //< struct Token
using Keys = std::vector<Token>;
using Tokens = std::vector<Token>;
using Options = std::map<std::string, bool>;
/**
* Creates a path-regex from string input (path)
* Updates keys-vector (empty input parameter)
*
* Calls parse-method and then tokens_to_regex-method based on the tokens returned from parse-method
* Puts the tokens that are keys (not string-tokens) into keys-vector
*
* std::vector<Token> keys (empty)
* Is created outside the class and sent in as parameter
* One Token-object in the keys-vector per parameter found in the path
*
* std::map<std::string, bool> options (optional)
* Can contain bool-values for the keys "sensitive", "strict" and/or "end"
* Default:
* strict = false
* sensitive = false
* end = true
*/
std::regex path_to_regex(const std::string& path, Keys& keys, const Options& options = Options{});
/**
* Creates a path-regex from string input (path)
*
* Calls parse-method and then tokens_to_regex-method based on the tokens returned from parse-method
*
* std::map<std::string, bool> options (optional)
* Can contain bool-values for the keys "sensitive", "strict" and/or "end"
* Default:
* strict = false
* sensitive = false
* end = true
*/
std::regex path_to_regex(const std::string& path, const Options& options = Options{});
/**
* Creates vector of tokens based on the given string (this vector of tokens can be sent as
* input to tokens_to_regex-method and includes tokens that are strings, not only tokens
* that are parameters in str)
*/
Tokens parse(const std::string& str);
/**
* Creates a regex based on the tokens and options (optional) given
*/
std::regex tokens_to_regex(const Tokens& tokens, const Options& options = Options{});
/**
* Goes through the tokens-vector and push all tokens that are not string-tokens
* onto keys-vector
*/
void tokens_to_keys(const Tokens& tokens, Keys& keys);
} //< namespace path2regex
#endif //< PATH_TO_REGEX_HPP
|