/* * Copyright 2013 Facebook, Inc. * * Licensed 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. */ #ifndef _STRINGUTIL_H_ #define _STRINGUTIL_H_ #include #include #include #include #include "types.h" //std::string based const std::string str_tolower(std::string s); const std::string str_toupper(std::string s); const std::string ucfirst(std::string s); std::vector split(std::string str_to_split, char delimiter); const std::string StringFormat(const char* format, ...); const std::string vStringFormat(const char* format, va_list args); std::string implode(std::string glue, std::vector src); template std::string implode(std::string glue, std::pair encapsulation, std::vector src) { if (src.empty()) { return {}; } std::ostringstream output; for (const T &src_iter : src) { output << encapsulation.first << src_iter << encapsulation.second << glue; } std::string final_output = output.str(); final_output.resize(output.str().size() - glue.size()); return final_output; } // this requires that #include be included in whatever file the invocation is made from template std::vector join_pair(std::string glue, std::pair first_encap, std::pair second_encap, std::vector> src) { if (src.empty()) { return {}; } std::vector output; for (const std::pair &src_iter : src) { output.push_back( // There are issues with including in a header file that result in compile // failure. I'm not sure if this applies only within the same project or across projects. // Since templates act similar to macros in regards to initialization, this call should be // safe so long as the '#include' rule above is observed. fmt::format( "{}{}{}{}{}{}{}", first_encap.first, src_iter.first, first_encap.second, glue, second_encap.first, src_iter.second, second_encap.second ) ); } return output; } std::vector SplitString(const std::string &s, char delim); std::string EscapeString(const char *src, size_t sz); std::string EscapeString(const std::string &s); bool StringIsNumber(const std::string &s); void ToLowerString(std::string &s); void ToUpperString(std::string &s); std::string JoinString(const std::vector& ar, const std::string &delim); void find_replace(std::string& string_subject, const std::string& search_string, const std::string& replace_string); //const char based bool atobool(const char* iBool); bool isAlphaNumeric(const char *text); bool strn0cpyt(char* dest, const char* source, uint32 size); char *CleanMobName(const char *in, char *out); char *RemoveApostrophes(const char *s); char* strn0cpy(char* dest, const char* source, uint32 size); const char *ConvertArray(int input, char *returnchar); const char *ConvertArrayF(float input, char *returnchar); const char *MakeLowerString(const char *source); int MakeAnyLenString(char** ret, const char* format, ...); uint32 AppendAnyLenString(char** ret, uint32* bufsize, uint32* strlen, const char* format, ...); uint32 hextoi(const char* num); uint64 hextoi64(const char* num); void MakeLowerString(const char *source, char *target); void RemoveApostrophes(std::string &s); #endif