Loading [MathJax]/extensions/tex2jax.js
VCV Rack API v2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
string.hpp
Go to the documentation of this file.
1#pragma once
2#include <stdarg.h>
3#include <vector>
4
5#include <common.hpp>
6
7
8namespace rack {
10namespace string {
11
12
16__attribute__((format(printf, 1, 2)))
17std::string f(const char* format, ...);
18std::string fV(const char* format, va_list args);
19
20// Converts std::string arguments of f() to `const char*`
21template<typename T>
22T convertFArg(const T& t) {return t;}
23inline const char* convertFArg(const std::string& s) {return s.c_str();}
24template<typename... Args>
25std::string f(Args... args) {
26 // Allows accessing the original f() above
27 typedef std::string (*FType)(const char* format, ...);
28 return FType(f)(convertFArg(args)...);
29}
30
32std::string lowercase(const std::string& s);
34std::string uppercase(const std::string& s);
36std::string trim(const std::string& s);
38std::string truncate(const std::string& s, size_t maxCodepoints);
40std::string truncatePrefix(const std::string& s, size_t maxCodepoints);
42std::string ellipsize(const std::string& s, size_t maxCodepoints);
44std::string ellipsizePrefix(const std::string& s, size_t maxCodepoints);
46bool startsWith(const std::string& str, const std::string& prefix);
48bool endsWith(const std::string& str, const std::string& suffix);
49
53std::string toBase64(const uint8_t* data, size_t dataLen);
54std::string toBase64(const std::vector<uint8_t>& data);
58std::vector<uint8_t> fromBase64(const std::string& str);
59
62 bool operator()(const std::string& a, const std::string& b) const;
63};
64
67template <typename TContainer>
68std::string join(const TContainer& container, std::string seperator = "") {
69 std::string s;
70 bool first = true;
71 for (const auto& c : container) {
72 if (!first)
73 s += seperator;
74 first = false;
75 s += c;
76 }
77 return s;
78}
79
90std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
91
93std::string formatTime(const char* format, double timestamp);
94std::string formatTimeISO(double timestamp);
95
96// Unicode functions
100std::string UTF32toUTF8(const std::u32string& s32);
104std::u32string UTF8toUTF32(const std::string& s8);
109size_t UTF8NextCodepoint(const std::string& s8, size_t pos);
114size_t UTF8PrevCodepoint(const std::string& s8, size_t pos);
118size_t UTF8Length(const std::string& s8);
123size_t UTF8CodepointIndex(const std::string& s8, size_t pos);
128size_t UTF8CodepointPos(const std::string& s8, size_t index);
129
130#if defined ARCH_WIN
137std::string UTF16toUTF8(const std::wstring& w);
138std::wstring UTF8toUTF16(const std::string& s);
139#endif
140
141
161struct Version {
162 std::vector<std::string> parts;
163
165 Version(const std::string& s);
166 Version(const char* s) : Version(std::string(s)) {}
167 operator std::string() const;
169 bool operator<(const Version& other);
170
171 std::string getMajor() const {
172 return get(parts, 0, "");
173 }
174 std::string getMinor() const {
175 return get(parts, 1, "");
176 }
177 std::string getRevision() const {
178 return get(parts, 2, "");
179 }
180};
181
182
184std::string translate(const std::string& id);
186std::string translate(const std::string& id, const std::string& language);
188std::vector<std::string> getLanguages();
189void init();
190
191
192} // namespace string
193} // namespace rack
__attribute__((format(printf, 5, 6))) void log(Level level
Do not use this function directly.
const char int const char const char * format
Definition logger.hpp:41
Supplemental std::string functions.
Definition string.hpp:10
std::string formatTimeISO(double timestamp)
std::string truncatePrefix(const std::string &s, size_t maxCodepoints)
Truncates the beginning of a string to not exceed a number of UTF-8 codepoints.
std::string f(Args... args)
Definition string.hpp:25
std::string ellipsize(const std::string &s, size_t maxCodepoints)
Truncates and adds "…" to the end of a string, to not exceed a number of UTF-8 codepoints.
std::string toBase64(const uint8_t *data, size_t dataLen)
Converts a byte array to a Base64-encoded string.
std::string UTF32toUTF8(const std::u32string &s32)
Converts a UTF-32 string to a UTF-8 string.
std::vector< uint8_t > fromBase64(const std::string &str)
Converts a Base64-encoded string to a byte array.
std::string ellipsizePrefix(const std::string &s, size_t maxCodepoints)
Truncates and adds "…" to the beginning of a string, to not exceed a number of UTF-8 codepoints.
std::u32string UTF8toUTF32(const std::string &s8)
Converts a UTF-8 string to a UTF-32 string.
__attribute__((format(printf, 1, 2))) std std::string fV(const char *format, va_list args)
Converts a printf format string and optional arguments into a std::string.
std::string translate(const std::string &id)
Returns translation string of the current language setting from translations/<language>....
std::vector< std::string > getLanguages()
Returns ISO 639-1 language codes of loaded translations, sorted by name of language.
bool endsWith(const std::string &str, const std::string &suffix)
Returns whether a string ends with the given substring.
size_t UTF8CodepointPos(const std::string &s8, size_t index)
Returns a codepoint's byte position in a valid UTF-8 string.
T convertFArg(const T &t)
Definition string.hpp:22
std::string truncate(const std::string &s, size_t maxCodepoints)
Truncates a string to not exceed a number of UTF-8 codepoints.
std::vector< std::string > split(const std::string &s, const std::string &seperator, size_t maxTokens=0)
Splits a string into a vector of tokens.
size_t UTF8NextCodepoint(const std::string &s8, size_t pos)
Finds the byte position of the next codepoint in a valid UTF-8 string.
size_t UTF8Length(const std::string &s8)
Returns the number of codepoints in a valid UTF-8 string.
bool startsWith(const std::string &str, const std::string &prefix)
Returns whether a string starts with the given substring.
std::string join(const TContainer &container, std::string seperator="")
Joins an container (vector, list, etc) of std::strings with an optional separator string.
Definition string.hpp:68
std::string uppercase(const std::string &s)
Replaces all characters to uppercase letters.
size_t UTF8PrevCodepoint(const std::string &s8, size_t pos)
Finds the byte position of the previous codepoint in a valid UTF-8 string.
size_t UTF8CodepointIndex(const std::string &s8, size_t pos)
Returns a codepoint's index in a valid UTF-8 string.
std::string trim(const std::string &s)
Removes whitespace from beginning and end of string.
std::string lowercase(const std::string &s)
Replaces all characters to lowercase letters.
std::string formatTime(const char *format, double timestamp)
Formats a UNIX timestamp with a strftime() string.
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
const C::mapped_type & get(const C &c, const typename C::key_type &key, const typename C::mapped_type &def=typename C::mapped_type())
Given a std::map c, returns the value of the given key, or returns def if the key doesn't exist.
Definition common.hpp:240
Definition string.hpp:60
bool operator()(const std::string &a, const std::string &b) const
Returns whether a < b using case-insensitive lexical comparison.
std::string getMinor() const
Definition string.hpp:174
std::string getRevision() const
Definition string.hpp:177
Version()
Definition string.hpp:164
operator std::string() const
std::string getMajor() const
Definition string.hpp:171
bool operator<(const Version &other)
Returns whether this version is earlier than other.
Version(const std::string &s)
std::vector< std::string > parts
Definition string.hpp:162
Version(const char *s)
Definition string.hpp:166