Browse Source

Make `string::f()` automatically convert all arguments (including format string) from `std::string` to `const char*` as needed.

tags/v2.6.1
Andrew Belt 5 months ago
parent
commit
164f396ce7
1 changed files with 13 additions and 2 deletions
  1. +13
    -2
      include/string.hpp

+ 13
- 2
include/string.hpp View File

@@ -10,12 +10,23 @@ namespace rack {
namespace string {


/** Converts a `printf()` format string and optional arguments into a std::string.
Remember that "%s" must reference a `char *`, so use `.c_str()` for `std::string`s, otherwise you will get binary garbage.
/** Converts a printf format string and optional arguments into a std::string.
The wrapper template function below automatically converts all arguments (including format string) from `std::string` to `const char*` as needed.
*/
__attribute__((format(printf, 1, 2)))
std::string f(const char* format, ...);
std::string fV(const char* format, va_list args);
// Converts std::string arguments of f() to `const char*`
template<typename T>
T convertFArg(const T& t) {return t;}
inline const char* convertFArg(const std::string& s) {return s.c_str();}
template<typename... Args>
std::string f(Args... args) {
// Allows accessing the original f() above
typedef std::string (*FType)(const char* format, ...);
return FType(f)(convertFArg(args)...);
}

/** Replaces all characters to lowercase letters */
std::string lowercase(const std::string& s);
/** Replaces all characters to uppercase letters */


Loading…
Cancel
Save