Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generated by V
- #define linux (1)
- #include <pthread.h>
- #include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
- #include <stdlib.h>
- #include <signal.h>
- #include <stdarg.h> // for va_list
- #include <inttypes.h> // int64_t etc
- //================================== TYPEDEFS ================================*/
- typedef unsigned char byte;
- typedef unsigned int uint;
- typedef int64_t i64;
- typedef int32_t i32;
- typedef int16_t i16;
- typedef int8_t i8;
- typedef uint64_t u64;
- typedef uint32_t u32;
- typedef uint16_t u16;
- typedef uint8_t u8;
- typedef uint32_t rune;
- typedef float f32;
- typedef double f64;
- typedef unsigned char* byteptr;
- typedef int* intptr;
- typedef void* voidptr;
- typedef struct array array;
- typedef struct map map;
- typedef array array_string;
- typedef array array_int;
- typedef array array_byte;
- typedef array array_uint;
- typedef array array_float;
- typedef map map_int;
- typedef map map_string;
- #ifndef bool
- typedef int bool;
- #define true 1
- #define false 0
- #endif
- //============================== HELPER C MACROS =============================*/
- #define _PUSH(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array__push(arr, &tmp);}
- #define _IN(typ, val, arr) array_##typ##_contains(arr, val)
- #define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))
- #define UTF8_CHAR_LEN( byte ) (( 0xE5000000 >> (( byte >> 3 ) & 0x1e )) & 3 ) + 1
- //================================== GLOBALS =================================*/
- //int V_ZERO = 0;
- byteptr g_str_buf;
- int load_so(byteptr);
- void reload_so();
- void init_consts();
- i64 total_m = 0; // For counting total RAM allocated
- int g_test_ok = 1;
- /*================================== FNS =================================*/
- #include <sys/stat.h>
- #include <execinfo.h>
- #include <signal.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <errno.h>
- #define MAX 1000
- #define MAX 5000
- typedef struct array array;
- typedef array array_int;
- typedef array array_string;
- typedef struct string string;
- typedef struct ustring ustring;
- typedef array array_byte;
- typedef struct map map;
- typedef array array_Entry;
- typedef struct Entry Entry;
- typedef struct Entry2 Entry2;
- typedef struct smap smap;
- typedef array array_Entry2;
- typedef struct Option Option;
- typedef struct StringBuilder StringBuilder;
- typedef struct os__FILE os__FILE;
- typedef struct os__File os__File;
- typedef array array_ustring;
- typedef struct os__Reader os__Reader;
- typedef struct os__FileInfo os__FileInfo;
- struct array {
- void* data;
- int len;
- int cap;
- int element_size;
- };
- struct string {
- byte* str;
- int len;
- };
- struct ustring {
- string s;
- array_int runes;
- int len;
- };
- struct map {
- int element_size;
- array_Entry entries;
- bool is_sorted;
- };
- struct Entry {
- string key;
- void* val;
- };
- struct Entry2 {
- string key;
- string val;
- };
- struct smap {
- array_Entry2 entries;
- bool is_sorted;
- };
- struct Option {
- void* data;
- string error;
- bool ok;
- };
- struct StringBuilder {
- array_byte buf;
- int len;
- };
- struct os__FILE {
- };
- struct os__File {
- os__FILE* cfile;
- };
- struct os__Reader {
- os__FILE* fp;
- };
- struct os__FileInfo {
- string name;
- int size;
- };
- string _STR(const char*, ...);
- string _STR_TMP(const char*, ...);
- array new_array(int mylen, int cap, int elm_size);
- array new_array_from_c_array(int len, int cap, int elm_size, void* c_array);
- array new_array_from_c_array_no_alloc(int len, int cap, int elm_size, void* c_array);
- array array_repeat(void* val, int nr_repeats, int elm_size);
- void array_append_array(array* a, array b);
- void array_sort_with_compare(array* a, void* compare);
- void array_insert(array* a, int i, void* val);
- void array_prepend(array* a, void* val);
- void array_delete(array* a, int idx);
- void* array__get(array a, int i);
- void* array_first(array a);
- void* array_last(array a);
- array array_left(array s, int n);
- array array_right(array s, int n);
- array array_slice(array s, int start, int _end);
- void array_set(array* a, int idx, void* val);
- void array__push(array* arr, void* val);
- void array__push_many(array* arr, void* val, int size);
- string array_int_str(array_int a);
- void v_array_int_free(array_int a);
- string array_string_str(array_string a);
- void v_free(void* a);
- string tos_clone(byte* s);
- string tos2(byte* s);
- string tos_no_len(byte* s);
- string string_clone(string a);
- byte* string_cstr(string s);
- string string_replace(string s, string rep, string with);
- int string_to_i(string s);
- float string_to_float(string s);
- bool string_eq(string s, string a);
- bool string_ne(string s, string a);
- bool string_ge(string s, string a);
- bool string_le(string s, string a);
- bool string_lt(string s, string a);
- bool string_gt(string s, string a);
- string string_add(string s, string a);
- array_string string_split(string s, string delim);
- array_string string_split_single(string s, byte delim);
- array_string string_split_into_lines(string s);
- string string_left(string s, int n);
- string string_right(string s, int n);
- string string_substr(string s, int start, int end);
- int string_index(string s, string p);
- int string_last_index(string s, string p);
- int string_index_after(string s, string p, int start);
- bool string_contains(string s, string p);
- bool string_starts_with(string s, string p);
- bool string_ends_with(string s, string p);
- string string_to_lower(string s);
- string string_to_upper(string s);
- string string_find_between(string s, string start, string end);
- bool array_string_contains(array_string ar, string val);
- bool array_int_contains(array_int ar, int val);
- void* array_string_to_c(array_string a);
- bool is_space(byte c);
- bool byte_is_space(byte c);
- string string_trim_space(string s);
- string string_trim(string s, byte c);
- string string_trim_left(string s, string cutset);
- string string_trim_right(string s, string cutset);
- int compare_strings(string* a, string* b);
- int compare_strings_by_len(string* a, string* b);
- int compare_lower_strings(string* a, string* b);
- void array_string_sort(array_string* s);
- void array_string_sort_ignore_case(array_string* s);
- void array_string_sort_by_len(array_string* s);
- ustring string_ustring(string s);
- ustring string_ustring_tmp(string s);
- string ustring_substr(ustring u, int start, int end);
- string ustring_left(ustring u, int pos);
- string ustring_right(ustring u, int pos);
- byte string_at(string s, int idx);
- string ustring_at(ustring u, int idx);
- void v_ustring_free(ustring u);
- int abs(int a);
- bool byte_is_digit(byte c);
- bool byte_is_letter(byte c);
- void v_string_free(string s);
- void v_array_string_free(array_string arr);
- string string_all_before(string s, string dot);
- string string_all_before_last(string s, string dot);
- string string_all_after(string s, string dot);
- string array_string_join(array_string a, string del);
- string array_string_join_lines(array_string s);
- string string_limit(string s, int max);
- bool byte_is_white(byte c);
- string repeat_char(byte c, int n);
- int string_hash(string s);
- void v_exit(string reason);
- bool isnil(void* v);
- void on_panic(int (*f)( int /*FFF*/ ));
- void print_backtrace();
- void v_panic(string s);
- void println(string s);
- void eprintln(string s);
- void v_print(string s);
- void println2(string s);
- byte* v_malloc(int n);
- byte* v_calloc(int n);
- int _strlen(byte* s);
- Option opt_ok(void* data);
- void* memdup(void* src, int sz);
- Option v_error(string s);
- array_int range_int(int start, int end);
- string double_str(double d);
- string float_str(float d);
- string f64_str(f64 d);
- string f32_str(f32 d);
- string ptr_str(void* ptr);
- string int_str(int nn);
- string u8_str(u8 nn);
- string i64_str(i64 nn);
- string bool_str(bool b);
- string int_hex(int n);
- string i64_hex(i64 n);
- bool array_byte_contains(array_byte a, byte val);
- string byte_str(byte c);
- int string_is_utf8(string s);
- string utf32_to_str(u32 code);
- string utf32_to_str_no_malloc(u32 code, void* buf);
- int string_utf32_code(string _rune);
- map new_map(int cap, int elm_size);
- Entry map_new_entry(map* m, string key, void* val);
- void map__set(map* m, string key, void* val);
- int volt_abs(int n);
- void map_bs(map m, string query, int start, int end, void* out);
- int compare_map(Entry* a, Entry* b);
- void map_sort(map* m);
- bool map_get(map m, string key, void* out);
- void v_map_print(map m);
- void v_map_free(map m);
- string map_string_str(map_string m);
- smap new_smap();
- void smap_set(smap* m, string key, string val);
- string smap_get(smap m, string key);
- string smap_bs(smap m, string query, int start, int end);
- int compare_smap(Entry2* a, Entry2* b);
- void smap_sort(smap* m);
- void v_smap_free(smap m);
- string smap_str(smap m);
- StringBuilder new_string_builder(int initial_size);
- void StringBuilder_write(StringBuilder* b, string s);
- void StringBuilder_writeln(StringBuilder* b, string s);
- string StringBuilder_str(StringBuilder b);
- void StringBuilder_cut(StringBuilder b, int n);
- array_string os__init_os_args(int argc, void* c);
- void os__parse_windows_cmd_line(byte* cmd);
- string os__read_file(string path);
- string os__File_read_rune(os__File f);
- int os__file_size(string path);
- int os__file_last_mod_unix(string path);
- array_string os__read_lines(string path);
- array_string os__read_file_into_lines(string path);
- array_ustring os__read_file_into_ulines(string path);
- array_string os__read_file_lines(string path);
- void os__append_to_file(string file, string s);
- os__File os__open(string path);
- os__File os__open_file(string file);
- os__File os__create(string path);
- os__File os__open_append(string path);
- os__File os__create_file(string file);
- os__File os__create_file_a(string file);
- os__File os__open_file_a(string file);
- os__File os__create_file2(string file, string mode);
- void os__File_append(os__File f, string s);
- void os__File_write(os__File f, void* data, int size);
- void os__File_write_at(os__File f, void* data, int size, int pos);
- void os__File_appendln(os__File f, string s);
- void os__File_close(os__File f);
- void os__close_file(os__FILE* fp);
- int os__system2(string cmd);
- os__FILE* os__popen(string path);
- string os__system(string cmd);
- array_string os__system_into_lines(string s);
- string os__getenv(string key);
- void os__exit(string reason);
- void os__exit1(string reason);
- bool os__file_exists(string path);
- void os__mkdir(string path);
- void os__rm(string path);
- void os__rmdir(string path, string guard);
- void os__unzip(string path, string out);
- void os__print_c_errno();
- string os__basedir(string path);
- string os__filename(string path);
- string os__get_line();
- string os__user_os();
- string os__home_dir();
- void os__write_file(string path, string text);
- void os__on_segfault(void* f);
- void os__log(string s);
- bool os__is_dir(string path);
- void os__chdir(string path);
- string os__getwd();
- array_string os__ls(string path);
- void os__print_backtrace();
- string filter_word(string word);
- bool is_letter(byte c);
- array_string os__args;
- #define os__BUF_SIZE 5000
- array new_array(int mylen, int cap, int elm_size) {
- array arr= /* S INIT */ (array){ .len = mylen , .cap = cap , .element_size = elm_size , .data = v_malloc ( cap * elm_size ) } ;
- return arr ;
- }
- array new_array_from_c_array(int len, int cap, int elm_size, void* c_array) {
- array arr= /* S INIT */ (array){ .len = len , .cap = cap , .element_size = elm_size , .data = v_malloc ( cap * elm_size ) } ;
- memcpy ( arr .data , c_array , len * elm_size ) ;
- return arr ;
- }
- array new_array_from_c_array_no_alloc(int len, int cap, int elm_size, void* c_array) {
- array arr= /* S INIT */ (array){ .len = len , .cap = cap , .element_size = elm_size , .data = c_array } ;
- return arr ;
- }
- array array_repeat(void* val, int nr_repeats, int elm_size) {
- array arr= /* S INIT */ (array){ .len = nr_repeats , .cap = nr_repeats , .element_size = elm_size , .data = v_malloc ( nr_repeats * elm_size ) } ;
- for (
- int i= 0 ; i < nr_repeats ; i ++ ) {
- memcpy ( arr .data + i * elm_size , val , elm_size ) ;
- }
- ;
- return arr ;
- }
- void array_append_array(array* a, array b) {
- for (
- int i= 0 ; i < b .len ; i ++ ) {
- void* val= ( *(void**) array__get( b , i) ) ;
- array__push( a , val ) ;
- }
- ;
- }
- void array_sort_with_compare(array* a, void* compare) {
- qsort ( a ->data , a ->len , a ->element_size , compare ) ;
- }
- void array_insert(array* a, int i, void* val) {
- if ( i >= a ->len ) {
- /*if*/
- v_panic ( tos2("array.insert: index larger than length") ) ;
- return ;
- }
- ;
- array__push( a , val ) ;
- int size= a ->element_size ;
- memmove ( a ->data + (/*lpar*/ i + 1 ) * size , a ->data + i * size , (/*lpar*/ a ->len - i ) * size ) ;
- array_set( a , i , val ) ;
- }
- void array_prepend(array* a, void* val) {
- array_insert( a , 0 , val ) ;
- }
- void array_delete(array* a, int idx) {
- int size= a ->element_size ;
- memmove ( a ->data + idx * size , a ->data + (/*lpar*/ idx + 1 ) * size , (/*lpar*/ a ->len - idx ) * size ) ;
- a ->len -- ;
- a ->cap -- ;
- }
- void* array__get(array a, int i) {
- if ( i < 0 || i >= a .len ) {
- /*if*/
- v_panic ( _STR("array index out of range: %d/%d", i, a .len) ) ;
- }
- ;
- return a .data + i * a .element_size ;
- }
- void* array_first(array a) {
- if ( a .len == 0 ) {
- /*if*/
- v_panic ( tos2("array.first: empty array") ) ;
- }
- ;
- return a .data + 0 ;
- }
- void* array_last(array a) {
- if ( a .len == 0 ) {
- /*if*/
- v_panic ( tos2("array.last: empty array") ) ;
- }
- ;
- return a .data + (/*lpar*/ a .len - 1 ) * a .element_size ;
- }
- array array_left(array s, int n) {
- if ( n >= s .len ) {
- /*if*/
- return s ;
- }
- ;
- return array_slice( s , 0 , n ) ;
- }
- array array_right(array s, int n) {
- if ( n >= s .len ) {
- /*if*/
- return s ;
- }
- ;
- return array_slice( s , n , s .len ) ;
- }
- array array_slice(array s, int start, int _end) {
- int end= _end ;
- if ( start > end ) {
- /*if*/
- v_panic ( _STR("invalid slice index: %d > %d", start, end) ) ;
- }
- ;
- if ( end >= s .len ) {
- /*if*/
- end = s .len ;
- }
- ;
- int l= end - start ;
- array res= /* S INIT */ (array){ .element_size = s .element_size , .data = s .data + start * s .element_size , .len = l , .cap = l } ;
- return res ;
- }
- void array_set(array* a, int idx, void* val) {
- if ( idx < 0 || idx >= a ->len ) {
- /*if*/
- v_panic ( _STR("array index out of range: %d / %d", idx, a ->len) ) ;
- }
- ;
- memcpy ( a ->data + a ->element_size * idx , val , a ->element_size ) ;
- }
- void array__push(array* arr, void* val) {
- if ( arr ->len >= arr ->cap - 1 ) {
- /*if*/
- int cap= (/*lpar*/ arr ->len + 1 ) * 2 ;
- if ( arr ->cap == 0 ) {
- /*if*/
- arr ->data = v_malloc ( cap * arr ->element_size ) ;
- }
- else {
- /*else if*/
- arr ->data = realloc ( arr ->data , cap * arr ->element_size ) ;
- }
- ;
- arr ->cap = cap ;
- }
- ;
- memcpy ( arr ->data + arr ->element_size * arr ->len , val , arr ->element_size ) ;
- arr ->len ++ ;
- }
- void array__push_many(array* arr, void* val, int size) {
- if ( arr ->len >= arr ->cap - size ) {
- /*if*/
- int cap= (/*lpar*/ arr ->len + size ) * 2 ;
- if ( arr ->cap == 0 ) {
- /*if*/
- arr ->data = v_malloc ( cap * arr ->element_size ) ;
- }
- else {
- /*else if*/
- arr ->data = realloc ( arr ->data , cap * arr ->element_size ) ;
- }
- ;
- arr ->cap = cap ;
- }
- ;
- memcpy ( arr ->data + arr ->element_size * arr ->len , val , arr ->element_size * size ) ;
- arr ->len += size ;
- }
- string array_int_str(array_int a) {
- string res= tos2("[") ;
- for (
- int i= 0 ; i < a .len ; i ++ ) {
- int val= ( *(int*) array__get( a , i) ) ;
- res = string_add(res, _STR("%d", val) ) ;
- if ( i < a .len - 1 ) {
- /*if*/
- res = string_add(res, tos2(", ") ) ;
- }
- ;
- }
- ;
- res = string_add(res, tos2("]") ) ;
- return res ;
- }
- void v_array_int_free(array_int a) {
- free ( a .data ) ;
- }
- string array_string_str(array_string a) {
- string res= tos2("[") ;
- for (
- int i= 0 ; i < a .len ; i ++ ) {
- string val= ( *(string*) array__get( a , i) ) ;
- res = string_add(res, _STR("\"%.*s\"", val.len, val.str) ) ;
- if ( i < a .len - 1 ) {
- /*if*/
- res = string_add(res, tos2(", ") ) ;
- }
- ;
- }
- ;
- res = string_add(res, tos2("]") ) ;
- return res ;
- }
- void v_free(void* a) {
- free ( a ) ;
- }
- string tos(byte* s, int len) {
- if ( isnil ( s ) ) {
- /*if*/
- v_panic ( tos2("tos(): nil string") ) ;
- }
- ;
- return /* S INIT */ (string){ .str = s , .len = len } ;
- }
- string tos_clone(byte* s) {
- if ( isnil ( s ) ) {
- /*if*/
- v_panic ( tos2("tos: nil string") ) ;
- return /* S INIT */ (string){ .str = 0 , .len = 0 } ;
- }
- ;
- int len= strlen ( s ) ;
- string res= tos ( s , len ) ;
- return string_clone( res ) ;
- }
- string tos2(byte* s) {
- if ( isnil ( s ) ) {
- /*if*/
- v_panic ( tos2("tos2: nil string") ) ;
- return /* S INIT */ (string){ .str = 0 , .len = 0 } ;
- }
- ;
- int len= strlen ( s ) ;
- string res= tos ( s , len ) ;
- return res ;
- }
- string tos_no_len(byte* s) {
- return tos2 ( s ) ;
- }
- string string_clone(string a) {
- string b= /* S INIT */ (string){ .len = a .len , .str = v_malloc ( a .len + 1 ) } ;
- for (
- int i= 0 ; i < a .len ; i ++ ) {
- b .str[ i ]/*rbyte 1*/ = a .str[ i ]/*rbyte 0*/ ;
- }
- ;
- b .str[ a .len ]/*rbyte 1*/ = '\0' ;
- return b ;
- }
- byte* string_cstr(string s) {
- string clone= string_clone( s ) ;
- return clone .str ;
- }
- string string_replace(string s, string rep, string with) {
- if ( s .len == 0 || rep .len == 0 ) {
- /*if*/
- return tos2("") ;
- }
- ;
- if ( ! string_contains( s , rep ) ) {
- /*if*/
- return s ;
- }
- ;
- array_int idxs=/*0*/ new_array_from_c_array(0, 0, sizeof(int), (int[]) { }) ;
- {
- }
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- int rep_i= 0 ;
- int j= i ;
- while ( rep_i < rep .len && j < s .len && s .str[ j ]/*rbyte 0*/ == rep .str[ rep_i ]/*rbyte 0*/ ) {
- rep_i ++ ;
- j ++ ;
- }
- ;
- if ( rep_i == rep .len ) {
- /*if*/
- _PUSH(& idxs , ( i ), tmp12, int) ;
- }
- ;
- }
- ;
- if ( idxs .len == 0 ) {
- /*if*/
- return s ;
- }
- ;
- int new_len= s .len + idxs .len * (/*lpar*/ with .len - rep .len ) ;
- byte* b= v_malloc ( new_len + 1 ) ;
- int idx_pos= 0 ;
- int cur_idx= ( *(int*) array__get( idxs , idx_pos) ) ;
- int b_i= 0 ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- if ( i == cur_idx ) {
- /*if*/
- for (
- int j= 0 ; j < with .len ; j ++ ) {
- b [/*ptr*/ b_i ]/*rbyte 1*/ = with .str[ j ]/*rbyte 0*/ ;
- b_i ++ ;
- }
- ;
- i += rep .len - 1 ;
- idx_pos ++ ;
- if ( idx_pos < idxs .len ) {
- /*if*/
- cur_idx = ( *(int*) array__get( idxs , idx_pos) ) ;
- }
- ;
- }
- else {
- /*else if*/
- b [/*ptr*/ b_i ]/*rbyte 1*/ = s .str[ i ]/*rbyte 0*/ ;
- b_i ++ ;
- }
- ;
- }
- ;
- b [/*ptr*/ new_len ]/*rbyte 1*/ = '\0' ;
- return tos ( b , new_len ) ;
- }
- int string_to_i(string s) {
- return atoi ( s .str ) ;
- }
- float string_to_float(string s) {
- return atof ( s .str ) ;
- }
- bool string_eq(string s, string a) {
- if ( isnil ( s .str ) ) {
- /*if*/
- v_panic ( tos2("string.eq(): nil string") ) ;
- }
- ;
- if ( s .len != a .len ) {
- /*if*/
- return 0 ;
- }
- ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- if ( s .str[ i ]/*rbyte 0*/ != a .str[ i ]/*rbyte 0*/ ) {
- /*if*/
- return 0 ;
- }
- ;
- }
- ;
- return 1 ;
- }
- bool string_ne(string s, string a) {
- return ! string_eq( s , a ) ;
- }
- bool string_ge(string s, string a) {
- int j= 0 ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- if ( i >= a .len ) {
- /*if*/
- return 1 ;
- }
- ;
- if ( (/*casttt*/ (int)( /*77*/ s .str[ i ]/*rbyte 0*/ ) ) < (/*casttt*/ (int)( /*77*/ a .str[ j ]/*rbyte 0*/ ) ) ) {
- /*if*/
- return 0 ;
- }
- else if ( (/*casttt*/ (int)( /*77*/ s .str[ i ]/*rbyte 0*/ ) ) > (/*casttt*/ (int)( /*77*/ a .str[ j ]/*rbyte 0*/ ) ) ) {
- /*if*/
- return 1 ;
- }
- ;
- j ++ ;
- }
- ;
- return 1 ;
- }
- bool string_le(string s, string a) {
- return ! string_ge( s , a ) || string_eq( s , a )/*8*/ ;
- }
- bool string_lt(string s, string a) {
- return string_le( s , a ) && string_ne( s , a )/*8*/ ;
- }
- bool string_gt(string s, string a) {
- return string_ge( s , a ) && string_ne( s , a )/*8*/ ;
- }
- string string_add(string s, string a) {
- int new_len= a .len + s .len ;
- string res= /* S INIT */ (string){ .len = new_len , .str = v_malloc ( new_len + 1 ) } ;
- for (
- int j= 0 ; j < s .len ; j ++ ) {
- res .str[ j ]/*rbyte 1*/ = s .str[ j ]/*rbyte 0*/ ;
- }
- ;
- for (
- int j= 0 ; j < a .len ; j ++ ) {
- res .str[ s .len + j ]/*rbyte 1*/ = a .str[ j ]/*rbyte 0*/ ;
- }
- ;
- res .str[ new_len ]/*rbyte 1*/ = '\0' ;
- return res ;
- }
- array_string string_split(string s, string delim) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- if ( delim .len == 0 ) {
- /*if*/
- _PUSH(& res , ( s ), tmp32, string) ;
- return res ;
- }
- ;
- if ( delim .len == 1 ) {
- /*if*/
- return string_split_single( s , delim .str[ 0 ]/*rbyte 0*/ ) ;
- }
- ;
- int i= 0 ;
- int start= 0 ;
- while ( i < s .len ) {
- bool a= s .str[ i ]/*rbyte 0*/ == delim .str[ 0 ]/*rbyte 0*/ ;
- int j= 1 ;
- while ( j < delim .len && a ) {
- a = a && s .str[ i + j ]/*rbyte 0*/ == delim .str[ j ]/*rbyte 0*/ ;
- j ++ ;
- }
- ;
- bool last= i == s .len - 1 ;
- if ( a || last ) {
- /*if*/
- if ( last ) {
- /*if*/
- i ++ ;
- }
- ;
- string val= string_substr( s , start , i ) ;
- if ( val .len > 0 ) {
- /*if*/
- if ( string_starts_with( val , delim ) ) {
- /*if*/
- val = string_right( val , delim .len ) ;
- }
- ;
- _PUSH(& res , ( string_trim_space( val ) ), tmp39, string) ;
- }
- ;
- start = i ;
- }
- ;
- i ++ ;
- }
- ;
- return res ;
- }
- array_string string_split_single(string s, byte delim) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- if ( (/*casttt*/ (int)( /*77*/ delim ) ) == 0 ) {
- /*if*/
- _PUSH(& res , ( s ), tmp41, string) ;
- return res ;
- }
- ;
- int i= 0 ;
- int start= 0 ;
- while ( i < s .len ) {
- bool a= s .str[ i ]/*rbyte 0*/ == delim ;
- bool b= i == s .len - 1 ;
- if ( a || b ) {
- /*if*/
- if ( i == s .len - 1 ) {
- /*if*/
- i ++ ;
- }
- ;
- string val= string_substr( s , start , i ) ;
- if ( val .len > 0 ) {
- /*if*/
- _PUSH(& res , ( string_trim_space( val ) ), tmp47, string) ;
- }
- ;
- start = i + 1 ;
- }
- ;
- i ++ ;
- }
- ;
- return res ;
- }
- array_string string_split_into_lines(string s) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- if ( s .len == 0 ) {
- /*if*/
- return res ;
- }
- ;
- int start= 0 ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- bool last= i == s .len - 1 ;
- if ( (/*casttt*/ (int)( /*77*/ s .str[ i ]/*rbyte 0*/ ) ) == 10 || last ) {
- /*if*/
- if ( last ) {
- /*if*/
- i ++ ;
- }
- ;
- string line= string_substr( s , start , i ) ;
- _PUSH(& res , ( line ), tmp53, string) ;
- start = i + 1 ;
- }
- ;
- }
- ;
- return res ;
- }
- string string_left(string s, int n) {
- if ( n >= s .len ) {
- /*if*/
- return s ;
- }
- ;
- return string_substr( s , 0 , n ) ;
- }
- string string_right(string s, int n) {
- if ( n >= s .len ) {
- /*if*/
- return tos2("") ;
- }
- ;
- return string_substr( s , n , s .len ) ;
- }
- string string_substr(string s, int start, int end) {
- if ( start >= s .len ) {
- /*if*/
- return tos2("") ;
- }
- ;
- int len= end - start ;
- string res= /* S INIT */ (string){ .str = s .str + start , .len = len } ;
- return res ;
- }
- int string_index(string s, string p) {
- if ( p .len > s .len ) {
- /*if*/
- return - 1 ;
- }
- ;
- int i= 0 ;
- while ( i < s .len ) {
- int j= 0 ;
- int ii= i ;
- while ( j < p .len && s .str[ ii ]/*rbyte 0*/ == p .str[ j ]/*rbyte 0*/ ) {
- j ++ ;
- ii ++ ;
- }
- ;
- if ( j == p .len ) {
- /*if*/
- return i ;
- }
- ;
- i ++ ;
- }
- ;
- return - 1 ;
- }
- int string_last_index(string s, string p) {
- if ( p .len > s .len ) {
- /*if*/
- return - 1 ;
- }
- ;
- int i= s .len - p .len ;
- while ( i >= 0 ) {
- int j= 0 ;
- while ( j < p .len && s .str[ i + j ]/*rbyte 0*/ == p .str[ j ]/*rbyte 0*/ ) {
- j ++ ;
- }
- ;
- if ( j == p .len ) {
- /*if*/
- return i ;
- }
- ;
- i -- ;
- }
- ;
- return - 1 ;
- }
- int string_index_after(string s, string p, int start) {
- if ( p .len > s .len ) {
- /*if*/
- return - 1 ;
- }
- ;
- int strt= start ;
- if ( start < 0 ) {
- /*if*/
- strt = 0 ;
- }
- ;
- if ( start >= s .len ) {
- /*if*/
- return - 1 ;
- }
- ;
- int i= strt ;
- while ( i < s .len ) {
- int j= 0 ;
- int ii= i ;
- while ( j < p .len && s .str[ ii ]/*rbyte 0*/ == p .str[ j ]/*rbyte 0*/ ) {
- j ++ ;
- ii ++ ;
- }
- ;
- if ( j == p .len ) {
- /*if*/
- return i ;
- }
- ;
- i ++ ;
- }
- ;
- return - 1 ;
- }
- bool string_contains(string s, string p) {
- bool res= string_index( s , p ) > 0 - 1 ;
- return res ;
- }
- bool string_starts_with(string s, string p) {
- bool res= string_index( s , p ) == 0 ;
- return res ;
- }
- bool string_ends_with(string s, string p) {
- if ( p .len > s .len ) {
- /*if*/
- return 0 ;
- }
- ;
- bool res= string_last_index( s , p ) == s .len - p .len ;
- return res ;
- }
- string string_to_lower(string s) {
- byte* b= v_malloc ( s .len ) ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- b [/*ptr*/ i ]/*rbyte 1*/ = tolower ( s .str [/*ptr*/ i ]/*rbyte 0*/ ) ;
- }
- ;
- return tos ( b , s .len ) ;
- }
- string string_to_upper(string s) {
- byte* b= v_malloc ( s .len ) ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- b [/*ptr*/ i ]/*rbyte 1*/ = toupper ( s .str [/*ptr*/ i ]/*rbyte 0*/ ) ;
- }
- ;
- return tos ( b , s .len ) ;
- }
- string string_find_between(string s, string start, string end) {
- int start_pos= string_index( s , start ) ;
- if ( start_pos == - 1 ) {
- /*if*/
- return tos2("") ;
- }
- ;
- string val= string_right( s , start_pos + start .len ) ;
- int end_pos= string_index( val , end ) ;
- if ( end_pos == - 1 ) {
- /*if*/
- return val ;
- }
- ;
- return string_left( val , end_pos ) ;
- }
- bool array_string_contains(array_string ar, string val) {
- array_string tmp75 = ar;
- ;
- for (int tmp76 = 0; tmp76 < tmp75 .len; tmp76 ++) {
- string s = ((string *) tmp75.data)[tmp76];
- if (string_eq( s , val )/*8*/ ) {
- /*if*/
- return 1 ;
- }
- ;
- }
- ;
- return 0 ;
- }
- bool array_int_contains(array_int ar, int val) {
- array_int tmp77 = ar ;
- ;
- for (int i = 0; i < tmp77 .len; i ++) {
- int s = ((int *) tmp77 . data)[i];
- if ( s == val ) {
- /*if*/
- return 1 ;
- }
- ;
- }
- ;
- return 0 ;
- }
- void* array_string_to_c(array_string a) {
- char ** res = malloc(sizeof(char*) * a.len);
- for (
- int i= 0 ; i < a .len ; i ++ ) {
- string val= ( *(string*) array__get( a , i) ) ;
- res[i] = val.str;
- }
- ;
- return res;
- return 0 ;
- }
- bool is_space(byte c) {
- return isspace ( c ) ;
- }
- bool byte_is_space(byte c) {
- return is_space ( c ) ;
- }
- string string_trim_space(string s) {
- if (string_eq( s , tos2("") )/*8*/ ) {
- /*if*/
- return tos2("") ;
- }
- ;
- int i= 0 ;
- while ( i < s .len && is_space ( s .str[ i ]/*rbyte 0*/ ) ) {
- i ++ ;
- }
- ;
- string res= string_right( s , i ) ;
- int end= res .len - 1 ;
- while ( end >= 0 && is_space ( res .str[ end ]/*rbyte 1*/ ) ) {
- end -- ;
- }
- ;
- res = string_left( res , end + 1 ) ;
- return res ;
- }
- string string_trim(string s, byte c) {
- if (string_eq( s , tos2("") )/*8*/ ) {
- /*if*/
- return tos2("") ;
- }
- ;
- int i= 0 ;
- while ( i < s .len && c == s .str[ i ]/*rbyte 0*/ ) {
- i ++ ;
- }
- ;
- string res= string_right( s , i ) ;
- int end= res .len - 1 ;
- while ( end >= 0 && c == res .str[ end ]/*rbyte 1*/ ) {
- end -- ;
- }
- ;
- res = string_left( res , end + 1 ) ;
- return res ;
- }
- string string_trim_left(string s, string cutset) {
- int start= string_index( s , cutset ) ;
- if ( start != 0 ) {
- /*if*/
- return s ;
- }
- ;
- while ( start < s .len - 1 && s .str[ start ]/*rbyte 0*/ == cutset .str[ 0 ]/*rbyte 0*/ ) {
- start ++ ;
- }
- ;
- return string_right( s , start ) ;
- }
- string string_trim_right(string s, string cutset) {
- return s ;
- int pos= string_last_index( s , cutset ) ;
- if ( pos == - 1 ) {
- /*if*/
- return s ;
- }
- ;
- return string_left( s , pos ) ;
- }
- int compare_strings(string* a, string* b) {
- if ( string_le(* a ,* b ) ) {
- /*if*/
- return - 1 ;
- }
- ;
- if ( string_ge(* a ,* b ) ) {
- /*if*/
- return 1 ;
- }
- ;
- return 0 ;
- }
- int compare_strings_by_len(string* a, string* b) {
- if ( a ->len < b ->len ) {
- /*if*/
- return - 1 ;
- }
- ;
- if ( a ->len > b ->len ) {
- /*if*/
- return 1 ;
- }
- ;
- return 0 ;
- }
- int compare_lower_strings(string* a, string* b) {
- string aa= string_to_lower(* a ) ;
- string bb= string_to_lower(* a ) ;
- return compare_strings (& /*11 EXP:"string*" GOT:"string" */ aa ,& /*11 EXP:"string*" GOT:"string" */ bb ) ;
- }
- void array_string_sort(array_string* s) {
- array_sort_with_compare( s , compare_strings ) ;
- }
- void array_string_sort_ignore_case(array_string* s) {
- array_sort_with_compare( s , compare_lower_strings ) ;
- }
- void array_string_sort_by_len(array_string* s) {
- array_sort_with_compare( s , compare_strings_by_len ) ;
- }
- ustring string_ustring(string s) {
- ustring res= /* S INIT */ (ustring){ .s = s , .runes = new_array ( 0 , s .len , sizeof( int) ) , .len = 0 } ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- int char_len= 0 ;
- char_len =UTF8_CHAR_LEN(s.str[i]);
- _PUSH(& res .runes , ( i ), tmp95, int) ;
- i += char_len - 1 ;
- res .len ++ ;
- }
- ;
- return res ;
- }
- array_int g_ustring_runes;
- ustring string_ustring_tmp(string s) {
- ustring res= /* S INIT */ (ustring){ .s = s , .runes = 0 , .len = 0 } ;
- res.runes = g_ustring_runes ;
- res.runes.len = s.len ;
- int j= 0 ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- int char_len= 0 ;
- char_len =UTF8_CHAR_LEN(s.str[i]);
- int tmp100 = i;
- array_set(&/*q*/ res .runes , j , & tmp100) ;
- j ++ ;
- i += char_len - 1 ;
- res .len ++ ;
- }
- ;
- return res ;
- }
- string ustring_substr(ustring u, int start, int end) {
- start = ( *(int*) array__get( u .runes , start) ) ;
- if ( end >= u .runes .len ) {
- /*if*/
- end = u .s .len ;
- }
- else {
- /*else if*/
- end = ( *(int*) array__get( u .runes , end) ) ;
- }
- ;
- return string_substr( u .s , start , end ) ;
- }
- string ustring_left(ustring u, int pos) {
- return ustring_substr( u , 0 , pos ) ;
- }
- string ustring_right(ustring u, int pos) {
- return ustring_substr( u , pos , u .len ) ;
- }
- byte string_at(string s, int idx) {
- if ( idx < 0 || idx >= s .len ) {
- /*if*/
- v_panic ( _STR("string index out of range: %d / %d", idx, s .len) ) ;
- }
- ;
- return s .str [/*ptr*/ idx ]/*rbyte 0*/ ;
- }
- string ustring_at(ustring u, int idx) {
- return ustring_substr( u , idx , idx + 1 ) ;
- }
- void v_ustring_free(ustring u) {
- v_array_int_free( u .runes ) ;
- }
- int abs(int a) {
- if ( a >= 0 ) {
- /*if*/
- return a ;
- }
- ;
- return - a ;
- }
- bool byte_is_digit(byte c) {
- return c >= '0' && c <= '9' ;
- }
- bool byte_is_letter(byte c) {
- return (/*lpar*/ c >= 'a' && c <= 'z' ) || (/*lpar*/ c >= 'A' && c <= 'Z' ) ;
- }
- void v_string_free(string s) {
- free ( s .str ) ;
- }
- void v_array_string_free(array_string arr) {
- array_string tmp105 = arr;
- ;
- for (int tmp106 = 0; tmp106 < tmp105 .len; tmp106 ++) {
- string s = ((string *) tmp105.data)[tmp106];
- v_string_free( s ) ;
- }
- ;
- free ( arr .data ) ;
- }
- string string_all_before(string s, string dot) {
- int pos= string_index( s , dot ) ;
- if ( pos == - 1 ) {
- /*if*/
- return s ;
- }
- ;
- return string_left( s , pos ) ;
- }
- string string_all_before_last(string s, string dot) {
- int pos= string_last_index( s , dot ) ;
- if ( pos == - 1 ) {
- /*if*/
- return s ;
- }
- ;
- return string_left( s , pos ) ;
- }
- string string_all_after(string s, string dot) {
- int pos= string_last_index( s , dot ) ;
- if ( pos == - 1 ) {
- /*if*/
- return s ;
- }
- ;
- return string_right( s , pos + dot .len ) ;
- }
- string array_string_join(array_string a, string del) {
- if ( a .len == 0 ) {
- /*if*/
- return tos2("") ;
- }
- ;
- int len= 0 ;
- array_string tmp111 = a ;
- ;
- for (int i = 0; i < tmp111 .len; i ++) {
- string val = ((string *) tmp111 . data)[i];
- len += val .len + del .len ;
- }
- ;
- len -= del .len ;
- string res= tos2("") ;
- res .len = len ;
- res .str = v_malloc ( res .len + 1 ) ;
- int idx= 0 ;
- array_string tmp114 = a ;
- ;
- for (int i = 0; i < tmp114 .len; i ++) {
- string val = ((string *) tmp114 . data)[i];
- for (
- int j= 0 ; j < val .len ; j ++ ) {
- byte c= val .str[ j ]/*rbyte 0*/ ;
- res .str [/*ptr*/ idx ]/*rbyte 1*/ = val .str [/*ptr*/ j ]/*rbyte 0*/ ;
- idx ++ ;
- }
- ;
- if ( i != a .len - 1 ) {
- /*if*/
- for (
- int k= 0 ; k < del .len ; k ++ ) {
- res .str [/*ptr*/ idx ]/*rbyte 1*/ = del .str [/*ptr*/ k ]/*rbyte 0*/ ;
- idx ++ ;
- }
- ;
- }
- ;
- }
- ;
- res .str [/*ptr*/ res .len ]/*rbyte 1*/ = '\0' ;
- return res ;
- }
- string array_string_join_lines(array_string s) {
- return array_string_join( s , tos2("\n") ) ;
- }
- string string_limit(string s, int max) {
- ustring u= string_ustring( s ) ;
- if ( u .len <= max ) {
- /*if*/
- return s ;
- }
- ;
- return ustring_substr( u , 0 , max ) ;
- }
- bool byte_is_white(byte c) {
- int i= (/*casttt*/ (int)( /*77*/ c ) ) ;
- return i == 10 || i == 32 || i == 9 || i == 13 || c == '\r' ;
- }
- string repeat_char(byte c, int n) {
- if ( n <= 0 ) {
- /*if*/
- return tos2("") ;
- }
- ;
- byte* arr= v_malloc ( n + 1 ) ;
- for (
- int i= 0 ; i < n ; i ++ ) {
- arr [/*ptr*/ i ]/*rbyte 1*/ = c ;
- }
- ;
- arr [/*ptr*/ n ]/*rbyte 1*/ = '\0' ;
- return tos ( arr , n ) ;
- }
- int string_hash(string s) {
- int hash= (/*casttt*/ (int)( /*77*/ 0 ) ) ;
- for (
- int i= 0 ; i < s .len ; i ++ ) {
- hash = hash * (/*casttt*/ (int)( /*77*/ 31 ) ) + (/*casttt*/ (int)( /*77*/ s .str [/*ptr*/ i ]/*rbyte 0*/ ) ) ;
- }
- ;
- return hash ;
- }
- void v_exit(string reason) {
- if (string_eq( reason , tos2("") )/*8*/ ) {
- /*if*/
- v_panic ( tos2("exit empty reason") ) ;
- }
- ;
- println2 ( _STR("exit(): %.*s", reason.len, reason.str) ) ;
- exit ( 0 ) ;
- }
- bool isnil(void* v) {
- return v == 0 ;
- }
- void on_panic(int (*f)( int /*FFF*/ )) {
- }
- void print_backtrace() {
- return ;
- #ifdef mac
- voidptr buffer [100 ]= {} /* arkek init*/ ;
- void* nr_ptrs= backtrace ( buffer , 100 ) ;
- backtrace_symbols_fd ( buffer , nr_ptrs , 1 ) ;
- #endif
- ;
- }
- void v_panic(string s) {
- println2 ( _STR("V panic: %.*s", s.len, s.str) ) ;
- print_backtrace ( ) ;
- exit ( 1 ) ;
- }
- void println(string s) {
- if ( isnil ( s .str ) ) {
- /*if*/
- v_panic ( tos2("println(NIL)") ) ;
- }
- ;
- printf ( "%.*s\n" , s .len , s .str ) ;
- }
- void eprintln(string s) {
- if ( isnil ( s .str ) ) {
- /*if*/
- v_panic ( tos2("eprintln(NIL)") ) ;
- }
- ;
- #ifdef mac
- fprintf ( stderr , "%.*s\n" , s .len , s .str ) ;
- ;
- #else
- println2 ( s ) ;
- #endif
- ;
- }
- void v_print(string s) {
- printf ( "%.*s" , s .len , s .str ) ;
- }
- void println2(string s) {
- printf ( "%.*s\n" , s .len , s .str ) ;
- }
- byte* v_malloc(int n) {
- if ( n < 0 ) {
- /*if*/
- v_panic ( tos2("malloc(<0)") ) ;
- }
- ;
- #ifdef VPLAY
- if ( n > 10000 ) {
- /*if*/
- v_panic ( tos2("allocating more than 10 KB is not allowed in the playground") ) ;
- }
- ;
- #endif
- #ifdef DEBUG_ALLOC
- i64 total= (/*casttt*/ (i64)( /*77*/ 0 ) ) ;
- total_m += n;
- total = total_m;
- println2 ( _STR("\n\n\nmalloc(%d) total=%lld", n, total) ) ;
- print_backtrace ( ) ;
- #endif
- byte* ptr= malloc ( n ) ;
- if ( isnil ( ptr ) ) {
- /*if*/
- v_panic ( _STR("malloc(%d) failed", n) ) ;
- }
- ;
- return ptr ;
- }
- byte* v_calloc(int n) {
- if ( n < 0 ) {
- /*if*/
- v_panic ( tos2("calloc(<0)") ) ;
- }
- ;
- return calloc ( sizeof( float) * n , sizeof( float) ) ;
- }
- int _strlen(byte* s) {
- return strlen ( s ) ;
- }
- Option opt_ok(void* data) {
- return /* S INIT */ (Option){ .data = data , .ok = 1 , .error = tos("", 0) , } ;
- }
- void* memdup(void* src, int sz) {
- byte* mem= v_malloc ( sz ) ;
- return memcpy ( mem , src , sz ) ;
- }
- Option v_error(string s) {
- return /* S INIT */ (Option){ .error = s , .data = 0 , .ok = 0 } ;
- }
- array_int range_int(int start, int end) {
- int len= end - start ;
- /* arr init tmp*/ int tmp7 = 0;
- array_int res= array_repeat(&tmp7, len , sizeof(int) ) ;
- for (
- int i= 0 ; i < len ; i ++ ) {
- int tmp10 = start + i;
- array_set(&/*q*/ res , i , & tmp10) ;
- }
- ;
- return res ;
- }
- string double_str(double d) {
- byte* buf= v_malloc ( sizeof( double) * 5 + 1 ) ;
- sprintf ( buf , "%f" , d ) ;
- return tos ( buf , _strlen ( buf ) ) ;
- }
- string float_str(float d) {
- byte* buf= v_malloc ( sizeof( double) * 5 + 1 ) ;
- sprintf ( buf , "%f" , d ) ;
- return tos ( buf , _strlen ( buf ) ) ;
- }
- string f64_str(f64 d) {
- byte* buf= v_malloc ( sizeof( double) * 5 + 1 ) ;
- sprintf ( buf , "%f" , d ) ;
- return tos ( buf , _strlen ( buf ) ) ;
- }
- string f32_str(f32 d) {
- byte* buf= v_malloc ( sizeof( double) * 5 + 1 ) ;
- sprintf ( buf , "%f" , d ) ;
- return tos ( buf , _strlen ( buf ) ) ;
- }
- string ptr_str(void* ptr) {
- byte* buf= v_malloc ( sizeof( double) * 5 + 1 ) ;
- sprintf ( buf , "%p" , ptr ) ;
- return tos ( buf , _strlen ( buf ) ) ;
- }
- string int_str(int nn) {
- int n= nn ;
- if ( n == 0 ) {
- /*if*/
- return tos2("0") ;
- }
- ;
- int max= 16 ;
- byte* buf= v_malloc ( max ) ;
- int len= 0 ;
- bool is_neg= 0 ;
- if ( n < 0 ) {
- /*if*/
- n = - n ;
- is_neg = 1 ;
- }
- ;
- while ( n > 0 ) {
- int d= n % 10 ;
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = d + (/*casttt*/ (int)( /*77*/ '0' ) ) ;
- len ++ ;
- n = n / 10 ;
- }
- ;
- if ( is_neg ) {
- /*if*/
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = '-' ;
- len ++ ;
- }
- ;
- return tos ( buf + max - len , len ) ;
- }
- string u8_str(u8 nn) {
- u8 n= nn ;
- if ( n == (/*casttt*/ (u8)( /*77*/ 0 ) ) ) {
- /*if*/
- return tos2("0") ;
- }
- ;
- int max= 5 ;
- byte* buf= v_malloc ( max ) ;
- int len= 0 ;
- bool is_neg= 0 ;
- if ( n < (/*casttt*/ (u8)( /*77*/ 0 ) ) ) {
- /*if*/
- n = - n ;
- is_neg = 1 ;
- }
- ;
- while ( n > (/*casttt*/ (u8)( /*77*/ 0 ) ) ) {
- u8 d= n % (/*casttt*/ (u8)( /*77*/ 10 ) ) ;
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = d + (/*casttt*/ (u8)( /*77*/ '0' ) ) ;
- len ++ ;
- n = n / (/*casttt*/ (u8)( /*77*/ 10 ) ) ;
- }
- ;
- if ( is_neg ) {
- /*if*/
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = '-' ;
- len ++ ;
- }
- ;
- return tos ( buf + max - len , len ) ;
- }
- string i64_str(i64 nn) {
- i64 n= nn ;
- if ( n == (/*casttt*/ (i64)( /*77*/ 0 ) ) ) {
- /*if*/
- return tos2("0") ;
- }
- ;
- int max= 32 ;
- byte* buf= v_malloc ( max ) ;
- int len= 0 ;
- bool is_neg= 0 ;
- if ( n < (/*casttt*/ (i64)( /*77*/ 0 ) ) ) {
- /*if*/
- n = - n ;
- is_neg = 1 ;
- }
- ;
- while ( n > (/*casttt*/ (i64)( /*77*/ 0 ) ) ) {
- int d= (/*casttt*/ (int)( /*77*/ n % (/*casttt*/ (i64)( /*77*/ 10 ) ) ) ) ;
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = d + (/*casttt*/ (int)( /*77*/ '0' ) ) ;
- len ++ ;
- n = n / (/*casttt*/ (i64)( /*77*/ 10 ) ) ;
- }
- ;
- if ( is_neg ) {
- /*if*/
- buf [/*ptr*/ max - len - 1 ]/*rbyte 1*/ = '-' ;
- len ++ ;
- }
- ;
- return tos ( buf + max - len , len ) ;
- }
- string bool_str(bool b) {
- if ( b ) {
- /*if*/
- return tos2("true") ;
- }
- ;
- return tos2("false") ;
- }
- string int_hex(int n) {
- string s= int_str( n ) ;
- byte* hex= v_malloc ( s .len + 2 ) ;
- sprintf ( hex , "0x%x" , n ) ;
- return tos ( hex , s .len + 2 ) ;
- }
- string i64_hex(i64 n) {
- string s= i64_str( n ) ;
- byte* hex= v_malloc ( s .len + 2 ) ;
- sprintf ( hex , "0x%x" , n ) ;
- return tos ( hex , s .len + 2 ) ;
- }
- bool array_byte_contains(array_byte a, byte val) {
- array_byte tmp28 = a;
- ;
- for (int tmp29 = 0; tmp29 < tmp28 .len; tmp29 ++) {
- byte aa = ((byte *) tmp28.data)[tmp29];
- if ( aa == val ) {
- /*if*/
- return 1 ;
- }
- ;
- }
- ;
- return 0 ;
- }
- string byte_str(byte c) {
- string str= /* S INIT */ (string){ .len = 1 , .str = v_malloc ( 2 ) } ;
- str .str [/*ptr*/ 0 ]/*rbyte 1*/ = c ;
- str .str [/*ptr*/ 1 ]/*rbyte 1*/ = '\0' ;
- return str ;
- }
- int string_is_utf8(string s) {
- int faulty_bytes= 0 ;
- int len= s .len ;
- int i= 0 ;
- byte * str = s.str;
- while (i < len) {
- if (str[i] <= 0x7F) /* 00..7F */ {
- i += 1;
- }
- else if (str[i] >= 0xC2 && str[i] <= 0xDF) /* C2..DF 80..BF */ {
- if (i + 1 < len) /* Expect a 2nd byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
- printf( "After a first byte between C2 and DF, expecting a 2nd byte between 80 and BF");
- faulty_bytes = 2;
- goto end;
- }
- }
- else {
- printf( "After a first byte between C2 and DF, expecting a 2nd byte.");
- faulty_bytes = 1;
- goto end;
- }
- i += 2;
- }
- else if (str[i] == 0xE0) /* E0 A0..BF 80..BF */ {
- if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
- if (str[i + 1] < 0xA0 || str[i + 1] > 0xBF) {
- printf( "After a first byte of E0, expecting a 2nd byte between A0 and BF.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte of E0, expecting a 3nd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- }
- else {
- printf( "After a first byte of E0, expecting two following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 3;
- }
- else if (str[i] >= 0xE1 && str[i] <= 0xEC) /* E1..EC 80..BF 80..BF */ {
- if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
- printf( "After a first byte between E1 and EC, expecting the 2nd byte between 80 and BF.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte between E1 and EC, expecting the 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- }
- else {
- printf( "After a first byte between E1 and EC, expecting two following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 3;
- }
- else if (str[i] == 0xED) /* ED 80..9F 80..BF */ {
- if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0x9F) {
- printf( "After a first byte of ED, expecting 2nd byte between 80 and 9F.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte of ED, expecting 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- }
- else {
- printf( "After a first byte of ED, expecting two following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 3;
- }
- else if (str[i] >= 0xEE && str[i] <= 0xEF) /* EE..EF 80..BF 80..BF */ {
- if (i + 2 < len) /* Expect a 2nd and 3rd byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
- printf( "After a first byte between EE and EF, expecting 2nd byte between 80 and BF.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte between EE and EF, expecting 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- }
- else {
- printf( "After a first byte between EE and EF, two following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 3;
- }
- else if (str[i] == 0xF0) /* F0 90..BF 80..BF 80..BF */ {
- if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
- if (str[i + 1] < 0x90 || str[i + 1] > 0xBF) {
- printf( "After a first byte of F0, expecting 2nd byte between 90 and BF.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte of F0, expecting 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
- printf( "After a first byte of F0, expecting 4th byte between 80 and BF.");
- faulty_bytes = 4;
- goto end;
- }
- }
- else {
- printf( "After a first byte of F0, expecting three following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 4;
- }
- else if (str[i] >= 0xF1 && str[i] <= 0xF3) /* F1..F3 80..BF 80..BF 80..BF */ {
- if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0xBF) {
- printf( "After a first byte of F1, F2, or F3, expecting a 2nd byte between 80 and BF.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte of F1, F2, or F3, expecting a 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
- printf( "After a first byte of F1, F2, or F3, expecting a 4th byte between 80 and BF.");
- faulty_bytes = 4;
- goto end;
- }
- }
- else {
- printf( "After a first byte of F1, F2, or F3, expecting three following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 4;
- }
- else if (str[i] == 0xF4) /* F4 80..8F 80..BF 80..BF */ {
- if (i + 3 < len) /* Expect a 2nd, 3rd 3th byte */ {
- if (str[i + 1] < 0x80 || str[i + 1] > 0x8F) {
- printf( "After a first byte of F4, expecting 2nd byte between 80 and 8F.");
- faulty_bytes = 2;
- goto end;
- }
- if (str[i + 2] < 0x80 || str[i + 2] > 0xBF) {
- printf( "After a first byte of F4, expecting 3rd byte between 80 and BF.");
- faulty_bytes = 3;
- goto end;
- }
- if (str[i + 3] < 0x80 || str[i + 3] > 0xBF) {
- printf( "After a first byte of F4, expecting 4th byte between 80 and BF.");
- faulty_bytes = 4;
- goto end;
- }
- }
- else {
- printf( "After a first byte of F4, expecting three following bytes.");
- faulty_bytes = 1;
- goto end;
- }
- i += 4;
- }
- else {
- printf( "i=%d Expecting bytes in the following ranges: 00..7F C2..F4.",
- i);
- faulty_bytes = 1;
- goto end;
- }
- }
- end: ;
- bool ok= faulty_bytes == 0 ;
- if ( ok ) {
- /*if*/
- return - 1 ;
- }
- ;
- if ( ! ok ) {
- /*if*/
- println2 ( _STR("utf is bad dalen=%d KEK %.*s sdf", len, s.len, s.str) ) ;
- }
- ;
- return i ;
- }
- string utf32_to_str(u32 code) {
- byte* buffer= v_malloc ( 5 ) ;
- if (code <= 0x7F) {
- buffer[0] = code;
- return tos(buffer, 1);
- }
- if (code <= 0x7FF) {
- buffer[0] = 0xC0 | (code >> 6); /* 110xxxxx */
- buffer[1] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 2);
- }
- if (code <= 0xFFFF) {
- buffer[0] = 0xE0 | (code >> 12); /* 1110xxxx */
- buffer[1] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
- buffer[2] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 3);
- }
- if (code <= 0x10FFFF) {
- buffer[0] = 0xF0 | (code >> 18); /* 11110xxx */
- buffer[1] = 0x80 | ((code >> 12) & 0x3F); /* 10xxxxxx */
- buffer[2] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
- buffer[3] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 4);
- }
- return tos2("") ;
- }
- string utf32_to_str_no_malloc(u32 code, void* buf) {
- char* buffer = buf;
- if (code <= 0x7F) {
- buffer[0] = code;
- return tos(buffer, 1);
- }
- if (code <= 0x7FF) {
- buffer[0] = 0xC0 | (code >> 6); /* 110xxxxx */
- buffer[1] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 2);
- }
- if (code <= 0xFFFF) {
- buffer[0] = 0xE0 | (code >> 12); /* 1110xxxx */
- buffer[1] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
- buffer[2] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 3);
- }
- if (code <= 0x10FFFF) {
- buffer[0] = 0xF0 | (code >> 18); /* 11110xxx */
- buffer[1] = 0x80 | ((code >> 12) & 0x3F); /* 10xxxxxx */
- buffer[2] = 0x80 | ((code >> 6) & 0x3F); /* 10xxxxxx */
- buffer[3] = 0x80 | (code & 0x3F); /* 10xxxxxx */
- return tos(buffer, 4);
- }
- return tos2("") ;
- }
- int string_utf32_code(string _rune) {
- if ( _rune .len == 0 ) {
- /*if*/
- return 0 ;
- }
- ;
- if ( _rune .len == 1 ) {
- /*if*/
- return (/*casttt*/ (int)( /*77*/ _rune .str[ 0 ]/*rbyte 0*/ ) ) ;
- }
- ;
- byte b= (/*casttt*/ (byte)( /*77*/ (/*casttt*/ (int)( /*77*/ _rune .str[ 0 ]/*rbyte 0*/ ) ) ) ) ;
- b <<= _rune.len;
- int res= (/*casttt*/ (int)( /*77*/ b ) ) ;
- int shift= 6 - _rune .len ;
- for (
- int i= 1 ; i < _rune .len ; i ++ ) {
- int c= (/*casttt*/ (int)( /*77*/ _rune .str[ i ]/*rbyte 0*/ ) ) ;
- res <<= shift;
- res |= c & 0x3f;
- shift = 6 ;
- }
- ;
- return res ;
- }
- map new_map(int cap, int elm_size) {
- map res= /* S INIT */ (map){ .element_size = elm_size , .entries = new_array(0, 1, sizeof(Entry)) , .is_sorted = 0 } ;
- return res ;
- }
- Entry map_new_entry(map* m, string key, void* val) {
- Entry new_e= /* S INIT */ (Entry){ .key = key , .val = v_malloc ( m ->element_size ) } ;
- memcpy ( new_e .val , val , m ->element_size ) ;
- return new_e ;
- }
- void map__set(map* m, string key, void* val) {
- Entry e= map_new_entry(& /* ? */* m , key , val ) ;
- for (
- int i= 0 ; i < m ->entries .len ; i ++ ) {
- Entry entry= ( *(Entry*) array__get( m ->entries , i) ) ;
- if (string_eq( entry .key , key )/*8*/ ) {
- /*if*/
- Entry tmp8 = e;
- array_set(&/*q*/ m ->entries , i , & tmp8) ;
- return ;
- }
- ;
- }
- ;
- _PUSH(& m ->entries , ( e ), tmp9, Entry) ;
- m ->is_sorted = 0 ;
- }
- int volt_abs(int n) {
- if ( n < 0 ) {
- /*if*/
- return - n ;
- }
- ;
- return n ;
- }
- void map_bs(map m, string query, int start, int end, void* out) {
- int mid= start + (/*lpar*/ (/*lpar*/ end - start ) / 2 ) ;
- if ( end - start == 0 ) {
- /*if*/
- Entry last= ( *(Entry*) array__get( m .entries , end) ) ;
- memcpy ( out , last .val , m .element_size ) ;
- return ;
- }
- ;
- if ( end - start == 1 ) {
- /*if*/
- Entry first= ( *(Entry*) array__get( m .entries , start) ) ;
- memcpy ( out , first .val , m .element_size ) ;
- return ;
- }
- ;
- if ( mid >= m .entries .len ) {
- /*if*/
- return ;
- }
- ;
- Entry mid_msg= ( *(Entry*) array__get( m .entries , mid) ) ;
- if (string_lt( query , mid_msg .key )/*8*/ ) {
- /*if*/
- map_bs( m , query , start , mid , out ) ;
- return ;
- }
- ;
- map_bs( m , query , mid , end , out ) ;
- }
- int compare_map(Entry* a, Entry* b) {
- if (string_lt( a ->key , b ->key )/*8*/ ) {
- /*if*/
- return - 1 ;
- }
- ;
- if (string_gt( a ->key , b ->key )/*8*/ ) {
- /*if*/
- return 1 ;
- }
- ;
- return 0 ;
- }
- void map_sort(map* m) {
- array_sort_with_compare(& /* ? */ m ->entries , compare_map ) ;
- m ->is_sorted = 1 ;
- }
- bool map_get(map m, string key, void* out) {
- if ( m .is_sorted ) {
- /*if*/
- map_bs( m , key , 0 , m .entries .len , out ) ;
- return 1 ;
- }
- ;
- for (
- int i= 0 ; i < m .entries .len ; i ++ ) {
- Entry entry= ( *(Entry*) array__get( m .entries , i) ) ;
- if (string_eq( entry .key , key )/*8*/ ) {
- /*if*/
- memcpy ( out , entry .val , m .element_size ) ;
- return 1 ;
- }
- ;
- }
- ;
- return 0 ;
- }
- void v_map_print(map m) {
- println ( tos2("<<<<<<<<") ) ;
- for (
- int i= 0 ; i < m .entries .len ; i ++ ) {
- }
- ;
- println ( tos2(">>>>>>>>>>") ) ;
- }
- void v_map_free(map m) {
- }
- string map_string_str(map_string m) {
- if ( m .entries .len == 0 ) {
- /*if*/
- return tos2("{}") ;
- }
- ;
- string s= tos2("{\n") ;
- array_Entry tmp26 = m .entries;
- ;
- for (int tmp27 = 0; tmp27 < tmp26 .len; tmp27 ++) {
- Entry entry = ((Entry *) tmp26.data)[tmp27];
- string tmp28 = tos("", 0); bool tmp29 = map_get( m , entry .key, & tmp28);
- if (!tmp29) tmp28 = tos("", 0);
- string val= tmp28 ;
- s = string_add(s, _STR(" \"%.*s\" => \"%.*s\"\n", entry .key.len, entry .key.str, val.len, val.str) ) ;
- }
- ;
- s = string_add(s, tos2("}") ) ;
- return s ;
- }
- smap new_smap() {
- smap res= /* S INIT */ (smap){ .entries = new_array(0, 1, sizeof(Entry2)) , .is_sorted = 0 } ;
- return res ;
- }
- void smap_set(smap* m, string key, string val) {
- Entry2 e= /* S INIT */ (Entry2){ .key = key , .val = val } ;
- _PUSH(& m ->entries , ( e ), tmp3, Entry2) ;
- }
- string smap_get(smap m, string key) {
- if ( m .is_sorted ) {
- /*if*/
- return smap_bs( m , key , 0 , m .entries .len ) ;
- }
- ;
- for (
- int i= 0 ; i < m .entries .len ; i ++ ) {
- Entry2 entry= ( *(Entry2*) array__get( m .entries , i) ) ;
- if (string_eq( entry .key , key )/*8*/ ) {
- /*if*/
- return entry .val ;
- }
- ;
- }
- ;
- return tos2("") ;
- }
- string smap_bs(smap m, string query, int start, int end) {
- int mid= start + (/*lpar*/ (/*lpar*/ end - start ) / 2 ) ;
- if ( end - start == 0 ) {
- /*if*/
- Entry2 last= ( *(Entry2*) array__get( m .entries , end) ) ;
- return last .val ;
- }
- ;
- if ( end - start == 1 ) {
- /*if*/
- Entry2 first= ( *(Entry2*) array__get( m .entries , start) ) ;
- return first .val ;
- }
- ;
- if ( mid >= m .entries .len ) {
- /*if*/
- return tos2("") ;
- }
- ;
- Entry2 mid_msg= ( *(Entry2*) array__get( m .entries , mid) ) ;
- if (string_lt( query , mid_msg .key )/*8*/ ) {
- /*if*/
- return smap_bs( m , query , start , mid ) ;
- }
- ;
- return smap_bs( m , query , mid , end ) ;
- }
- int compare_smap(Entry2* a, Entry2* b) {
- if (string_lt( a ->key , b ->key )/*8*/ ) {
- /*if*/
- return - 1 ;
- }
- ;
- if (string_gt( a ->key , b ->key )/*8*/ ) {
- /*if*/
- return 1 ;
- }
- ;
- return 0 ;
- }
- void smap_sort(smap* m) {
- array_sort_with_compare(& /* ? */ m ->entries , compare_smap ) ;
- m ->is_sorted = 1 ;
- }
- void v_smap_free(smap m) {
- }
- string smap_str(smap m) {
- if ( m .entries .len == 0 ) {
- /*if*/
- return tos2("{}") ;
- }
- ;
- string s= tos2("{\n") ;
- array_Entry2 tmp19 = m .entries;
- ;
- for (int tmp20 = 0; tmp20 < tmp19 .len; tmp20 ++) {
- Entry2 entry = ((Entry2 *) tmp19.data)[tmp20];
- s = string_add(s, _STR(" \"%.*s\" => \"%.*s\"\n", entry .key.len, entry .key.str, entry .val.len, entry .val.str) ) ;
- }
- ;
- s = string_add(s, tos2("}") ) ;
- return s ;
- }
- StringBuilder new_string_builder(int initial_size) {
- return /* S INIT */ (StringBuilder){ .buf = new_array ( 0 , initial_size , sizeof( byte) ) , .len = 0 } ;
- }
- void StringBuilder_write(StringBuilder* b, string s) {
- array__push_many(& /* ? */ b ->buf , s .str , s .len ) ;
- b ->len += s .len ;
- }
- void StringBuilder_writeln(StringBuilder* b, string s) {
- array__push_many(& /* ? */ b ->buf , s .str , s .len ) ;
- _PUSH(& b ->buf , ( '\n' ), tmp1, byte) ;
- b ->len += s .len + 1 ;
- }
- string StringBuilder_str(StringBuilder b) {
- return tos ( b .buf .data , b .len ) ;
- }
- void StringBuilder_cut(StringBuilder b, int n) {
- b .len -= n ;
- }
- array_string os__init_os_args(int argc, void* c) {
- array_string args=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- char** argv = (char**) c;
- for (
- int i= 0 ; i < argc ; i ++ ) {
- string arg= tos2("") ;
- arg = tos(argv[i], strlen(argv[i]));
- _PUSH(& args , ( arg ), tmp4, string) ;
- }
- ;
- os__args = args;
- return args ;
- }
- void os__parse_windows_cmd_line(byte* cmd) {
- string s= tos2 ( cmd ) ;
- array_string vals= string_split( s , tos2(" ") ) ;
- println (array_string_str( vals ) ) ;
- os__args = vals;
- }
- string os__read_file(string path) {
- string res= tos2("") ;
- FILE *f = fopen(path.str, "r");
- if (!f) return tos("", 0);
- fseek(f, 0, SEEK_END);
- long fsize = ftell(f);
- rewind(f);
- char *string = malloc(fsize + 1);
- fread(string, fsize, 1, f);
- fclose(f);
- string[fsize] = 0;
- res = tos(string, fsize);
- return res ;
- }
- string os__File_read_rune(os__File f) {
- if (!f.cfile) return tos("", 0);
- byte* c= v_malloc ( 1 ) ;
- fread ( c , 1 , 1 , f .cfile ) ;
- return tos ( c , 1 ) ;
- }
- int os__file_size(string path) {
- struct stat s;
- stat(path.str, &s);
- return s.st_size;
- FILE *f = fopen(path.str, "r");
- if (!f) return 0;
- fseek(f, 0, SEEK_END);
- long fsize = ftell(f);
- rewind(f);
- return fsize;
- return 0 ;
- }
- int os__file_last_mod_unix(string path) {
- struct stat attr;
- stat(path.str, &attr);
- return attr.st_mtime ;
- return 0 ;
- }
- array_string os__read_lines(string path) {
- return os__read_file_lines ( path ) ;
- }
- array_string os__read_file_into_lines(string path) {
- return os__read_file_lines ( path ) ;
- }
- array_ustring os__read_file_into_ulines(string path) {
- array_string lines= os__read_file_into_lines ( path ) ;
- array_ustring ulines=/*0*/ new_array_from_c_array(0, 0, sizeof(ustring), (ustring[]) { }) ;
- array_string tmp11 = lines;
- ;
- for (int tmp12 = 0; tmp12 < tmp11 .len; tmp12 ++) {
- string myline = ((string *) tmp11.data)[tmp12];
- _PUSH(& ulines , ( string_ustring( myline ) ), tmp13, ustring) ;
- }
- ;
- return ulines ;
- }
- array_string os__read_file_lines(string path) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- char buf[os__BUF_SIZE];
- FILE *fp = fopen(path.str, "rb");
- if (!fp)
- {
- println ( _STR("failed to open file \"%.*s\"", path.len, path.str) ) ;
- return res ;
- }
- while (fgets(buf, os__BUF_SIZE, fp) != NULL)
- {
- string val= tos2("") ;
- buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
- #ifdef windows
- if (buf[strlen(buf)-2] == 13)
- buf[strlen(buf) - 2] = '\0'; // eat the newline fgets() stores
- #endif
- val=tos_clone(buf) ;
- #ifdef windows
- if ( string_at( val , val .len - 1) == 13 ) {
- /*if*/
- }
- ;
- #endif
- _PUSH(& res , ( val ), tmp18, string) ;
- }
- fclose(fp);
- return res ;
- }
- void os__append_to_file(string file, string s) {
- FILE* fp = fopen(file.str, "a");
- fputs(s.str, fp);
- fputs("\n", fp);
- fclose(fp);
- }
- os__File os__open(string path) {
- return os__open_file ( path ) ;
- }
- os__File os__open_file(string file) {
- return os__create_file2 ( file , tos2("r") ) ;
- }
- os__File os__create(string path) {
- return os__create_file ( path ) ;
- }
- os__File os__open_append(string path) {
- return os__create_file ( path ) ;
- }
- os__File os__create_file(string file) {
- return os__create_file2 ( file , tos2("w") ) ;
- }
- os__File os__create_file_a(string file) {
- return os__create_file2 ( file , tos2("a") ) ;
- }
- os__File os__open_file_a(string file) {
- return os__create_file2 ( file , tos2("a") ) ;
- }
- os__File os__create_file2(string file, string mode) {
- os__File res= /* S INIT */ (os__File){ .cfile = fopen ( string_cstr( file ) , string_cstr( mode ) ) } ;
- if ( isnil ( res .cfile ) ) {
- /*if*/
- println ( _STR("coudlnt create file \"%.*s\"", file.len, file.str) ) ;
- }
- ;
- return res ;
- }
- void os__File_append(os__File f, string s) {
- string ss= string_clone( s ) ;
- fputs ( string_cstr( ss ) , f .cfile ) ;
- }
- void os__File_write(os__File f, void* data, int size) {
- fwrite ( data , 1 , size , f .cfile ) ;
- }
- void os__File_write_at(os__File f, void* data, int size, int pos) {
- fseek ( f .cfile , pos , SEEK_SET ) ;
- fwrite ( data , 1 , size , f .cfile ) ;
- fseek ( f .cfile , 0 , SEEK_END ) ;
- }
- void os__File_appendln(os__File f, string s) {
- fputs ( string_cstr( s ) , f .cfile ) ;
- fputs ( "\n" , f .cfile ) ;
- }
- void os__File_close(os__File f) {
- fclose ( f .cfile ) ;
- }
- void os__close_file(os__FILE* fp) {
- #ifdef windows
- #endif
- ;
- if (fp)
- fclose ( fp ) ;
- }
- int os__system2(string cmd) {
- string cstr= string_clone( cmd ) ;
- int ret= (/*casttt*/ (int)( /*77*/ system ( string_cstr( cstr ) ) ) ) ;
- if ( ret == - 1 ) {
- /*if*/
- os__print_c_errno ( ) ;
- }
- ;
- return ret ;
- }
- os__FILE* os__popen(string path) {
- byte* cpath= string_cstr( path ) ;
- #ifdef windows
- return _popen ( cpath , "r" ) ;
- ;
- #else
- return popen ( cpath , "r" ) ;
- #endif
- ;
- }
- string os__system(string cmd) {
- string res= tos2("") ;
- string ss= _STR("%.*s 2>&1", cmd.len, cmd.str) ;
- int _= 0 ;
- os__FILE* f= os__popen ( ss ) ;
- if ( isnil ( f ) ) {
- /*if*/
- println ( _STR("popen %.*s failed", cmd.len, cmd.str) ) ;
- }
- ;
- char buf[MAX];
- while (fgets(buf, MAX, f) != NULL) {
- res = string_add(res, tos(buf, strlen(buf)));
- }
- return string_trim_space( res ) ;
- }
- array_string os__system_into_lines(string s) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- string cmd= _STR("%.*s 2>&1", s.len, s.str) ;
- #ifdef windows
- FILE* f = _popen(cmd.str, "r");
- #else
- FILE* f = popen(cmd.str, "r");
- #endif
- char * buf = malloc(sizeof(char) * MAX);
- while (fgets(buf, MAX, f) != NULL)
- {
- string val= tos2("") ;
- buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
- val=tos_clone(buf);
- _PUSH(& res , ( val ), tmp31, string) ;
- }
- return res ;
- }
- string os__getenv(string key) {
- void* s= getenv ( string_cstr( key ) ) ;
- if ( isnil ( s ) ) {
- /*if*/
- return tos2("") ;
- }
- ;
- return tos2 ( s ) ;
- }
- void os__exit(string reason) {
- println2 ( _STR("exit(): %.*s", reason.len, reason.str) ) ;
- os__log ( reason ) ;
- exit ( 0 ) ;
- }
- void os__exit1(string reason) {
- println2 ( _STR("exit(): %.*s", reason.len, reason.str) ) ;
- exit ( 1 ) ;
- }
- bool os__file_exists(string path) {
- bool res= 0 ;
- #ifdef windows
- res = _access( path.str, 0 ) != -1 ;
- #else
- res = access( path.str, 0 ) != -1 ;
- #endif
- return res ;
- }
- void os__mkdir(string path) {
- #ifdef windows
- path = string_replace( path , tos2("/") , tos2("\\") ) ;
- CreateDirectory ( string_cstr( path ) , 0 ) ;
- ;
- #else
- println ( _STR("AAAAAAAA $$ \"%.*s\"", path.len, path.str) ) ;
- mkdir ( string_cstr( path ) , 511 ) ;
- #endif
- ;
- }
- void os__rm(string path) {
- #ifdef windows
- ;
- #else
- remove ( string_cstr( path ) ) ;
- #endif
- ;
- }
- void os__rmdir(string path, string guard) {
- if ( ! string_contains( path , guard ) ) {
- /*if*/
- println ( _STR("rmdir canceled because the path doesnt contain %.*s", guard.len, guard.str) ) ;
- return ;
- }
- ;
- println2 ( _STR("rmdir \"%.*s\"", path.len, path.str) ) ;
- #ifndef windows
- os__system ( _STR("rm -rf \"%.*s\"", path.len, path.str) ) ;
- #else
- os__system ( _STR("rmdir /s /q \"%.*s\"", path.len, path.str) ) ;
- #endif
- }
- void os__unzip(string path, string out) {
- #ifdef windows
- char *s="powershell.exe -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('PATH', 'OUT'); }\" ";
- string cmd= tos2("") ;
- cmd = tos(s, strlen(s));
- cmd = string_replace( cmd , tos2("PATH") , path ) ;
- cmd = string_replace( cmd , tos2("OUT") , out ) ;
- os__system ( cmd ) ;
- ;
- #else
- os__system ( _STR("unzip -o -d \"%.*s\" \"%.*s\"", out.len, out.str, path.len, path.str) ) ;
- #endif
- ;
- }
- void os__print_c_errno() {
- printf("errno=%d err='%s'\n", errno, strerror(errno));
- }
- string os__basedir(string path) {
- int pos= string_last_index( path , tos2("/") ) ;
- if ( pos == - 1 ) {
- /*if*/
- return path ;
- }
- ;
- return string_left( path , pos + 1 ) ;
- }
- string os__filename(string path) {
- return string_all_after( path , tos2("/") ) ;
- }
- string os__get_line() {
- int max= 256 ;
- byte* buf= v_malloc ( max ) ;
- int nr_chars= getline ( & /*vvar*/ buf , & /*vvar*/ max , stdin ) ;
- if ( nr_chars == 0 ) {
- /*if*/
- return tos2("") ;
- }
- ;
- return tos ( buf , nr_chars - 1 ) ;
- }
- string os__user_os() {
- #ifdef linux
- return tos2("linux") ;
- #endif
- ;
- #ifdef mac
- return tos2("mac") ;
- #endif
- ;
- #ifdef windows
- return tos2("windows") ;
- #endif
- ;
- return tos2("unknown") ;
- }
- string os__home_dir() {
- string home= os__getenv ( tos2("HOME") ) ;
- #ifdef windows
- home = os__getenv ( tos2("HOMEDRIVE") ) ;
- home = string_add(home, os__getenv ( tos2("HOMEPATH") ) ) ;
- #endif
- ;
- home = string_add(home, tos2("/") ) ;
- return home ;
- }
- void os__write_file(string path, string text) {
- os__File f= os__create ( path ) ;
- os__File_appendln( f , text ) ;
- os__File_close( f ) ;
- }
- void os__on_segfault(void* f) {
- #ifdef windows
- return ;
- #endif
- #ifdef mac
- struct sigaction sa;
- memset(&sa, 0, sizeof(struct sigaction));
- sigemptyset(&sa.sa_mask);
- sa.sa_sigaction = f;
- sa.sa_flags = SA_SIGINFO;
- sigaction(SIGSEGV, &sa, 0);
- #endif
- }
- void os__log(string s) {
- }
- bool os__is_dir(string path) {
- struct stat statbuf;
- byte* cstr= string_cstr( path ) ;
- if (stat(cstr, &statbuf) != 0)
- {
- return 0 ;
- }
- return S_ISDIR(statbuf.st_mode);
- return 0 ;
- }
- void os__chdir(string path) {
- chdir ( string_cstr( path ) ) ;
- }
- string os__getwd() {
- byte* cwd= v_malloc ( 1024 ) ;
- if (getcwd(cwd, 1024)) return tos2(cwd);
- return tos2("") ;
- }
- array_string os__ls(string path) {
- array_string res=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir (path.str)) == NULL)
- {
- println ( _STR("ls() couldnt open dir \"%.*s\"", path.len, path.str) ) ;
- os__print_c_errno ( ) ;
- return res ;
- }
- while ((ent = readdir (dir)) != NULL) {
- string name= tos2("") ;
- name = tos_clone(ent->d_name);//, strlen(ent->d_name));
- if (string_ne( name , tos2(".") )/*8*/ && string_ne( name , tos2("..") )/*8*/ && string_ne( name , tos2("") )/*8*/ ) {
- /*if*/
- _PUSH(& res , ( name ), tmp5, string) ;
- }
- ;
- }
- closedir (dir);
- return res ;
- }
- void os__print_backtrace() {
- void *buffer[100];
- int nptrs= 0 ;
- nptrs = backtrace(buffer, 100);
- printf("%d!!\n", nptrs);
- backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
- }
- int main(int argc, char** argv) {
- init_consts();
- os__init_os_args(argc, argv);
- string path= tos2("cinderella.txt") ;
- if ( os__args .len != 2 ) {
- /*if*/
- println ( tos2("usage: word_counter [text_file]") ) ;
- println ( _STR("using %.*s", path.len, path.str) ) ;
- }
- else {
- /*else if*/
- path = ( *(string*) array__get( os__args , 1) ) ;
- }
- ;
- array_string lines= os__read_file_lines ( string_trim_space( path ) ) ;
- map_int m= new_map(1, sizeof(int)) ;
- array_string tmp6 = lines;
- ;
- for (int tmp7 = 0; tmp7 < tmp6 .len; tmp7 ++) {
- string line = ((string *) tmp6.data)[tmp7];
- array_string words= string_split( string_to_lower( line ) , tos2(" ") ) ;
- array_string tmp9 = words;
- ;
- for (int tmp10 = 0; tmp10 < tmp9 .len; tmp10 ++) {
- string word = ((string *) tmp9.data)[tmp10];
- string key= filter_word ( word ) ;
- if (string_eq( key , tos2("") )/*8*/ ) {
- /*if*/
- continue
- ;
- }
- ;
- int tmp12 = 0; bool tmp13 = map_get( m , key, & tmp12);
- int tmp14 = tmp12 + 1;
- map__set(& m , key , & tmp14) ;
- }
- ;
- }
- ;
- array_string keys=/*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }) ;
- array_Entry tmp16 = m .entries;
- ;
- for (int tmp17 = 0; tmp17 < tmp16 .len; tmp17 ++) {
- Entry e = ((Entry *) tmp16.data)[tmp17];
- _PUSH(& keys , ( e .key ), tmp18, string) ;
- }
- ;
- array_string_sort(& /* ? */ keys ) ;
- array_string tmp19 = keys;
- ;
- for (int tmp20 = 0; tmp20 < tmp19 .len; tmp20 ++) {
- string key = ((string *) tmp19.data)[tmp20];
- int tmp21 = 0; bool tmp22 = map_get( m , key, & tmp21);
- int val= tmp21 ;
- println ( _STR("%.*s => %d", key.len, key.str, val) ) ;
- }
- ;
- }
- string filter_word(string word) {
- if (string_eq( word , tos2("") )/*8*/ || string_eq( word , tos2(" ") )/*8*/ ) {
- /*if*/
- return tos2("") ;
- }
- ;
- int i= 0 ;
- while ( i < word .len && ! is_letter ( string_at( word , i) ) ) {
- i ++ ;
- }
- ;
- int start= i ;
- while ( i < word .len && is_letter ( string_at( word , i) ) ) {
- i ++ ;
- }
- ;
- int end= i ;
- return string_substr( word , start , end ) ;
- }
- bool is_letter(byte c) {
- return byte_is_letter( c ) ;
- }
- void init_consts() { g_str_buf=malloc(1000); os__args = /*0*/ new_array_from_c_array(0, 0, sizeof(string), (string[]) { }); }
- string _STR(const char *fmt, ...) {
- va_list argptr;
- va_start(argptr, fmt);
- size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
- va_end(argptr);
- byte* buf = malloc(len);
- va_start(argptr, fmt);
- vsprintf(buf, fmt, argptr);
- va_end(argptr);
- #ifdef DEBUG_ALLOC
- puts("_STR:");
- puts(buf);
- #endif
- return tos2(buf);
- }
- string _STR_TMP(const char *fmt, ...) {
- va_list argptr;
- va_start(argptr, fmt);
- size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
- va_end(argptr);
- va_start(argptr, fmt);
- vsprintf(g_str_buf, fmt, argptr);
- va_end(argptr);
- #ifdef DEBUG_ALLOC
- //puts("_STR_TMP:");
- //puts(g_str_buf);
- #endif
- return tos2(g_str_buf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement