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_r296887540
########## 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 { + +bool StringUtils::StringToBool(std::string input, bool &output) { + std::transform(input.begin(), input.end(), input.begin(), ::tolower); + std::istringstream(input) >> std::boolalpha >> output; + return output; +} + +std::string StringUtils::trim(std::string s) { + return trimRight(trimLeft(s)); +} + +std::vector<std::string> StringUtils::split(const std::string &str, const std::string &delimiter) { + std::vector<std::string> result; + auto curr = str.begin(); + auto end = str.end(); + auto is_func = [delimiter](int s) { + return delimiter.at(0) == s; + }; + while (curr != end) { + curr = std::find_if_not(curr, end, is_func); + if (curr == end) { + break; + } + auto next = std::find_if(curr, end, is_func); + result.push_back(std::string(curr, next)); + curr = next; + } + + return result; +} + +bool StringUtils::StringToFloat(std::string input, float &output, FailurePolicy cp /*= RETURN*/) { + try { + output = std::stof(input); + } catch (const std::invalid_argument &ie) { + switch (cp) { + case RETURN: + case NOTHING: + return false; + case EXIT: + exit(1); + case EXCEPT: + throw ie; + } + } catch (const std::out_of_range &ofr) { + switch (cp) { + case RETURN: + case NOTHING: + return false; + case EXIT: + exit(1); + case EXCEPT: + throw ofr; + } + } + + return true; +} + +std::string StringUtils::replaceEnvironmentVariables(std::string& original_string) { Review comment: I've always wondered why this is in StringUtils. I probably put it here...but I'm not sure it fits. Making these changes helps highlight this, Thanks! ---------------------------------------------------------------- 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
