Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/extern/isocline/src/common.h
2727 views
1
/* ----------------------------------------------------------------------------
2
Copyright (c) 2021, Daan Leijen
3
This is free software; you can redistribute it and/or modify it
4
under the terms of the MIT License. A copy of the license can be
5
found in the "LICENSE" file at the root of this distribution.
6
-----------------------------------------------------------------------------*/
7
8
#pragma once
9
#ifndef IC_COMMON_H
10
#define IC_COMMON_H
11
12
//-------------------------------------------------------------
13
// Headers and defines
14
//-------------------------------------------------------------
15
16
#include <sys/types.h> // ssize_t
17
#include <limits.h>
18
#include <stddef.h>
19
#include <stdint.h>
20
#include <stdbool.h>
21
#include <assert.h>
22
#include "../include/isocline.h" // ic_malloc_fun_t, ic_color_t etc.
23
24
# ifdef __cplusplus
25
# define ic_extern_c extern "C"
26
# else
27
# define ic_extern_c
28
# endif
29
30
#if defined(IC_SEPARATE_OBJS)
31
# define ic_public ic_extern_c
32
# if defined(__GNUC__) // includes clang and icc
33
# define ic_private __attribute__((visibility("hidden")))
34
# else
35
# define ic_private
36
# endif
37
#else
38
# define ic_private static
39
# define ic_public ic_extern_c
40
#endif
41
42
#define ic_unused(x) (void)(x)
43
44
45
//-------------------------------------------------------------
46
// ssize_t
47
//-------------------------------------------------------------
48
49
#if defined(_MSC_VER)
50
typedef intptr_t ssize_t;
51
#endif
52
53
#define ssizeof(tp) (ssize_t)(sizeof(tp))
54
static inline size_t to_size_t(ssize_t sz) { return (sz >= 0 ? (size_t)sz : 0); }
55
static inline ssize_t to_ssize_t(size_t sz) { return (sz <= SIZE_MAX/2 ? (ssize_t)sz : 0); }
56
57
ic_private void ic_memmove(void* dest, const void* src, ssize_t n);
58
ic_private void ic_memcpy(void* dest, const void* src, ssize_t n);
59
ic_private void ic_memset(void* dest, uint8_t value, ssize_t n);
60
ic_private bool ic_memnmove(void* dest, ssize_t dest_size, const void* src, ssize_t n);
61
62
ic_private ssize_t ic_strlen(const char* s);
63
ic_private bool ic_strcpy(char* dest, ssize_t dest_size /* including 0 */, const char* src);
64
ic_private bool ic_strncpy(char* dest, ssize_t dest_size /* including 0 */, const char* src, ssize_t n);
65
66
ic_private bool ic_contains(const char* big, const char* s);
67
ic_private bool ic_icontains(const char* big, const char* s);
68
ic_private char ic_tolower(char c);
69
ic_private void ic_str_tolower(char* s);
70
ic_private int ic_stricmp(const char* s1, const char* s2);
71
ic_private int ic_strnicmp(const char* s1, const char* s2, ssize_t n);
72
73
74
75
//---------------------------------------------------------------------
76
// Unicode
77
//
78
// We use "qutf-8" (quite like utf-8) encoding and decoding.
79
// Internally we always use valid utf-8. If we encounter invalid
80
// utf-8 bytes (or bytes >= 0x80 from any other encoding) we encode
81
// these as special code points in the "raw plane" (0xEE000 - 0xEE0FF).
82
// When decoding we are then able to restore such raw bytes as-is.
83
// See <https://github.com/koka-lang/koka/blob/master/kklib/include/kklib/string.h>
84
//---------------------------------------------------------------------
85
86
typedef uint32_t unicode_t;
87
88
ic_private void unicode_to_qutf8(unicode_t u, uint8_t buf[5]);
89
ic_private unicode_t unicode_from_qutf8(const uint8_t* s, ssize_t len, ssize_t* nread); // validating
90
91
ic_private unicode_t unicode_from_raw(uint8_t c);
92
ic_private bool unicode_is_raw(unicode_t u, uint8_t* c);
93
94
ic_private bool utf8_is_cont(uint8_t c);
95
96
97
//-------------------------------------------------------------
98
// Colors
99
//-------------------------------------------------------------
100
101
// A color is either RGB or an ANSI code.
102
// (RGB colors have bit 24 set to distinguish them from the ANSI color palette colors.)
103
// (Isocline will automatically convert from RGB on terminals that do not support full colors)
104
typedef uint32_t ic_color_t;
105
106
// Create a color from a 24-bit color value.
107
ic_private ic_color_t ic_rgb(uint32_t hex);
108
109
// Create a color from a 8-bit red/green/blue components.
110
// The value of each component is capped between 0 and 255.
111
ic_private ic_color_t ic_rgbx(ssize_t r, ssize_t g, ssize_t b);
112
113
#define IC_COLOR_NONE (0)
114
#define IC_RGB(rgb) (0x1000000 | (uint32_t)(rgb)) // ic_rgb(rgb) // define to it can be used as a constant
115
116
// ANSI colors.
117
// The actual colors used is usually determined by the terminal theme
118
// See <https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit>
119
#define IC_ANSI_BLACK (30)
120
#define IC_ANSI_MAROON (31)
121
#define IC_ANSI_GREEN (32)
122
#define IC_ANSI_OLIVE (33)
123
#define IC_ANSI_NAVY (34)
124
#define IC_ANSI_PURPLE (35)
125
#define IC_ANSI_TEAL (36)
126
#define IC_ANSI_SILVER (37)
127
#define IC_ANSI_DEFAULT (39)
128
129
#define IC_ANSI_GRAY (90)
130
#define IC_ANSI_RED (91)
131
#define IC_ANSI_LIME (92)
132
#define IC_ANSI_YELLOW (93)
133
#define IC_ANSI_BLUE (94)
134
#define IC_ANSI_FUCHSIA (95)
135
#define IC_ANSI_AQUA (96)
136
#define IC_ANSI_WHITE (97)
137
138
#define IC_ANSI_DARKGRAY IC_ANSI_GRAY
139
#define IC_ANSI_LIGHTGRAY IC_ANSI_SILVER
140
#define IC_ANSI_MAGENTA IC_ANSI_FUCHSIA
141
#define IC_ANSI_CYAN IC_ANSI_AQUA
142
143
144
145
//-------------------------------------------------------------
146
// Debug
147
//-------------------------------------------------------------
148
149
#if defined(IC_NO_DEBUG_MSG)
150
#define debug_msg(fmt,...) (void)(0)
151
#else
152
ic_private void debug_msg( const char* fmt, ... );
153
#endif
154
155
156
//-------------------------------------------------------------
157
// Abstract environment
158
//-------------------------------------------------------------
159
struct ic_env_s;
160
typedef struct ic_env_s ic_env_t;
161
162
163
//-------------------------------------------------------------
164
// Allocation
165
//-------------------------------------------------------------
166
167
typedef struct alloc_s {
168
ic_malloc_fun_t* malloc;
169
ic_realloc_fun_t* realloc;
170
ic_free_fun_t* free;
171
} alloc_t;
172
173
174
ic_private void* mem_malloc( alloc_t* mem, ssize_t sz );
175
ic_private void* mem_zalloc( alloc_t* mem, ssize_t sz );
176
ic_private void* mem_realloc( alloc_t* mem, void* p, ssize_t newsz );
177
ic_private void mem_free( alloc_t* mem, const void* p );
178
ic_private char* mem_strdup( alloc_t* mem, const char* s);
179
ic_private char* mem_strndup( alloc_t* mem, const char* s, ssize_t n);
180
181
#define mem_zalloc_tp(mem,tp) (tp*)mem_zalloc(mem,ssizeof(tp))
182
#define mem_malloc_tp_n(mem,tp,n) (tp*)mem_malloc(mem,(n)*ssizeof(tp))
183
#define mem_zalloc_tp_n(mem,tp,n) (tp*)mem_zalloc(mem,(n)*ssizeof(tp))
184
#define mem_realloc_tp(mem,tp,p,n) (tp*)mem_realloc(mem,p,(n)*ssizeof(tp))
185
186
187
#endif // IC_COMMON_H
188
189