BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
encode_collection.h
Go to the documentation of this file.
1#ifndef BPMNOS_encode_collection_H
2#define BPMNOS_encode_collection_H
3
4#include <string>
5#include <regex>
6#include <cassert>
7#include <iostream>
8
10
11namespace BPMNOS {
12
13/// Function to replace a collection that is not preceded by an alphanumeric or underscore
14inline std::string encodeCollection(std::string text) {
15 std::string pattern = "(^|[^a-zA-Z0-9_])\\[(.*?)\\]";// "[\\s]*\\[(.*?)\\]";
16 std::regex regular_expression(pattern); // Match "[" ... "]" that is not preceded by an alphanumeric or underscore
17 std::smatch match;
18
19 while (std::regex_search(text, match, regular_expression)) {
20/*
21std::cerr << match[0].str() << std::endl;
22std::cerr << match[1].str() << std::endl;
23std::cerr << match[2].str() << std::endl;
24 assert(match[1].str().front() == '[' && match[1].str().back() == ']');
25*/
26 if ( match[2].str().contains("[") ) {
27 throw std::runtime_error("Nested collections are not supported");
28 }
29 std::string collection = "[" + match[2].str() + "]";
30 // Convert collection to a number using the registry
31 auto id = collectionRegistry(collection);
32
33 // Replace the matched substring with the number
34 size_t startPos = (size_t)match.position(2)-1;
35 size_t length = (size_t)match.length(2)+2;
36 text.replace(startPos, length, std::to_string(id));
37 }
38 return text;
39}
40
41} // namespace BPMNOS
42#endif // BPMNOS_encode_collection_H
CollectionRegistry collectionRegistry
std::string encodeCollection(std::string text)
Function to replace a collection that is not preceded by an alphanumeric or underscore.