#ifndef UTILS_H #define UTILS_H #include "pch.h" #include "ts/decl.h" template struct Queue { std::queue q; std::mutex m; void push(T val) { std::lock_guard lock(m); q.push(val); } std::optional front() { if (q.empty()) return std::nullopt; return q.front(); } bool pop(T &val) { std::lock_guard lock(m); if (q.empty()) return false; val = q.front(); q.pop(); return true; } void pop() { std::lock_guard lock(m); q.pop(); } bool empty() { std::lock_guard lock(m); return q.empty(); } }; struct Coord { uint32_t row; uint32_t col; bool operator<(const Coord &other) const { return row < other.row || (row == other.row && col < other.col); } bool operator<=(const Coord &other) const { return *this < other || *this == other; } bool operator==(const Coord &other) const { return row == other.row && col == other.col; } bool operator!=(const Coord &other) const { return !(*this == other); } bool operator>(const Coord &other) const { return other < *this; } bool operator>=(const Coord &other) const { return !(*this < other); } }; struct Match { size_t start; size_t end; std::string text; }; #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) std::string clean_text(const std::string &input); std::string percent_encode(const std::string &s); std::string percent_decode(const std::string &s); uint32_t count_clusters(const char *line, size_t len, size_t from, size_t to); std::string trim(const std::string &s); int display_width(const char *str, size_t len); uint32_t get_visual_col_from_bytes(const char *line, uint32_t len, uint32_t byte_limit); uint32_t get_bytes_from_visual_col(const char *line, uint32_t len, uint32_t target_visual_col); int utf8_byte_offset_to_utf16(const char *s, size_t byte_pos); void log(const char *fmt, ...); std::string path_abs(const std::string &path_str); std::string path_to_file_uri(const std::string &path_str); std::string get_exe_dir(); char *load_file(const char *path, uint32_t *out_len); char *detect_file_type(const char *filename); Language language_for_file(const char *filename); void copy_to_clipboard(const char *text, size_t len); char *get_from_clipboard(uint32_t *out_len); template inline T *safe_get(std::map &m, uint16_t key) { auto it = m.find(key); if (it == m.end()) return nullptr; return &it->second; } template auto throttle(std::chrono::milliseconds min_duration, Func &&func, Args &&...args) { auto start = std::chrono::steady_clock::now(); if constexpr (std::is_void_v>) { std::invoke(std::forward(func), std::forward(args)...); } else { auto result = std::invoke(std::forward(func), std::forward(args)...); auto elapsed = std::chrono::steady_clock::now() - start; if (elapsed < min_duration) std::this_thread::sleep_for(min_duration - elapsed); return result; } auto elapsed = std::chrono::steady_clock::now() - start; if (elapsed < min_duration) std::this_thread::sleep_for(min_duration - elapsed); } #endif