BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
encode_quoted_strings.h
Go to the documentation of this file.
1#ifndef BPMNOS_encode_quoted_strings_H
2#define BPMNOS_encode_quoted_strings_H
3
4#include <string>
5#include <unordered_map>
6#include <regex>
7#include <stack>
8#include <tuple>
9#include <iostream>
10
11#include "StringRegistry.h"
12
13namespace BPMNOS {
14
15// Function to replace all strings within double quotes
16inline std::string encodeQuotedStrings(std::string text) {
17 std::regex doubleQuoteRegex("\"([^\"]*)\""); // Match strings within double quotes
18 std::stack< std::tuple<size_t, size_t, long unsigned int> > replacements;
19
20 // collect all matches and their positions
21 auto begin = std::sregex_iterator(text.begin(), text.end(), doubleQuoteRegex);
22 auto end = std::sregex_iterator();
23
24 for (auto it = begin; it != end; ++it) {
25 std::string matchedString = (*it)[1]; // Extract content inside quotes
26 auto id = stringRegistry(matchedString);
27 replacements.emplace(it->position(), it->length(), id);
28 }
29
30 // replace matches in reverse order to preserve positions
31 while (!replacements.empty()) {
32 auto& [startPos, length, id] = replacements.top();
33 text.replace(startPos, length, std::to_string(id));
34 replacements.pop();
35 }
36
37 return text;
38}
39
40} // namespace BPMNOS
41#endif // BPMNOS_encode_quoted_strings_H
StringRegistry stringRegistry
std::string encodeQuotedStrings(std::string text)