1 #ifndef R_STR_H
2 #define R_STR_H
3 
4 #include <wchar.h>
5 #include "r_str_util.h"
6 #include "r_list.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 typedef enum {
13 	R_STRING_ENC_LATIN1 = 'a',
14 	R_STRING_ENC_UTF8 = '8',
15 	R_STRING_ENC_UTF16LE = 'u',
16 	R_STRING_ENC_UTF32LE = 'U',
17 	R_STRING_ENC_UTF16BE = 'b',
18 	R_STRING_ENC_UTF32BE = 'B',
19 	R_STRING_ENC_GUESS = 'g',
20 } RStrEnc;
21 
22 typedef int (*RStrRangeCallback) (void *, int);
23 
24 typedef struct r_charset_rune_t {
25 	ut8 *ch;
26 	ut8 *hx;
27 	struct r_charset_rune_t *left;
28 	struct r_charset_rune_t *right;
29 } RCharsetRune;
30 
31 typedef struct r_charset_t {
32 	Sdb *db;
33 	Sdb *db_char_to_hex;
34 	RCharsetRune *custom_charset;
35 	size_t remaining;
36 } RCharset;
37 
38 #define R_STR_ISEMPTY(x) (!(x) || !*(x))
39 #define R_STR_ISNOTEMPTY(x) ((x) && *(x))
40 #define R_STR_DUP(x) ((x) ? strdup ((x)) : NULL)
41 #define r_str_array(x,y) ((y>=0 && y<(sizeof(x)/sizeof(*x)))?x[y]:"")
42 R_API RCharset *r_charset_new(void);
43 R_API void r_charset_free(RCharset *charset);
44 R_API RCharsetRune *r_charset_rune_new(const ut8 *ch, const ut8 *hx);
45 R_API void r_charset_rune_free(RCharsetRune *rcr);
46 R_API size_t r_charset_encode_str(RCharset *rc, ut8 *out, size_t out_len, const ut8 *in, size_t in_len);
47 R_API size_t r_charset_decode_str(RCharset *rc, ut8 *out, size_t out_len, const ut8 *in, size_t in_len);
48 R_API bool r_charset_open(RCharset *c, const char *cs);
49 R_API RCharsetRune * add_rune(RCharsetRune *rcsr, const ut8 *ch, const ut8 *hx);
50 R_API RCharsetRune *search_from_hex(RCharsetRune *rcsr, const ut8 *hx);
51 R_API RCharsetRune *search_from_char(RCharsetRune *rcsr, const ut8 *ch);
52 
53 // str
54 R_API char *r_str_repeat(const char *ch, int sz);
55 R_API const char *r_str_pad(const char ch, int len);
56 R_API const char *r_str_rstr(const char *base, const char *p);
57 R_API const char *r_strstr_ansi (const char *a, const char *b);
58 R_API const char *r_str_rchr(const char *base, const char *p, int ch);
59 R_API const char *r_str_closer_chr(const char *b, const char *s);
60 R_API int r_str_bounds(const char *str, int *h);
61 R_API char *r_str_crop(const char *str, unsigned int x, unsigned int y, unsigned int x2, unsigned int y2);
62 R_API char *r_str_scale(const char *r, int w, int h);
63 R_API bool r_str_range_in(const char *r, ut64 addr);
64 R_API size_t r_str_len_utf8(const char *s);
65 R_API size_t r_str_len_utf8_ansi(const char *str);
66 R_API size_t r_str_len_utf8char(const char *s, int left);
67 R_API size_t r_str_utf8_charsize(const char *str);
68 R_API size_t r_str_utf8_charsize_prev(const char *str, int prev_len);
69 R_API size_t r_str_utf8_charsize_last(const char *str);
70 R_API void r_str_filter_zeroline(char *str, int len);
71 R_API size_t r_str_utf8_codepoint(const char *s, size_t left);
72 R_API bool r_str_char_fullwidth(const char *s, size_t left);
73 R_API int r_str_write(int fd, const char *b);
74 R_API size_t r_str_ncpy(char *dst, const char *src, size_t n);
75 R_API void r_str_sanitize(char *c);
76 R_API char *r_str_sanitize_sdb_key(const char *s);
77 R_API const char *r_str_casestr(const char *a, const char *b);
78 R_API const char *r_str_firstbut(const char *s, char ch, const char *but);
79 R_API const char *r_str_lastbut(const char *s, char ch, const char *but);
80 R_API int r_str_split(char *str, char ch);
81 R_API RList *r_str_split_list(char *str, const char *c, int n);
82 R_API RList *r_str_split_duplist(const char *str, const char *c, bool trim);
83 R_API size_t *r_str_split_lines(char *str, size_t *count);
84 R_API char* r_str_replace(char *str, const char *key, const char *val, int g);
85 R_API char *r_str_replace_icase(char *str, const char *key, const char *val, int g, int keep_case);
86 R_API char *r_str_replace_in(char *str, ut32 sz, const char *key, const char *val, int g);
87 #define r_str_cpy(x,y) memmove ((x), (y), strlen (y) + 1);
88 #define r_str_cat(x,y) memmove ((x) + strlen (x), (y), strlen (y) + 1);
89 R_API int r_str_bits(char *strout, const ut8 *buf, int len, const char *bitz);
90 R_API int r_str_bits64(char *strout, ut64 in);
91 R_API ut64 r_str_bits_from_string(const char *buf, const char *bitz);
92 R_API int r_str_rwx(const char *str);
93 R_API int r_str_replace_ch(char *s, char a, char b, bool g);
94 R_API int r_str_replace_char(char *s, int a, int b);
95 R_API int r_str_replace_char_once(char *s, int a, int b);
96 R_API void r_str_remove_char(char *str, char c);
97 R_API const char *r_str_rwx_i(int rwx);
98 R_API int r_str_fmtargs(const char *fmt);
99 R_API char *r_str_arg_escape(const char *arg);
100 R_API int r_str_arg_unescape(char *arg);
101 R_API char **r_str_argv(const char *str, int *_argc);
102 R_API void r_str_argv_free(char **argv);
103 R_API char *r_str_new(const char *str);
104 R_API int r_snprintf (char *string, int len, const char *fmt, ...) R_PRINTF_CHECK(3, 4);
105 R_API bool r_str_is_ascii(const char *str);
106 R_API char *r_str_nextword(char *s, char ch);
107 R_API bool r_str_is_printable(const char *str);
108 R_API bool r_str_is_printable_limited(const char *str, int size);
109 R_API bool r_str_is_printable_incl_newlines(const char *str);
110 R_API char *r_str_appendlen(char *ptr, const char *string, int slen);
111 R_API char *r_str_newf(const char *fmt, ...) R_PRINTF_CHECK(1, 2);
112 R_API char *r_str_newlen(const char *str, int len);
113 R_API const char *r_str_sysbits(const int v);
114 R_API char *r_str_trunc_ellipsis(const char *str, int len);
115 R_API const char *r_str_bool(int b);
116 R_API bool r_str_is_true(const char *s);
117 R_API bool r_str_is_false(const char *s);
118 R_API bool r_str_is_bool(const char *val);
119 R_API const char *r_str_ansi_chrn(const char *str, size_t n);
120 R_API size_t r_str_ansi_len(const char *str);
121 R_API size_t r_str_ansi_nlen(const char *str, size_t len);
122 R_API int r_str_ansi_trim(char *str, int str_len, int n);
123 R_API int r_str_ansi_filter(char *str, char **out, int **cposs, int len);
124 R_API char *r_str_ansi_crop(const char *str, unsigned int x, unsigned int y, unsigned int x2, unsigned int y2);
125 R_API int r_str_word_count(const char *string);
126 R_API int r_str_char_count(const char *string, char ch);
127 R_API char *r_str_word_get0set(char *stra, int stralen, int idx, const char *newstr, int *newlen);
128 R_API int r_str_word_set0(char *str);
129 R_API int r_str_word_set0_stack(char *str);
130 R_API const char *r_str_word_get0(const char *str, int idx);
131 R_API char *r_str_word_get_first(const char *string);
132 R_API void r_str_trim(char *str);
133 R_API char *r_str_wrap(const char *str, int w);
134 R_API char *r_str_trim_dup(const char *str);
135 R_API char *r_str_trim_lines(char *str);
136 R_API void r_str_trim_head(char *str);
137 R_API const char *r_str_trim_head_ro(const char *str);
138 R_API const char *r_str_trim_head_wp(const char *str);
139 R_API void r_str_trim_tail(char *str);
140 R_API ut32 r_str_hash(const char *str);
141 R_API ut64 r_str_hash64(const char *str);
142 R_API char *r_str_trim_nc(char *str);
143 R_API const char *r_str_nstr(const char *from, const char *to, int size);
144 R_API const char *r_str_lchr(const char *str, char chr);
145 R_API const char *r_sub_str_lchr(const char *str, int start, int end, char chr);
146 R_API const char *r_sub_str_rchr(const char *str, int start, int end, char chr);
147 R_API char *r_str_ichr(char *str, char chr);
148 R_API bool r_str_ccmp(const char *dst, const char *orig, int ch);
149 R_API bool r_str_cmp_list(const char *list, const char *item, char sep);
150 R_API int r_str_cmp(const char *dst, const char *orig, int len);
151 R_API int r_str_casecmp(const char *dst, const char *orig);
152 R_API int r_str_ncasecmp(const char *dst, const char *orig, size_t n);
153 R_API int r_str_ccpy(char *dst, char *orig, int ch);
154 R_API const char *r_str_get(const char *str);
155 R_API const char *r_str_get_fail(const char *str, const char *failstr);
156 R_API const char *r_str_getf(const char *str);
157 R_API char *r_str_ndup(const char *ptr, int len);
158 R_API char *r_str_dup(char *ptr, const char *string);
159 R_API int r_str_inject(char *begin, char *end, char *str, int maxlen);
160 R_API int r_str_delta(char *p, char a, char b);
161 R_API void r_str_filter(char *str, int len);
162 R_API const char * r_str_tok(const char *str1, const char b, size_t len);
163 R_API wchar_t *r_str_mb_to_wc(const char *buf);
164 R_API char *r_str_wc_to_mb(const wchar_t *buf);
165 R_API wchar_t *r_str_mb_to_wc_l(const char *buf, int len);
166 R_API char *r_str_wc_to_mb_l(const wchar_t *buf, int len);
167 R_API const char *r_str_str_xy(const char *s, const char *word, const char *prev, int *x, int *y);
168 
169 typedef void(*str_operation)(char *c);
170 
171 R_API int r_str_do_until_token(str_operation op, char *str, const char tok);
172 
173 R_API void r_str_reverse(char *str);
174 R_API int r_str_re_match(const char *str, const char *reg);
175 R_API int r_str_re_replace(const char *str, const char *reg, const char *sub);
176 R_API int r_str_path_unescape(char *path);
177 R_API char *r_str_path_escape(const char *path);
178 R_API int r_str_unescape(char *buf);
179 R_API char *r_str_sanitize_r2(const char *buf);
180 R_API char *r_str_escape(const char *buf);
181 R_API char *r_str_escape_sh(const char *buf);
182 R_API char *r_str_escape_sql(const char *buf);
183 R_API char *r_str_escape_dot(const char *buf);
184 R_API char *r_str_escape_latin1(const char *buf, bool show_asciidot, bool esc_bslash, bool colors);
185 R_API char *r_str_escape_utf8(const char *buf, bool show_asciidot, bool esc_bslash);
186 R_API char *r_str_escape_utf8_keep_printable(const char *buf, bool show_asciidot, bool esc_bslash); // like escape_utf8 but leaves valid \uXXXX chars directly in utf-8
187 R_API char *r_str_escape_utf16le(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
188 R_API char *r_str_escape_utf32le(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
189 R_API char *r_str_escape_utf16be(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
190 R_API char *r_str_escape_utf32be(const char *buf, int buf_size, bool show_asciidot, bool esc_bslash);
191 R_API void r_str_byte_escape(const char *p, char **dst, int dot_nl, bool default_dot, bool esc_bslash);
192 R_API char *r_str_format_msvc_argv(size_t argc, const char **argv);
193 R_API void r_str_uri_decode(char *buf);
194 R_API char *r_str_uri_encode(const char *buf);
195 R_API char *r_str_utf16_decode(const ut8 *s, int len);
196 R_API int r_str_utf16_to_utf8(ut8 *dst, int len_dst, const ut8 *src, int len_src, int little_endian);
197 R_API char *r_str_utf16_encode(const char *s, int len);
198 R_API char *r_str_escape_utf8_for_json(const char *s, int len);
199 R_API char *r_str_escape_utf8_for_json_strip(const char *buf, int buf_size);
200 R_API char *r_str_encoded_json(const char *buf, int buf_size, int encoding);
201 R_API char *r_str_home(const char *str);
202 R_API char *r_str_r2_prefix(const char *str);
203 R_API size_t r_str_nlen(const char *s, int n);
204 R_API size_t r_str_nlen_w(const char *s, int n);
205 R_API size_t r_wstr_clen(const char *s);
206 R_API char *r_str_prepend(char *ptr, const char *string);
207 R_API char *r_str_prefix_all(const char *s, const char *pfx);
208 R_API char *r_str_append(char *ptr, const char *string);
209 R_API char *r_str_append_owned(char *ptr, char *string);
210 R_API char *r_str_appendf(char *ptr, const char *fmt, ...) R_PRINTF_CHECK(2, 3);
211 R_API char *r_str_appendch(char *x, char y);
212 R_API void r_str_case(char *str, bool up);
213 R_API void r_str_trim_path(char *s);
214 R_API ut8 r_str_contains_macro(const char *input_value);
215 R_API void r_str_truncate_cmd(char *string);
216 R_API char* r_str_replace_thunked(char *str, char *clean, int *thunk, int clen,
217 				  const char *key, const char *val, int g);
218 R_API bool r_str_glob(const char *str, const char *glob);
219 R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen);
220 R_API char *r_str_between(const char *str, const char *prefix, const char *suffix);
221 R_API bool r_str_startswith(const char *str, const char *needle);
222 R_API bool r_str_endswith(const char *str, const char *needle);
223 R_API bool r_str_isnumber (const char *str);
224 R_API const char *r_str_last (const char *in, const char *ch);
225 R_API char* r_str_highlight(char *str, const char *word, const char *color, const char *color_reset);
226 R_API char *r_qrcode_gen(const ut8 *text, int len, bool utf8, bool inverted);
227 R_API char *r_str_from_ut64(ut64 val);
228 R_API void r_str_stripLine(char *str, const char *key);
229 R_API char *r_str_list_join(RList *str, const char *sep);
230 R_API char *r_str_array_join(const char **a, size_t n, const char *sep);
231 
232 R_API const char *r_str_sep(const char *base, const char *sep);
233 R_API const char *r_str_rsep(const char *base, const char *p, const char *sep);
234 R_API char *r_str_donut(int size);
235 R_API char *r_str_version(const char *program);
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif //  R_STR_H
242