VCV Rack API v2
Loading...
Searching...
No Matches
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
50struct Location {
52 size_t line;
54 size_t column;
55};
56
57Location positionToLocation(const std::string& s, size_t pos);
58size_t locationToPosition(const std::string& s, Location location);
59
63std::string toBase64(const uint8_t* data, size_t dataLen);
64std::string toBase64(const std::vector<uint8_t>& data);
68std::vector<uint8_t> fromBase64(const std::string& str);
69
70int strcasecmp(const char* s1, const char* s2);
71
74 bool operator()(const std::string& a, const std::string& b) const;
75};
76
79template <typename TContainer>
80std::string join(const TContainer& container, std::string seperator = "") {
81 std::string s;
82 bool first = true;
83 for (const auto& c : container) {
84 if (!first)
85 s += seperator;
86 first = false;
87 s += c;
88 }
89 return s;
90}
91
102std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
103
105std::string formatTime(const char* format, double timestamp);
106std::string formatTimeISO(double timestamp);
107
108// Unicode functions
112std::string UTF32toUTF8(const std::u32string& s32);
116std::u32string UTF8toUTF32(const std::string& s8);
121size_t UTF8NextCodepoint(const std::string& s8, size_t pos);
126size_t UTF8PrevCodepoint(const std::string& s8, size_t pos);
130size_t UTF8Length(const std::string& s8);
135size_t UTF8CodepointIndex(const std::string& s8, size_t pos);
140size_t UTF8CodepointPos(const std::string& s8, size_t index);
141
142#if defined ARCH_WIN
149std::string UTF16toUTF8(const std::wstring& w);
150std::wstring UTF8toUTF16(const std::string& s);
151#endif
152
153
173struct Version {
174 std::vector<std::string> parts;
175
177 Version(const std::string& s);
178 Version(const char* s) : Version(std::string(s)) {}
179 operator std::string() const;
181 bool operator<(const Version& other);
182
183 std::string getMajor() const {
184 return get(parts, 0, "");
185 }
186 std::string getMinor() const {
187 return get(parts, 1, "");
188 }
189 std::string getRevision() const {
190 return get(parts, 2, "");
191 }
192};
193
194
196std::string translate(const std::string& id);
198std::string translate(const std::string& id, const std::string& language);
200std::vector<std::string> getLanguages();
201void init();
202
203
204} // namespace string
205} // 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.
Location positionToLocation(const std::string &s, size_t pos)
Given a byte position of s, returns the 2D UTF-8 location of the cursor.
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.
int strcasecmp(const char *s1, const char *s2)
__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:80
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.
size_t locationToPosition(const std::string &s, Location location)
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:72
bool operator()(const std::string &a, const std::string &b) const
Returns whether a < b using case-insensitive lexical comparison.
Definition string.hpp:50
size_t line
Line number, 0-indexed.
Definition string.hpp:52
size_t column
UTF-8 codepoint index, 0-indexed.
Definition string.hpp:54
std::string getMinor() const
Definition string.hpp:186
std::string getRevision() const
Definition string.hpp:189
Version()
Definition string.hpp:176
operator std::string() const
std::string getMajor() const
Definition string.hpp:183
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:174
Version(const char *s)
Definition string.hpp:178