diff options
Diffstat (limited to 'util.h')
-rw-r--r-- | util.h | 186 |
1 files changed, 183 insertions, 3 deletions
@@ -56,11 +56,20 @@ typedef struct { v2 pos, size; } Rect; +// UTF-32 string +typedef struct { + char32_t *str; + size_t len; +} String32; + // ctype functions for 32-bit chars. -bool is_word(char32_t c); -bool is_digit(char32_t c); -bool is_space(char32_t c); +bool is32_word(char32_t c); +bool is32_space(char32_t c); +bool is32_alpha(char32_t c); +bool is32_alnum(char32_t c); +bool is32_digit(char32_t c); +bool is32_graph(char32_t c); bool is_a_tty(FILE *out); // terminal colors. if `out` is a TTY, these will return the appropriate escape sequences. // if `out` is not a TTY, these will return "". @@ -253,5 +262,176 @@ int timespec_cmp(struct timespec a, struct timespec b); bool timespec_eq(struct timespec a, struct timespec b); struct timespec timespec_max(struct timespec a, struct timespec b); double timespec_to_seconds(struct timespec ts); +bool is32_word(char32_t c); +bool is32_space(char32_t c); +bool is32_alpha(char32_t c); +bool is32_alnum(char32_t c); +bool is32_digit(char32_t c); +bool is32_graph(char32_t c); +bool is_a_tty(FILE *out); +const char *term_italics(FILE *out); +const char *term_bold(FILE *out); +const char *term_yellow(FILE *out); +const char *term_clear(FILE *out); +u8 util_popcount(u64 x); +u8 util_count_leading_zeroes32(u32 x); +bool util_is_power_of_2(u64 x); +char32_t *util_mem32chr(char32_t *s, char32_t c, size_t n); +const char32_t *util_mem32chr_const(const char32_t *s, char32_t c, size_t n); +bool str_has_prefix(const char *str, const char *prefix); +bool str_has_path_prefix(const char *path, const char *prefix); +bool streq(const char *a, const char *b); +size_t strn_len(const char *src, size_t n); +char *strn_dup(const char *src, size_t n); +char *str_dup(const char *src); +void strn_cat(char *dst, size_t dst_sz, const char *src, size_t src_len); +void str_cat(char *dst, size_t dst_sz, const char *src); +void strn_cpy(char *dst, size_t dst_sz, const char *src, size_t src_len); +void str_cpy(char *dst, size_t dst_sz, const char *src); +char *a_sprintf(const char *fmt, ...); +char *strstr_case_insensitive(const char *haystack, const char *needle); +void print_bytes(const u8 *bytes, size_t n); +int strcmp_case_insensitive(const char *a, const char *b); +int str_qsort_case_insensitive_cmp(const void *av, const void *bv); +int qsort_with_context_cmp(const void *a, const void *b, void *context); +const char *path_filename(const char *path); +bool path_is_absolute(const char *path); +void path_full(const char *dir, const char *relpath, char *abspath, size_t abspath_size); +bool paths_eq(const char *path1, const char *path2); +void change_directory(const char *path); +bool copy_file(const char *src, const char *dst); +float degrees(float r); +float radians(float r); +float lerpf(float x, float a, float b); +float normf(float x, float a, float b); +float clampf(float x, float a, float b); +int clampi(int x, int a, int b); +i16 clamp_i16(i16 x, i16 a, i16 b); +u16 clamp_u16(u16 x, u16 a, u16 b); +i32 clamp_i32(i32 x, i32 a, i32 b); +u32 clamp_u32(u32 x, u32 a, u32 b); +u8 ndigits_u64(u64 x); +float remapf(float x, float from_a, float from_b, float to_a, float to_b); +float minf(float a, float b); +float maxf(float a, float b); +double maxd(double a, double b); +double mind(double a, double b); +u32 min_u32(u32 a, u32 b); +u32 max_u32(u32 a, u32 b); +void sort2_u32(u32 *a, u32 *b); +i32 min_i32(i32 a, i32 b); +i32 max_i32(i32 a, i32 b); +u64 min_u64(u64 a, u64 b); +u64 max_u64(u64 a, u64 b); +i64 min_i64(i64 a, i64 b); +i64 max_i64(i64 a, i64 b); +i64 mod_i64(i64 a, i64 b); +i32 mod_i32(i32 a, i32 b); +i64 abs_i64(i64 x); +i64 sgn_i64(i64 x); +float sgnf(float x); +float smoothstepf(float x); +float randf(void); +float rand_gauss(void); +u32 rand_u32(void); +float rand_uniform(float from, float to); +float sigmoidf(float x); +i32 ceildivi32(i32 x, i32 y); +v2 V2(float x, float y); +v2 v2_add(v2 a, v2 b); +v2 v2_add_const(v2 a, float c); +v2 v2_sub(v2 a, v2 b); +v2 v2_scale(v2 v, float s); +v2 v2_mul(v2 a, v2 b); +v2 v2_clamp(v2 x, v2 a, v2 b); +float v2_dot(v2 a, v2 b); +float v2_len(v2 v); +v2 v2_lerp(float x, v2 a, v2 b); +v2 v2_rotate(v2 v, float theta); +v2 v2_normalize(v2 v); +float v2_dist(v2 a, v2 b); +float v2_dist_squared(v2 a, v2 b); +void v2_print(v2 v); +v2 v2_rand_unit(void); +v2 v2_polar(float r, float theta); +v3 V3(float x, float y, float z); +v3 v3_from_v2(v2 v); +v3 v3_add(v3 a, v3 b); +v3 v3_sub(v3 a, v3 b); +v3 v3_scale(v3 v, float s); +v3 v3_lerp(float x, v3 a, v3 b); +float v3_dot(v3 u, v3 v); +v3 v3_cross(v3 u, v3 v); +float v3_len(v3 v); +float v3_dist(v3 a, v3 b); +float v3_dist_squared(v3 a, v3 b); +v3 v3_normalize(v3 v); +v2 v3_xy(v3 v); +v3 v3_on_sphere(float yaw, float pitch); +void v3_print(v3 v); +v3 v3_rand(void); +v3 v3_rand_unit(void); +v4 V4(float x, float y, float z, float w); +v4 v4_add(v4 a, v4 b); +v4 v4_sub(v4 a, v4 b); +v4 v4_scale(v4 v, float s); +v4 v4_scale_xyz(v4 v, float s); +v4 v4_lerp(float x, v4 a, v4 b); +float v4_dot(v4 u, v4 v); +v4 v4_mul(v4 u, v4 v); +float v4_len(v4 v); +v4 v4_normalize(v4 v); +v3 v4_xyz(v4 v); +v4 v4_rand(void); +void v4_print(v4 v); +v2d V2D(double x, double y); +void m4_print(m4 m); +m4 m4_yaw(float yaw); +m4 m4_pitch(float pitch); +m4 m4_translate(v3 t); +v3 m4_mul_v3(m4 m, v3 v); +m4 m4_perspective(float fov, float aspect, float z_near, float z_far); +m4 m4_ortho(float left, float right, float bottom, float top, float near_, float far_); +m4 m4_mul(m4 a, m4 b); +m4 m4_inv(m4 mat); +v2i V2I(int x, int y); +void rgba_u32_to_floats(u32 rgba, float floats[4]); +v4 rgba_u32_to_v4(u32 rgba); +u32 rgba_v4_to_u32(v4 color); +float rgba_brightness(u32 color); +bool rect_contains_point_v2(v2 pos, v2 size, v2 point); +bool centered_rect_contains_point(v2 center, v2 size, v2 point); +Rect rect(v2 pos, v2 size); +Rect rect_endpoints(v2 e1, v2 e2); +Rect rect4(float x1, float y1, float x2, float y2); +Rect rect_xywh(float x, float y, float w, float h); +Rect rect_centered(v2 center, v2 size); +v2 rect_center(Rect r); +bool rect_contains_point(Rect r, v2 point); +Rect rect_translate(Rect r, v2 by); +void rect_coords(Rect r, float *x1, float *y1, float *x2, float *y2); +void rect_print(Rect r); +float rects_intersect(Rect r1, Rect r2); +bool rect_clip_to_rect(Rect *clipped, Rect clipper); +Rect rect_shrink(Rect r, float amount); +Rect rect_grow(Rect r, float amount); +v4 color_rgba_to_hsva(v4 rgba); +v4 color_hsva_to_rgba(v4 hsva); +u32 color_interpolate(float x, u32 color1, u32 color2); +int timespec_cmp(struct timespec a, struct timespec b); +bool timespec_eq(struct timespec a, struct timespec b); +struct timespec timespec_max(struct timespec a, struct timespec b); +double timespec_to_seconds(struct timespec ts); +String32 str32(char32_t *str, size_t len); +String32 str32_substr(String32 s, size_t from, size_t len); +void str32_free(String32 *s); +String32 str32_from_utf8(char const *utf8); +char *str32_to_utf8_cstr(String32 s); +int str32_cmp_ascii(String32 s, char const *ascii); +bool str32_has_ascii_prefix(String32 s, char const *ascii); +size_t str32chr(String32 s, char32_t c); +size_t str32_count_char(String32 s, char32_t c); +size_t str32_remove_all_instances_of_char(String32 *s, char32_t c); +size_t str32_ascii_spn(String32 s, char const *charset); #endif // UTIL_H_ |