adamdebreceni commented on code in PR #1692:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1692#discussion_r1423922940


##########
extensions/standard-processors/utils/JoltUtils.cpp:
##########
@@ -0,0 +1,1134 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JoltUtils.h"
+#include "rapidjson/error/en.h"
+#include "Exception.h"
+
+namespace org::apache::nifi::minifi::utils::jolt {
+
+
+static bool isSpecialChar(char ch) {
+  static constexpr std::array SPECIAL_CHARS{'.', '[', ']', '$', '&', '@', '#', 
'*'};
+  return std::find(SPECIAL_CHARS.begin(), SPECIAL_CHARS.end(), ch) != 
SPECIAL_CHARS.end();
+}

Review Comment:
   I would keep them separate, as these chars do trigger special behavior while 
`\` is there to suppress them



##########
extensions/standard-processors/utils/JoltUtils.cpp:
##########
@@ -0,0 +1,1134 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JoltUtils.h"
+#include "rapidjson/error/en.h"
+#include "Exception.h"
+
+namespace org::apache::nifi::minifi::utils::jolt {
+
+
+static bool isSpecialChar(char ch) {
+  static constexpr std::array SPECIAL_CHARS{'.', '[', ']', '$', '&', '@', '#', 
'*'};
+  return std::find(SPECIAL_CHARS.begin(), SPECIAL_CHARS.end(), ch) != 
SPECIAL_CHARS.end();
+}
+
+bool Spec::Template::check(std::string_view str) {
+  enum class State {
+    Plain,
+    Escaped
+  } state = State::Plain;
+  for (char ch : str) {
+    switch (state) {
+      case State::Plain: {
+        if (ch == '&') {
+          return true;
+        } else if (ch == '\\') {
+          state = State::Escaped;
+        }
+        break;
+      }
+      case State::Escaped: {
+        state = State::Plain;
+        break;
+      }
+    }
+  }
+  return false;
+}
+
+nonstd::expected<std::pair<Spec::Template, Spec::It>, std::string> 
Spec::Template::parse(It begin, It end) {
+  enum class State {
+    Plain,
+    Escaped,
+    Template,  // &
+    SimpleIndex,  // &1
+    CanonicalTemplate,  // &(
+    ParentIndex,  // &(1
+    NextIndex,  // &(1,
+    MatchIndex  // &(1,0
+  };
+
+  std::vector<std::string> fragments;
+  std::vector<std::pair<size_t, size_t>> references;
+  fragments.push_back({});
+  State state = State::Plain;
+  std::string target;
+  // go beyond the last char on purpose
+  auto ch_it = begin;
+  while (ch_it <= end) {
+    std::optional<char> ch;
+    if (ch_it < end) {
+      ch = *ch_it;
+    }
+    bool force_terminate = false;
+    switch (state) {
+      case State::Plain: {
+        if (ch == '\\') {
+          state = State::Escaped;
+        } else if (ch == '&') {
+          references.push_back({});
+          fragments.push_back({});
+          state = State::Template;
+        } else if (ch == ')' || ch == ']' || ch == '.' || ch == '[') {
+          force_terminate = true;
+        } else if (ch) {
+          fragments.back() += ch.value();
+        }
+        break;
+      }
+      case State::Escaped: {
+        if (!ch) {
+          return nonstd::make_unexpected("Unterminated escape sequence");
+        }
+        if (ch != '\\' && !isSpecialChar(ch.value())) {
+          return nonstd::make_unexpected(fmt::format("Unknown escape sequence 
in template '\\{}'", ch.value()));
+        }
+        fragments.back() += ch.value();
+        state = State::Plain;
+        break;
+      }
+      case State::Template: {
+        if (ch == '(') {
+          state = State::CanonicalTemplate;
+        } else if (ch && std::isdigit(static_cast<unsigned char>(ch.value()))) 
{
+          target.clear();
+          target += ch.value();
+          state = State::SimpleIndex;
+        } else {
+          state = State::Plain;
+          // reprocess this char in a different state
+          --ch_it;

Review Comment:
   added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to