Skip to content

Commit b29acd7

Browse files
committed
Modifying grammar to check start fo regex quickly to speed-up attribute parsing
1 parent b5a8c54 commit b29acd7

9 files changed

+179
-82
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ set(
1818
src/grammar.cpp
1919
src/parser.cpp
2020
src/writer.cpp
21+
src/matcher.cpp
2122
)
2223

2324
set(
2425
HEADER_FILES
2526
include/sdptransform.hpp
2627
include/json.hpp
28+
include/string_view.hpp
29+
include/string_view_lite.hpp
30+
include/matcher.hpp
2731
)
2832

2933
add_library(sdptransform STATIC ${SOURCE_FILES} ${HEADER_FILES})

include/matcher.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include "string_view.hpp"
4+
#include <vector>
5+
#include <regex>
6+
#include <memory>
7+
8+
namespace sdptransform
9+
{
10+
namespace matcher
11+
{
12+
class Interface {
13+
public:
14+
virtual bool doMatch(const std::string& content) const = 0;
15+
using Results=std::vector<string_view>;
16+
virtual Results getMatches(const std::string& content) const = 0;
17+
};
18+
using Ptr=std::shared_ptr<Interface>;
19+
20+
class Regex final : public Interface {
21+
public:
22+
Regex(std::string&& regex) : regex_(std::move(regex)) {}
23+
Regex(const char* prefix, std::string&& regex) : regex_(std::move(regex)), prefix_(prefix) {}
24+
bool doMatch(const std::string& content) const override;
25+
Results getMatches(const std::string& content) const override;
26+
private:
27+
std::pair<std::string::const_iterator,std::string::const_iterator> getIteratorAfterPrefixMatch(const std::string& content) const;
28+
std::regex regex_;
29+
std::string prefix_={};
30+
};
31+
32+
template<typename... T>
33+
inline std::shared_ptr<Regex> createRegex(T&&... vals) {
34+
return std::make_shared<Regex>(std::forward<T>(vals)...);
35+
}
36+
37+
class WholeMatcher final : public Interface {
38+
public:
39+
bool doMatch(const std::string&) const override { return true; }
40+
Results getMatches(const std::string& content) const override { return {string_view(content)}; }
41+
};
42+
43+
inline std::shared_ptr<WholeMatcher> createWholeMatcher() {
44+
return std::make_shared<WholeMatcher>();
45+
}
46+
47+
} // namespace matcher
48+
49+
} // namespace sdptransform

include/sdptransform.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <string>
66
#include <vector>
77
#include <map>
8-
#include <regex>
98
#include <functional>
9+
#include "matcher.hpp"
1010

1111
using json = nlohmann::json;
1212

@@ -18,7 +18,7 @@ namespace sdptransform
1818
{
1919
std::string name;
2020
std::string push;
21-
std::regex reg;
21+
matcher::Ptr reg;
2222
std::vector<std::string> names;
2323
std::vector<char> types;
2424
std::string format;

include/string_view.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <string_view>
77
namespace sdptransform {
88
using string_view=std::string_view;
9-
string_view convertMatchToView(const std::ssub_match& match) {
9+
inline string_view convertMatchToView(const std::ssub_match& match)
10+
{
1011
#if __cplusplus >= 202002L && __cpp_lib_concepts
1112
return string_view(match.first, match.second);
1213
#else
@@ -18,7 +19,8 @@ namespace sdptransform {
1819
#include <string_view_lite.hpp>
1920
namespace sdptransform {
2021
using string_view=string_view_lite;
21-
string_view convertMatchToView(const std::ssub_match& match) {
22+
inline string_view convertMatchToView(const std::ssub_match& match)
23+
{
2224
return string_view(match.first, match.second);
2325
}
2426
}

include/string_view_lite.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace sdptransform
3838
}
3939
}
4040
string_view_lite(std::string::const_iterator begin, std::string::const_iterator end) : data_(&(*begin)), size_(end - begin) {}
41-
string_view_lite(const std::string &s) : data_(s.data()), size_(s.size()) {}
41+
explicit string_view_lite(const std::string &s) : data_(s.data()), size_(s.size()) {}
4242

4343
// iterator support
4444
constexpr const_iterator begin() const noexcept { return data_; }
@@ -176,7 +176,7 @@ namespace sdptransform
176176
size_type size_ = 0U; // exposition only
177177
};
178178

179-
std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &
179+
inline std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &
180180
operator<<(std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &os,
181181
string_view_lite str)
182182
{

0 commit comments

Comments
 (0)