Skip to content

Commit

Permalink
Modifying grammar to check start fo regex quickly to speed-up attribu…
Browse files Browse the repository at this point in the history
…te parsing
  • Loading branch information
CharlesC87 committed Feb 13, 2023
1 parent b5a8c54 commit b29acd7
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 82 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ set(
src/grammar.cpp
src/parser.cpp
src/writer.cpp
src/matcher.cpp
)

set(
HEADER_FILES
include/sdptransform.hpp
include/json.hpp
include/string_view.hpp
include/string_view_lite.hpp
include/matcher.hpp
)

add_library(sdptransform STATIC ${SOURCE_FILES} ${HEADER_FILES})
Expand Down
49 changes: 49 additions & 0 deletions include/matcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "string_view.hpp"
#include <vector>
#include <regex>
#include <memory>

namespace sdptransform
{
namespace matcher
{
class Interface {
public:
virtual bool doMatch(const std::string& content) const = 0;
using Results=std::vector<string_view>;
virtual Results getMatches(const std::string& content) const = 0;
};
using Ptr=std::shared_ptr<Interface>;

class Regex final : public Interface {
public:
Regex(std::string&& regex) : regex_(std::move(regex)) {}
Regex(const char* prefix, std::string&& regex) : regex_(std::move(regex)), prefix_(prefix) {}
bool doMatch(const std::string& content) const override;
Results getMatches(const std::string& content) const override;
private:
std::pair<std::string::const_iterator,std::string::const_iterator> getIteratorAfterPrefixMatch(const std::string& content) const;
std::regex regex_;
std::string prefix_={};
};

template<typename... T>
inline std::shared_ptr<Regex> createRegex(T&&... vals) {
return std::make_shared<Regex>(std::forward<T>(vals)...);
}

class WholeMatcher final : public Interface {
public:
bool doMatch(const std::string&) const override { return true; }
Results getMatches(const std::string& content) const override { return {string_view(content)}; }
};

inline std::shared_ptr<WholeMatcher> createWholeMatcher() {
return std::make_shared<WholeMatcher>();
}

} // namespace matcher

} // namespace sdptransform
4 changes: 2 additions & 2 deletions include/sdptransform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <string>
#include <vector>
#include <map>
#include <regex>
#include <functional>
#include "matcher.hpp"

using json = nlohmann::json;

Expand All @@ -18,7 +18,7 @@ namespace sdptransform
{
std::string name;
std::string push;
std::regex reg;
matcher::Ptr reg;
std::vector<std::string> names;
std::vector<char> types;
std::string format;
Expand Down
6 changes: 4 additions & 2 deletions include/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <string_view>
namespace sdptransform {
using string_view=std::string_view;
string_view convertMatchToView(const std::ssub_match& match) {
inline string_view convertMatchToView(const std::ssub_match& match)
{
#if __cplusplus >= 202002L && __cpp_lib_concepts
return string_view(match.first, match.second);
#else
Expand All @@ -18,7 +19,8 @@ namespace sdptransform {
#include <string_view_lite.hpp>
namespace sdptransform {
using string_view=string_view_lite;
string_view convertMatchToView(const std::ssub_match& match) {
inline string_view convertMatchToView(const std::ssub_match& match)
{
return string_view(match.first, match.second);
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/string_view_lite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace sdptransform
}
}
string_view_lite(std::string::const_iterator begin, std::string::const_iterator end) : data_(&(*begin)), size_(end - begin) {}
string_view_lite(const std::string &s) : data_(s.data()), size_(s.size()) {}
explicit string_view_lite(const std::string &s) : data_(s.data()), size_(s.size()) {}

// iterator support
constexpr const_iterator begin() const noexcept { return data_; }
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace sdptransform
size_type size_ = 0U; // exposition only
};

std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &
inline std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &
operator<<(std::basic_ostream<string_view_lite::value_type, string_view_lite::Traits_type> &os,
string_view_lite str)
{
Expand Down
Loading

0 comments on commit b29acd7

Please sign in to comment.