#ifndef ANT_UTILS_H #define ANT_UTILS_H #include #include #include #include #include "hash.h" // IWYU pragma: keep typedef struct { char *ptr; char *heap; } cstr_buf_t; extern const char *const module_resolve_extensions[]; double half_to_double(uint16_t bits16); uint16_t double_to_half(double value); char hex_char(int v); void hex_encode(const uint8_t *src, size_t len, char *dst); void ts_strip_thread_cleanup(void); int is_typescript_file(const char *filename); char *resolve_js_file(const char *filename); char *resolve_typescript_source_fallback(const char *filename); typedef enum { ANT_TS_SOURCE_AUTO = 0, ANT_TS_SOURCE_MODULE, ANT_TS_SOURCE_SCRIPT, ANT_TS_SOURCE_COMMONJS, } ant_ts_source_mode_t; int ant_mkdir_p(const char *path); int ant_user_bin_path(char *out, size_t out_size); int ant_get_exe_path(char *out, size_t out_len, int argc, char **argv); int ant_xdg_cache_path(char *out, size_t out_size, const char *suffix); int ant_xdg_data_path(char *out, size_t out_size, const char *suffix); int ant_xdg_state_path(char *out, size_t out_size, const char *suffix); bool ant_env_bool(const char *value, bool default_value); int strip_typescript_inplace( char **buffer, size_t len, const char *filename, size_t *out_len, const char **error_detail ); int transform_typescript( char **buffer, size_t len, const char *filename, ant_ts_source_mode_t source_mode, size_t *out_len, const char **error_detail ); void *try_oom(size_t size); void cstr_free(cstr_buf_t *buf); char *cstr_init( cstr_buf_t *buf, char *stack, size_t stack_size, const char *src, size_t len ); #define CSTR_BUF(name, size) \ char name##_stack[size]; \ cstr_buf_t name = {0} #define CSTR_INIT(buf, src, len) \ cstr_init(&(buf), buf##_stack, sizeof(buf##_stack), (src), (len)) typedef struct { const char *ptr; size_t len; } repl_capture_t; bool repl_template( const char *repl, size_t repl_len, const char *matched, size_t matched_len, const char *str, size_t str_len, size_t position, const repl_capture_t *caps, int ncaptures, char **buf, size_t *buf_len, size_t *buf_cap ); static inline void *vec_grow(void *items, size_t *cap, size_t need, size_t elem, size_t first) { if (need <= *cap) return items; size_t next = *cap ? *cap : first; if (!next) return NULL; while (next < need) { if (next > SIZE_MAX / 2) return NULL; next *= 2; } if (next > SIZE_MAX / elem) return NULL; void *grown = realloc(items, next * elem); if (!grown) return NULL; *cap = next; return grown; } #endif