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);
20std::string lowercase(const std::string& s);
22std::string uppercase(const std::string& s);
24std::string trim(const std::string& s);
26std::string ellipsize(const std::string& s, size_t len);
28std::string ellipsizePrefix(const std::string& s, size_t len);
30bool startsWith(const std::string& str, const std::string& prefix);
32bool endsWith(const std::string& str, const std::string& suffix);
33
37std::string toBase64(const uint8_t* data, size_t dataLen);
38std::string toBase64(const std::vector<uint8_t>& data);
42std::vector<uint8_t> fromBase64(const std::string& str);
43
46 bool operator()(const std::string& a, const std::string& b) const;
47};
48
51template <typename TContainer>
52std::string join(const TContainer& container, std::string seperator = "") {
53 std::string s;
54 bool first = true;
55 for (const auto& c : container) {
56 if (!first)
57 s += seperator;
58 first = false;
59 s += c;
60 }
61 return s;
62}
63
74std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
75
77std::string formatTime(const char* format, double timestamp);
78std::string formatTimeISO(double timestamp);
79
80
81#if defined ARCH_WIN
88std::string UTF16toUTF8(const std::wstring& w);
89std::wstring UTF8toUTF16(const std::string& s);
90#endif
91
92
112struct Version {
113 std::vector<std::string> parts;
114
116 Version(const std::string& s);
117 Version(const char* s) : Version(std::string(s)) {}
118 operator std::string() const;
120 bool operator<(const Version& other);
121
122 std::string getMajor() const {
123 return get(parts, 0, "");
124 }
125 std::string getMinor() const {
126 return get(parts, 1, "");
127 }
128 std::string getRevision() const {
129 return get(parts, 2, "");
130 }
131};
132
133
134} // namespace string
135} // 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
std::string formatTimeISO(double timestamp)
std::string ellipsizePrefix(const std::string &s, size_t len)
Truncates and adds "..." to the beginning of a string, not exceeding len characters.
std::string ellipsize(const std::string &s, size_t len)
Truncates and adds "..." to the end of a string, not exceeding len characters.
std::string toBase64(const uint8_t *data, size_t dataLen)
Converts a byte array to a Base64-encoded string.
std::vector< uint8_t > fromBase64(const std::string &str)
Converts a Base64-encoded string to a byte array.
__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.
bool endsWith(const std::string &str, const std::string &suffix)
Returns whether a string ends with the given substring.
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.
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:52
std::string uppercase(const std::string &s)
Replaces all characters to uppercase letters.
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:246
Definition string.hpp:44
bool operator()(const std::string &a, const std::string &b) const
Returns whether a < b using case-insensitive lexical comparison.
Structured version string, for comparison.
Definition string.hpp:112
std::string getMinor() const
Definition string.hpp:125
std::string getRevision() const
Definition string.hpp:128
Version()
Definition string.hpp:115
operator std::string() const
std::string getMajor() const
Definition string.hpp:122
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:113
Version(const char *s)
Definition string.hpp:117