phrocker commented on a change in pull request #585: MINIFICPP-910 - Extend 
StringUtils with string join capability
URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291440400
 
 

 ##########
 File path: libminifi/src/utils/StringUtils.cpp
 ##########
 @@ -0,0 +1,162 @@
+/**
+ * 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 "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace StringUtils {
+
+  std::string& replaceAll(std::string& source_string, const std::string 
&from_string, const std::string &to_string) {  // NOLINT
+    std::size_t loc = 0;
+    std::size_t lastFound;
+    while ((lastFound = source_string.find(from_string, loc)) != 
std::string::npos) {
+      source_string.replace(lastFound, from_string.size(), to_string);
+      loc = lastFound + to_string.size();
+    }
+    return source_string;
+  }
+
+  std::string replaceEnvironmentVariables(const std::string& original_string) {
+    int32_t beg_seq = 0;
+    int32_t end_seq = 0;
+    std::string source_string = original_string;
+    do {
+      beg_seq = source_string.find("${", beg_seq);
+      if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') {
+        beg_seq += 2;
+        continue;
+      }
+      if (beg_seq < 0)
+        break;
+      end_seq = source_string.find("}", beg_seq + 2);
+      if (end_seq < 0)
+        break;
+      if (end_seq - (beg_seq + 2) < 0) {
+        beg_seq += 2;
+        continue;
+      }
+      const std::string env_field = source_string.substr(beg_seq + 2, end_seq 
- (beg_seq + 2));
+      const std::string env_field_wrapped = source_string.substr(beg_seq, 
end_seq + 1);
+      if (env_field.empty()) {
+        continue;
+      }
+      const auto strVal = std::getenv(env_field.c_str());
+      std::string env_value;
+      if (strVal != nullptr)
+        env_value = strVal;
+      source_string = replaceAll(source_string, env_field_wrapped, env_value);
+      beg_seq = 0;  // restart
+    } while (beg_seq >= 0);
+
+    source_string = replaceAll(source_string, "\\$", "$");
+
+    return source_string;
+  }
+
+  std::string replaceMap(std::string source_string, const 
std::map<std::string, std::string> &replace_map) {
+    auto result_string = source_string;
+
+    std::vector<std::pair<size_t, std::pair<size_t, std::string>>> 
replacements;
+    for (const auto &replace_pair : replace_map) {
+      size_t replace_pos = 0;
+      while ((replace_pos = source_string.find(replace_pair.first, 
replace_pos)) != std::string::npos) {
+        replacements.emplace_back(std::make_pair(replace_pos, 
std::make_pair(replace_pair.first.length(), replace_pair.second)));
+        replace_pos += replace_pair.first.length();
+      }
+    }
+
+    std::sort(replacements.begin(), replacements.end(), [](const 
std::pair<size_t, std::pair<size_t, std::string>> a,
+                                                           const 
std::pair<size_t, std::pair<size_t, std::string>> &b) {
+      return a.first > b.first;
+    });
+
+    for (const auto &replacement : replacements) {
+      result_string = source_string.replace(replacement.first, 
replacement.second.first, replacement.second.second);
+    }
+
+    return result_string;
+  }
+
+  bool StringToFloat(std::string input, float &output, FailurePolicy cp) {  // 
NOLINT
 
 Review comment:
   As a project we historically do not use NOLINT unless in test code. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to