90 lines
3.7 KiB
C++
90 lines
3.7 KiB
C++
// 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 <string>
|
|
#include <vector>
|
|
|
|
// 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<std::string> StringSplit(const std::string& s, char delim);
|
|
std::vector<std::string> StringSplit(const std::string& s,
|
|
const std::string& delim);
|
|
std::vector<std::string> 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<std::string>& tokens, char glue);
|
|
std::string StringJoin(const std::vector<std::string>& tokens,
|
|
const std::string& glue = "");
|
|
std::string StringJoin(const std::vector<std::string>& 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<std::string>& haystack,
|
|
const std::string& needle);
|
|
} // namespace wvutil
|
|
#endif // WVCDM_UTIL_STRING_UTILS_H_
|