phrocker commented on a change in pull request #596: MINIFICPP-925 - Fix 
TailFile hang on long lines
URL: https://github.com/apache/nifi-minifi-cpp/pull/596#discussion_r296836531
 
 

 ##########
 File path: libminifi/include/utils/StringUtils.h
 ##########
 @@ -234,30 +138,139 @@ class StringUtils {
     return newString;
   }
 
-  static 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();
+  /**
+   * Hexdecodes the hexencoded string in data, ignoring every character that 
is not [0-9a-fA-F]
+   * @param data the output buffer where the hexdecoded bytes will be written. 
Must be at least length / 2 bytes long.
+   * @param data_length pointer to the length of data the data buffer. It will 
be filled with the length of the decoded bytes.
+   * @param hex the hexencoded string
+   * @param hex_length the length of hex
+   * @return true on success
+   */
+  inline static bool from_hex(uint8_t* data, size_t* data_length, const char* 
hex, size_t hex_length) {
+    if (*data_length < hex_length / 2) {
+      return false;
+    }
+    uint8_t n1;
+    bool found_first_nibble = false;
+    *data_length = 0;
+    for (size_t i = 0; i < hex_length; i++) {
+      const uint8_t byte = static_cast<uint8_t>(hex[i]);
+      if (byte > 127) {
+        continue;
+      }
+      uint8_t n = hex_lut[byte];
+      if (n != SKIP) {
+        if (found_first_nibble) {
+          data[(*data_length)++] = n1 << 4 | n;
+          found_first_nibble = false;
+        } else {
+          n1 = n;
+          found_first_nibble = true;
+        }
       }
     }
+    if (found_first_nibble) {
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * Hexdecodes a string
+   * @param hex the hexencoded string
+   * @param hex_length the length of hex
+   * @return the vector containing the hexdecoded bytes
+   */
+  inline static std::vector<uint8_t> from_hex(const char* hex, size_t 
hex_length) {
+    std::vector<uint8_t> decoded(hex_length / 2);
+    size_t data_length = decoded.size();
+    if (!from_hex(decoded.data(), &data_length, hex, hex_length)) {
+      throw std::runtime_error("Hexencoded string is malformatted");
+    }
+    decoded.resize(data_length);
+    return decoded;
+  }
+
+  /**
+   * Hexdecodes a string
+   * @param hex the hexencoded string
+   * @return the hexdecoded string
+   */
+  inline static std::string from_hex(const std::string& hex) {
+    auto data = from_hex(hex.data(), hex.length());
+    return std::string(reinterpret_cast<char*>(data.data()), data.size());
+  }
 
-    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;
-    });
+  /**
+   * Hexencodes bytes and writes the result to hex
+   * @param hex the output buffer where the hexencoded string will be written. 
Must be at least length * 2 bytes long.
+   * @param data the bytes to be hexencoded
+   * @param length the length of data. Must not be larger than 
std::numeric_limits<size_t>::max()
+   * @param uppercase whether the hexencoded string should be upper case
+   */
+  inline static void to_hex(char* hex, const uint8_t* data, size_t length, 
bool uppercase) {
 
 Review comment:
   When I evaluate necessity it's mostly to know if this is a piece of code 
that's absolutely needed for 0.7.0 or if it's something that can wait...

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to