// Copyright 2024 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine License // Agreement. #ifndef WVCDM_UTIL_STRING_UTILS_H_ #define WVCDM_UTIL_STRING_UTILS_H_ #include #include // Small set of simple string utilities. // // It is inspired by Python strings and Abseil. The CDM tends to not // be too fancy with its use of templated containers; none of the // Abseil container magic is not provided, nor are the fancy templated // configurations. namespace wvutil { // Splits a string into several substrings based on the provided // delimiter. // // Special cases: // - An empty delimiter will split the string into each character. std::vector StringSplit(const std::string& s, char delim); std::vector StringSplit(const std::string& s, const std::string& delim); std::vector StringSplit(const std::string& s, const char* delim); // Joins a list of strings into a single string, using the specified // "glue" character(s) between tokens. Note: |glue| can be empty. std::string StringJoin(const std::vector& tokens, char glue); std::string StringJoin(const std::vector& tokens, const std::string& glue = ""); std::string StringJoin(const std::vector& tokens, const char* glue); // Counts the number of instances of |needle| sequences found in the // |haystack|. // // Special cases: // - An empty needle will return the length of |haystack| plus 1, // including for an empty string. // Note: This is a convention used by many string utility // libraries. size_t StringCount(const std::string& haystack, char needle); size_t StringCount(const std::string& haystack, const std::string& needle); size_t StringCount(const std::string& haystack, const char* needle); // Checks if any instances of |needle| sequences found in the |haystack|. // // Special cases: // - An empty |needle| is always present, even if |haystack| is empty. // Note: This is a convention used by many string utility // libraries. bool StringContains(const std::string& haystack, char needle); bool StringContains(const std::string& haystack, const std::string& needle); bool StringContains(const std::string& haystack, const char* needle); // Checks if the |needle| sequences found at the beginning of |haystack|. // // Special cases: // - An empty |needle| is always present, even if |haystack| is empty. // Note: This is a convention used by many string utility // libraries. bool StringStartsWith(const std::string& haystack, char needle); bool StringStartsWith(const std::string& haystack, const std::string& needle); bool StringStartsWith(const std::string& haystack, const char* needle); // Checks if the |needle| sequences found at the end of |haystack|. // // Special cases: // - An empty |needle| is always present, even if |haystack| is empty. // Note: This is a convention used by many string utility // libraries. bool StringEndsWith(const std::string& haystack, char needle); bool StringEndsWith(const std::string& haystack, const std::string& needle); bool StringEndsWith(const std::string& haystack, const char* needle); // Removes any leading or trailing white space from the provided string. std::string StringTrim(const std::string& s); // Checks if the vector of strings contains any instance of the // specified |needle|. // // Note: Unlike the other utilities, an empty |needle| is treated // as a value. bool StringVecContains(const std::vector& haystack, const std::string& needle); } // namespace wvutil #endif // WVCDM_UTIL_STRING_UTILS_H_