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 <strutil.h>
8
10#include "Keywords.h"
11
12namespace BPMNOS {
13
14/// Function to replace a collection that is not preceded by an alphanumeric or underscore
15inline std::string encodeCollection(std::string text) {
16 std::string pattern = "(^|[^a-zA-Z0-9_])\\[(.*?)\\]";// "[\\s]*\\[(.*?)\\]";
17 std::regex regular_expression(pattern); // Match "[" ... "]" that is not preceded by an alphanumeric or underscore
18 std::smatch match;
19
20 while (std::regex_search(text, match, regular_expression)) {
21 if ( match[2].str().contains("[") ) {
22 throw std::runtime_error("Nested collections are not supported");
23 }
24 std::vector<double> collection;
25 for ( auto value : strutil::split(match[2].str(),',') ) {
26 strutil::trim(value);
27 if ( value == Keyword::False ) {
28 collection.push_back( false );
29 }
30 else if ( value == Keyword::True ) {
31 collection.push_back( true );
32 }
33 else {
34 try {
35 // try to convert to number
36 collection.push_back( std::stod(value) );
37 }
38 catch(...) {
39 throw std::runtime_error("BPMNOS: illegal value '" + value + "' in '" + text + "'");
40 }
41 }
42 }
43 // Convert collection to a number using the registry
44 auto id = collectionRegistry(collection);
45
46 // Replace the matched substring with the number
47 size_t startPos = (size_t)match.position(2)-1;
48 size_t length = (size_t)match.length(2)+2;
49 text.replace(startPos, length, std::to_string(id));
50 }
51 return text;
52}
53
54} // namespace BPMNOS
55#endif // BPMNOS_encode_collection_H
CollectionRegistry collectionRegistry
const std::string False
Definition Keywords.h:8
const std::string True
Definition Keywords.h:9
std::string encodeCollection(std::string text)
Function to replace a collection that is not preceded by an alphanumeric or underscore.