looper/util.hpp

59 lines
No EOL
1.7 KiB
C++

#pragma once
#include <string>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <cmath>
#include "log.hpp"
std::string PadZeros(std::string input, size_t required_length);
uint8_t TimeToComponentCount(double time_code);
std::string TimeToString(double time_code, uint8_t min_components = 1);
std::string get_prefs_path();
inline std::string to_string_with_decimals(double value, unsigned decimals) {
std::string num_text;
if (value == 0) {
num_text = "0";
} else {
size_t buflen = snprintf(NULL, 0, "%f", value);
buflen++;
char *buf = (char*)malloc(buflen);
memset(buf, 0, buflen);
snprintf(buf, buflen - 1, "%f", value);
num_text = std::string(buf);
free(buf);
}
int found = num_text.find(".");
if (found == -1) {
found = num_text.size();
num_text.append(".0");
}
if (num_text[0] == '.') {
num_text = "0" + num_text;
found++;
}
if (decimals == 0) {
num_text = num_text.substr(0, found);
return num_text;
}
int chars_after_decimal = num_text.size() - found - 1;
if (chars_after_decimal > decimals) {
num_text = num_text.substr(0, found + decimals + 1);
} else {
for (int i = chars_after_decimal; i < decimals; i++) {
num_text.push_back('0');
}
}
return num_text;
}
inline size_t combine_hashes(std::initializer_list<size_t> hashes) {
std::string values = "^";
values += hashes.size();
values += "@";
for (auto value : values) {
values += value;
values += ";";
}
return std::hash<std::string>()(values);
}
#define MIN(x, y) ((x < y) ? x : y)
#define MAX(x, y) ((x > y) ? x : y)