From f4e38fd061a158ecb94881273c6db6e6a332f61d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 17 Aug 2025 20:35:09 -0400 Subject: =?UTF-8?q?Switch=20from=20using=20dollar=20signs=20as=20namespace?= =?UTF-8?q?=20delimiters=20to=20using=20=E3=80=85,=20=E3=83=BD,=20and=20?= =?UTF-8?q?=E3=80=87.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/commands/commands.c | 32 +++---- lib/core/core.tm | 2 +- lib/patterns/patterns.c | 218 +++++++++++++++++++++++------------------------ lib/patterns/patterns.tm | 28 +++--- lib/random/random.tm | 2 +- lib/time/time.tm | 24 +++--- lib/time/time_defs.h | 4 +- 7 files changed, 155 insertions(+), 155 deletions(-) (limited to 'lib') diff --git a/lib/commands/commands.c b/lib/commands/commands.c index 1e9afe8e..b8d08d4b 100644 --- a/lib/commands/commands.c +++ b/lib/commands/commands.c @@ -62,13 +62,13 @@ int run_command(Text_t exe, List_t arg_list, Table_t env_table, posix_spawn_file_actions_addclose(&actions, child_errpipe[READ_END]); } - const char *exe_str = Text$as_c_string(exe); + const char *exe_str = Textヽas_c_string(exe); List_t arg_strs = {}; - List$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, exe_str, I(0), sizeof(char*)); for (int64_t i = 0; i < arg_list.length; i++) - List$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); - List$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, Textヽas_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, NULL, I(0), sizeof(char*)); char **args = arg_strs.data; extern char **environ; @@ -76,14 +76,14 @@ int run_command(Text_t exe, List_t arg_list, Table_t env_table, if (env_table.entries.length > 0) { List_t env_list = {}; // List of const char* for (char **e = environ; *e; e++) - List$insert(&env_list, e, I(0), sizeof(char*)); + Listヽinsert(&env_list, e, I(0), sizeof(char*)); for (int64_t i = 0; i < env_table.entries.length; i++) { struct { Text_t key, value; } *entry = env_table.entries.data + env_table.entries.stride*i; const char *env_entry = String(entry->key, "=", entry->value); - List$insert(&env_list, &env_entry, I(0), sizeof(char*)); + Listヽinsert(&env_list, &env_entry, I(0), sizeof(char*)); } - List$insert_value(&env_list, NULL, I(0), sizeof(char*)); + Listヽinsert_value(&env_list, NULL, I(0), sizeof(char*)); assert(env_list.stride == sizeof(char*)); env = env_list.data; } @@ -108,7 +108,7 @@ int run_command(Text_t exe, List_t arg_list, Table_t env_table, if (error_bytes) pollfds[2] = (struct pollfd){.fd=child_errpipe[WRITE_END], .events=POLLIN}; if (input_bytes.length > 0 && input_bytes.stride != 1) - List$compact(&input_bytes, sizeof(char)); + Listヽcompact(&input_bytes, sizeof(char)); if (output_bytes) *output_bytes = (List_t){.atomic=1, .stride=1, .length=0}; if (error_bytes) @@ -225,7 +225,7 @@ static Text_t _next_line(child_info_t *child) if (u8_check((uint8_t*)line, (size_t)len) != NULL) fail("Invalid UTF8!"); - Text_t line_text = Text$from_strn(line, len); + Text_t line_text = Textヽfrom_strn(line, len); free(line); return line_text; } @@ -243,13 +243,13 @@ OptionalClosure_t command_by_line(Text_t exe, List_t arg_list, Table_t env_table posix_spawn_file_actions_adddup2(&actions, child_outpipe[WRITE_END], STDOUT_FILENO); posix_spawn_file_actions_addclose(&actions, child_outpipe[READ_END]); - const char *exe_str = Text$as_c_string(exe); + const char *exe_str = Textヽas_c_string(exe); List_t arg_strs = {}; - List$insert_value(&arg_strs, exe_str, I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, exe_str, I(0), sizeof(char*)); for (int64_t i = 0; i < arg_list.length; i++) - List$insert_value(&arg_strs, Text$as_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); - List$insert_value(&arg_strs, NULL, I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, Textヽas_c_string(*(Text_t*)(arg_list.data + i*arg_list.stride)), I(0), sizeof(char*)); + Listヽinsert_value(&arg_strs, NULL, I(0), sizeof(char*)); char **args = arg_strs.data; extern char **environ; @@ -257,14 +257,14 @@ OptionalClosure_t command_by_line(Text_t exe, List_t arg_list, Table_t env_table if (env_table.entries.length > 0) { List_t env_list = {}; // List of const char* for (char **e = environ; *e; e++) - List$insert(&env_list, e, I(0), sizeof(char*)); + Listヽinsert(&env_list, e, I(0), sizeof(char*)); for (int64_t i = 0; i < env_table.entries.length; i++) { struct { Text_t key, value; } *entry = env_table.entries.data + env_table.entries.stride*i; const char *env_entry = String(entry->key, "=", entry->value); - List$insert(&env_list, &env_entry, I(0), sizeof(char*)); + Listヽinsert(&env_list, &env_entry, I(0), sizeof(char*)); } - List$insert_value(&env_list, NULL, I(0), sizeof(char*)); + Listヽinsert_value(&env_list, NULL, I(0), sizeof(char*)); assert(env_list.stride == sizeof(char*)); env = env_list.data; } diff --git a/lib/core/core.tm b/lib/core/core.tm index 0b8aba53..5ed77756 100644 --- a/lib/core/core.tm +++ b/lib/core/core.tm @@ -1,7 +1,7 @@ # This file just uses all the most commonly used standard # library modules so you don't have to import them one-by-one -use patterns_v1.0 +use patterns_v1.1 use commands_v1.0 use shell_v1.0 use pthreads_v1.0 diff --git a/lib/patterns/patterns.c b/lib/patterns/patterns.c index 224a00a0..5df1047c 100644 --- a/lib/patterns/patterns.c +++ b/lib/patterns/patterns.c @@ -48,7 +48,7 @@ static Text_t replace_list(Text_t text, List_t replacements, Text_t backref_pat, static INLINE void skip_whitespace(TextIter_t *state, int64_t *i) { while (*i < state->stack[0].text.length) { - int32_t grapheme = Text$get_grapheme_fast(state, *i); + int32_t grapheme = Textヽget_grapheme_fast(state, *i); if (grapheme > 0 && !uc_is_property_white_space((ucs4_t)grapheme)) return; *i += 1; @@ -57,7 +57,7 @@ static INLINE void skip_whitespace(TextIter_t *state, int64_t *i) static INLINE bool match_grapheme(TextIter_t *state, int64_t *i, int32_t grapheme) { - if (*i < state->stack[0].text.length && Text$get_grapheme_fast(state, *i) == grapheme) { + if (*i < state->stack[0].text.length && Textヽget_grapheme_fast(state, *i) == grapheme) { *i += 1; return true; } @@ -68,7 +68,7 @@ static INLINE bool match_str(TextIter_t *state, int64_t *i, const char *str) { int64_t matched = 0; while (matched[str]) { - if (*i + matched >= state->stack[0].text.length || Text$get_grapheme_fast(state, *i + matched) != str[matched]) + if (*i + matched >= state->stack[0].text.length || Textヽget_grapheme_fast(state, *i + matched) != str[matched]) return false; matched += 1; } @@ -80,7 +80,7 @@ static int64_t parse_int(TextIter_t *state, int64_t *i) { int64_t value = 0; for (;; *i += 1) { - uint32_t grapheme = Text$get_main_grapheme_fast(state, *i); + uint32_t grapheme = Textヽget_main_grapheme_fast(state, *i); int digit = uc_digit_value(grapheme); if (digit < 0) break; if (value >= INT64_MAX/10) break; @@ -95,7 +95,7 @@ static const char *get_property_name(TextIter_t *state, int64_t *i) char *name = GC_MALLOC_ATOMIC(UNINAME_MAX); char *dest = name; while (*i < state->stack[0].text.length) { - int32_t grapheme = Text$get_grapheme_fast(state, *i); + int32_t grapheme = Textヽget_grapheme_fast(state, *i); if (!(grapheme & ~0xFF) && (isalnum(grapheme) || grapheme == ' ' || grapheme == '_' || grapheme == '-')) { *dest = (char)grapheme; ++dest; @@ -116,16 +116,16 @@ static const char *get_property_name(TextIter_t *state, int64_t *i) } #define EAT1(state, index, cond) ({\ - int32_t grapheme = Text$get_grapheme_fast(state, index); \ + int32_t grapheme = Textヽget_grapheme_fast(state, index); \ bool success = (cond); \ if (success) index += 1; \ success; }) #define EAT2(state, index, cond1, cond2) ({\ - int32_t grapheme = Text$get_grapheme_fast(state, index); \ + int32_t grapheme = Textヽget_grapheme_fast(state, index); \ bool success = (cond1); \ if (success) { \ - grapheme = Text$get_grapheme_fast(state, index + 1); \ + grapheme = Textヽget_grapheme_fast(state, index + 1); \ success = (cond2); \ if (success) \ index += 2; \ @@ -138,12 +138,12 @@ static const char *get_property_name(TextIter_t *state, int64_t *i) static int64_t match_email(TextIter_t *state, int64_t index) { // email = local "@" domain - // local = 1-64 ([a-zA-Z0-9!#$%&‘*+–/=?^_`.{|}~] | non-ascii) + // local = 1-64 ([a-zA-Z0-9!#ヽ%&‘*+–/=?^_`.{|}~] | non-ascii) // domain = dns-label ("." dns-label)* // dns-label = 1-63 ([a-zA-Z0-9-] | non-ascii) if (index > 0) { - uint32_t prev_codepoint = Text$get_main_grapheme_fast(state, index - 1); + uint32_t prev_codepoint = Textヽget_main_grapheme_fast(state, index - 1); if (uc_is_property_alphabetic(prev_codepoint)) return -1; } @@ -187,7 +187,7 @@ static int64_t match_email(TextIter_t *state, int64_t index) static int64_t match_ipv6(TextIter_t *state, int64_t index) { if (index > 0) { - int32_t prev_codepoint = Text$get_grapheme_fast(state, index - 1); + int32_t prev_codepoint = Textヽget_grapheme_fast(state, index - 1); if ((prev_codepoint & ~0x7F) && (isxdigit(prev_codepoint) || prev_codepoint == ':')) return -1; } @@ -222,7 +222,7 @@ static int64_t match_ipv6(TextIter_t *state, int64_t index) static int64_t match_ipv4(TextIter_t *state, int64_t index) { if (index > 0) { - int32_t prev_codepoint = Text$get_grapheme_fast(state, index - 1); + int32_t prev_codepoint = Textヽget_grapheme_fast(state, index - 1); if ((prev_codepoint & ~0x7F) && (isdigit(prev_codepoint) || prev_codepoint == '.')) return -1; } @@ -310,7 +310,7 @@ static int64_t match_uri(TextIter_t *state, int64_t index) if (index > 0) { // Don't match if we're not at a word edge: - uint32_t prev_codepoint = Text$get_main_grapheme_fast(state, index - 1); + uint32_t prev_codepoint = Textヽget_main_grapheme_fast(state, index - 1); if (uc_is_property_alphabetic(prev_codepoint)) return -1; } @@ -408,10 +408,10 @@ static int64_t match_newline(TextIter_t *state, int64_t index) if (index >= state->stack[0].text.length) return -1; - uint32_t grapheme = index >= state->stack[0].text.length ? 0 : Text$get_main_grapheme_fast(state, index); + uint32_t grapheme = index >= state->stack[0].text.length ? 0 : Textヽget_main_grapheme_fast(state, index); if (grapheme == '\n') return 1; - if (grapheme == '\r' && Text$get_grapheme_fast(state, index + 1) == '\n') + if (grapheme == '\r' && Textヽget_grapheme_fast(state, index + 1) == '\n') return 2; return -1; } @@ -419,7 +419,7 @@ static int64_t match_newline(TextIter_t *state, int64_t index) static int64_t match_pat(TextIter_t *state, int64_t index, pat_t pat) { Text_t text = state->stack[0].text; - int32_t grapheme = index >= text.length ? 0 : Text$get_grapheme_fast(state, index); + int32_t grapheme = index >= text.length ? 0 : Textヽget_grapheme_fast(state, index); switch (pat.tag) { case PAT_START: { @@ -466,7 +466,7 @@ static int64_t match_pat(TextIter_t *state, int64_t index, pat_t pat) if (index + match_len >= text.length) return pat.negated ? 1 : -1; - int32_t c = Text$get_grapheme_fast(state, index + match_len); + int32_t c = Textヽget_grapheme_fast(state, index + match_len); if (c == open) depth += 1; else if (c == close) @@ -485,7 +485,7 @@ static int64_t match_pat(TextIter_t *state, int64_t index, pat_t pat) int32_t close = pat.quote_graphemes[1]; for (int64_t i = index + 1; i < text.length; i++) { - int32_t c = Text$get_grapheme_fast(state, i); + int32_t c = Textヽget_grapheme_fast(state, i); if (c == close) { return pat.negated ? -1 : (i - index) + 1; } else if (c == '\\' && index + 1 < text.length) { @@ -512,7 +512,7 @@ static pat_t parse_next_pat(TextIter_t *state, int64_t *index) uc_is_property((ucs4_t)grapheme, UC_PROPERTY_QUOTATION_MARK), grapheme == '?')) { // Quotations: "?", '?', etc - int32_t open = Text$get_grapheme_fast(state, *index-2); + int32_t open = Textヽget_grapheme_fast(state, *index-2); int32_t close = open; uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); if (!match_grapheme(state, index, close)) @@ -527,7 +527,7 @@ static pat_t parse_next_pat(TextIter_t *state, int64_t *index) uc_is_property((ucs4_t)grapheme, UC_PROPERTY_PAIRED_PUNCTUATION), grapheme == '?')) { // Nested punctuation: (?), [?], etc - int32_t open = Text$get_grapheme_fast(state, *index-2); + int32_t open = Textヽget_grapheme_fast(state, *index-2); int32_t close = open; uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); if (!match_grapheme(state, index, close)) @@ -541,7 +541,7 @@ static pat_t parse_next_pat(TextIter_t *state, int64_t *index) } else if (EAT1(state, *index, grapheme == '{')) { // named patterns {id}, {2-3 hex}, etc. skip_whitespace(state, index); int64_t min, max; - if (uc_is_digit((ucs4_t)Text$get_grapheme_fast(state, *index))) { + if (uc_is_digit((ucs4_t)Textヽget_grapheme_fast(state, *index))) { min = parse_int(state, index); skip_whitespace(state, index); if (match_grapheme(state, index, '+')) { @@ -569,7 +569,7 @@ static pat_t parse_next_pat(TextIter_t *state, int64_t *index) if (!prop_name) { // Literal character, e.g. {1?} skip_whitespace(state, index); - int32_t grapheme = Text$get_grapheme_fast(state, (*index)++); + int32_t grapheme = Textヽget_grapheme_fast(state, (*index)++); if (!match_grapheme(state, index, '}')) fail("Missing closing '}' in pattern: ", state->stack[0].text); return PAT(PAT_GRAPHEME, .grapheme=grapheme); @@ -680,7 +680,7 @@ static pat_t parse_next_pat(TextIter_t *state, int64_t *index) return PAT(PAT_GRAPHEME, .grapheme=(int32_t)grapheme); #undef PAT } else { - return (pat_t){.tag=PAT_GRAPHEME, .non_capturing=true, .min=1, .max=1, .grapheme=Text$get_grapheme_fast(state, (*index)++)}; + return (pat_t){.tag=PAT_GRAPHEME, .non_capturing=true, .min=1, .max=1, .grapheme=Textヽget_grapheme_fast(state, (*index)++)}; } } @@ -786,7 +786,7 @@ static int64_t match(Text_t text, int64_t text_index, Text_t pattern, int64_t pa static int64_t _find(Text_t text, Text_t pattern, int64_t first, int64_t last, int64_t *match_length, capture_t *captures) { - int32_t first_grapheme = Text$get_grapheme(pattern, 0); + int32_t first_grapheme = Textヽget_grapheme(pattern, 0); bool find_first = (first_grapheme != '{' && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); @@ -795,7 +795,7 @@ static int64_t _find(Text_t text, Text_t pattern, int64_t first, int64_t last, i for (int64_t i = first; i <= last; i++) { // Optimization: quickly skip ahead to first char in pattern: if (find_first) { - while (i < text.length && Text$get_grapheme_fast(&text_state, i) != first_grapheme) + while (i < text.length && Textヽget_grapheme_fast(&text_state, i) != first_grapheme) ++i; } @@ -813,7 +813,7 @@ static int64_t _find(Text_t text, Text_t pattern, int64_t first, int64_t last, i static OptionalPatternMatch find(Text_t text, Text_t pattern, Int_t from_index) { - int64_t first = Int64$from_int(from_index, false); + int64_t first = Int64ヽfrom_int(from_index, false); if (first == 0) fail("Invalid index: 0"); if (first < 0) first = text.length + first + 1; if (first > text.length || first < 1) @@ -827,22 +827,22 @@ static OptionalPatternMatch find(Text_t text, Text_t pattern, Int_t from_index) List_t capture_list = {}; for (int i = 0; captures[i].occupied; i++) { - Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); - List$insert(&capture_list, &capture, I(0), sizeof(Text_t)); + Text_t capture = Textヽslice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); + Listヽinsert(&capture_list, &capture, I(0), sizeof(Text_t)); } return (OptionalPatternMatch){ - .text=Text$slice(text, I(found+1), I(found+len)), + .text=Textヽslice(text, I(found+1), I(found+len)), .index=I(found+1), .captures=capture_list, }; } -PUREFUNC static bool Pattern$has(Text_t text, Text_t pattern) +PUREFUNC static bool Patternヽhas(Text_t text, Text_t pattern) { - if (Text$starts_with(pattern, Text("{start}"), &pattern)) { + if (Textヽstarts_with(pattern, Text("{start}"), &pattern)) { int64_t m = match(text, 0, pattern, 0, NULL, 0); return m >= 0; - } else if (Text$ends_with(text, Text("{end}"), NULL)) { + } else if (Textヽends_with(text, Text("{end}"), NULL)) { for (int64_t i = text.length-1; i >= 0; i--) { int64_t match_len = match(text, i, pattern, 0, NULL, 0); if (match_len >= 0 && i + match_len == text.length) @@ -855,16 +855,16 @@ PUREFUNC static bool Pattern$has(Text_t text, Text_t pattern) } } -static bool Pattern$matches(Text_t text, Text_t pattern) +static bool Patternヽmatches(Text_t text, Text_t pattern) { capture_t captures[MAX_BACKREFS] = {}; int64_t match_len = match(text, 0, pattern, 0, NULL, 0); return (match_len == text.length); } -static bool Pattern$match_at(Text_t text, Text_t pattern, Int_t pos, PatternMatch *dest) +static bool Patternヽmatch_at(Text_t text, Text_t pattern, Int_t pos, PatternMatch *dest) { - int64_t start = Int64$from_int(pos, false) - 1; + int64_t start = Int64ヽfrom_int(pos, false) - 1; capture_t captures[MAX_BACKREFS] = {}; int64_t match_len = match(text, start, pattern, 0, captures, 0); if (match_len < 0) @@ -872,16 +872,16 @@ static bool Pattern$match_at(Text_t text, Text_t pattern, Int_t pos, PatternMatc List_t capture_list = {}; for (int i = 0; captures[i].occupied; i++) { - Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); - List$insert(&capture_list, &capture, I(0), sizeof(Text_t)); + Text_t capture = Textヽslice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); + Listヽinsert(&capture_list, &capture, I(0), sizeof(Text_t)); } - dest->text = Text$slice(text, I(start+1), I(start+match_len)); + dest->text = Textヽslice(text, I(start+1), I(start+match_len)); dest->index = I(start+1); dest->captures = capture_list; return true; } -static OptionalList_t Pattern$captures(Text_t text, Text_t pattern) +static OptionalList_t Patternヽcaptures(Text_t text, Text_t pattern) { capture_t captures[MAX_BACKREFS] = {}; int64_t match_len = match(text, 0, pattern, 0, captures, 0); @@ -890,13 +890,13 @@ static OptionalList_t Pattern$captures(Text_t text, Text_t pattern) List_t capture_list = {}; for (int i = 0; captures[i].occupied; i++) { - Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); - List$insert(&capture_list, &capture, I(0), sizeof(Text_t)); + Text_t capture = Textヽslice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); + Listヽinsert(&capture_list, &capture, I(0), sizeof(Text_t)); } return capture_list; } -static List_t Pattern$find_all(Text_t text, Text_t pattern) +static List_t Patternヽfind_all(Text_t text, Text_t pattern) { if (pattern.length == 0) // special case return (List_t){.length=0}; @@ -906,8 +906,8 @@ static List_t Pattern$find_all(Text_t text, Text_t pattern) OptionalPatternMatch m = find(text, pattern, I(i)); if (m.is_none) break; - i = Int64$from_int(m.index, false) + m.text.length; - List$insert(&matches, &m, I_small(0), sizeof(PatternMatch)); + i = Int64ヽfrom_int(m.index, false) + m.text.length; + Listヽinsert(&matches, &m, I_small(0), sizeof(PatternMatch)); } return matches; } @@ -920,18 +920,18 @@ typedef struct { static OptionalPatternMatch next_match(match_iter_state_t *state) { - if (Int64$from_int(state->i, false) > state->state.stack[0].text.length) + if (Int64ヽfrom_int(state->i, false) > state->state.stack[0].text.length) return NONE_MATCH; OptionalPatternMatch m = find(state->state.stack[0].text, state->pattern, state->i); if (m.is_none) // No match state->i = I(state->state.stack[0].text.length + 1); else - state->i = Int$plus(m.index, I(MAX(1, m.text.length))); + state->i = Intヽplus(m.index, I(MAX(1, m.text.length))); return m; } -static Closure_t Pattern$by_match(Text_t text, Text_t pattern) +static Closure_t Patternヽby_match(Text_t text, Text_t pattern) { return (Closure_t){ .fn=(void*)next_match, @@ -944,7 +944,7 @@ static Text_t apply_backrefs(Text_t text, List_t recursive_replacements, Text_t if (backref_pat.length == 0) return replacement; - int32_t first_grapheme = Text$get_grapheme(backref_pat, 0); + int32_t first_grapheme = Textヽget_grapheme(backref_pat, 0); bool find_first = (first_grapheme != '{' && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); @@ -955,7 +955,7 @@ static Text_t apply_backrefs(Text_t text, List_t recursive_replacements, Text_t for (int64_t pos = 0; pos < replacement.length; ) { // Optimization: quickly skip ahead to first char in the backref pattern: if (find_first) { - while (pos < replacement.length && Text$get_grapheme_fast(&replacement_state, pos) != first_grapheme) + while (pos < replacement.length && Textヽget_grapheme_fast(&replacement_state, pos) != first_grapheme) ++pos; } @@ -974,39 +974,39 @@ static Text_t apply_backrefs(Text_t text, List_t recursive_replacements, Text_t if (backref < 0 || backref > 9) fail("Invalid backref index: ", backref, " (only 0-", MAX_BACKREFS-1, " are allowed)"); backref_len = (after_backref - pos); - if (Text$get_grapheme_fast(&replacement_state, pos + backref_len) == ';') + if (Textヽget_grapheme_fast(&replacement_state, pos + backref_len) == ';') backref_len += 1; // skip optional semicolon if (!captures[backref].occupied) fail("There is no capture number ", backref, "!"); - Text_t backref_text = Text$slice(text, I(captures[backref].index+1), I(captures[backref].index + captures[backref].length)); + Text_t backref_text = Textヽslice(text, I(captures[backref].index+1), I(captures[backref].index + captures[backref].length)); if (captures[backref].recursive && recursive_replacements.length > 0) backref_text = replace_list(backref_text, recursive_replacements, backref_pat, true); if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(replacement, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, backref_text); + Text_t before_slice = Textヽslice(replacement, I(nonmatching_pos+1), I(pos)); + ret = Textヽconcat(ret, before_slice, backref_text); } else { - ret = Text$concat(ret, backref_text); + ret = Textヽconcat(ret, backref_text); } pos += backref_len; nonmatching_pos = pos; } if (nonmatching_pos < replacement.length) { - Text_t last_slice = Text$slice(replacement, I(nonmatching_pos+1), I(replacement.length)); - ret = Text$concat(ret, last_slice); + Text_t last_slice = Textヽslice(replacement, I(nonmatching_pos+1), I(replacement.length)); + ret = Textヽconcat(ret, last_slice); } return ret; } -static Text_t Pattern$replace(Text_t text, Text_t pattern, Text_t replacement, Text_t backref_pat, bool recursive) +static Text_t Patternヽreplace(Text_t text, Text_t pattern, Text_t replacement, Text_t backref_pat, bool recursive) { Text_t ret = EMPTY_TEXT; - int32_t first_grapheme = Text$get_grapheme(pattern, 0); + int32_t first_grapheme = Textヽget_grapheme(pattern, 0); bool find_first = (first_grapheme != '{' && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); @@ -1023,7 +1023,7 @@ static Text_t Pattern$replace(Text_t text, Text_t pattern, Text_t replacement, T for (int64_t pos = 0; pos < text.length; ) { // Optimization: quickly skip ahead to first char in pattern: if (find_first) { - while (pos < text.length && Text$get_grapheme_fast(&text_state, pos) != first_grapheme) + while (pos < text.length && Textヽget_grapheme_fast(&text_state, pos) != first_grapheme) ++pos; } @@ -1040,22 +1040,22 @@ static Text_t Pattern$replace(Text_t text, Text_t pattern, Text_t replacement, T Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (List_t){}, replacement, backref_pat, captures); if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, replacement_text); + Text_t before_slice = Textヽslice(text, I(nonmatching_pos+1), I(pos)); + ret = Textヽconcat(ret, before_slice, replacement_text); } else { - ret = Text$concat(ret, replacement_text); + ret = Textヽconcat(ret, replacement_text); } nonmatching_pos = pos + match_len; pos += MAX(match_len, 1); } if (nonmatching_pos < text.length) { - Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); + Text_t last_slice = Textヽslice(text, I(nonmatching_pos+1), I(text.length)); + ret = Textヽconcat(ret, last_slice); } return ret; } -static Text_t Pattern$trim(Text_t text, Text_t pattern, bool trim_left, bool trim_right) +static Text_t Patternヽtrim(Text_t text, Text_t pattern, bool trim_left, bool trim_right) { int64_t first = 0, last = text.length-1; if (trim_left) { @@ -1071,14 +1071,14 @@ static Text_t Pattern$trim(Text_t text, Text_t pattern, bool trim_left, bool tri last = i-1; } } - return Text$slice(text, I(first+1), I(last+1)); + return Textヽslice(text, I(first+1), I(last+1)); } -static Text_t Pattern$map(Text_t text, Text_t pattern, Closure_t fn, bool recursive) +static Text_t Patternヽmap(Text_t text, Text_t pattern, Closure_t fn, bool recursive) { Text_t ret = EMPTY_TEXT; - int32_t first_grapheme = Text$get_grapheme(pattern, 0); + int32_t first_grapheme = Textヽget_grapheme(pattern, 0); bool find_first = (first_grapheme != '{' && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); @@ -1090,7 +1090,7 @@ static Text_t Pattern$map(Text_t text, Text_t pattern, Closure_t fn, bool recurs for (int64_t pos = 0; pos < text.length; pos++) { // Optimization: quickly skip ahead to first char in pattern: if (find_first) { - while (pos < text.length && Text$get_grapheme_fast(&text_state, pos) != first_grapheme) + while (pos < text.length && Textヽget_grapheme_fast(&text_state, pos) != first_grapheme) ++pos; } @@ -1099,37 +1099,37 @@ static Text_t Pattern$map(Text_t text, Text_t pattern, Closure_t fn, bool recurs if (match_len < 0) continue; PatternMatch m = { - .text=Text$slice(text, I(pos+1), I(pos+match_len)), + .text=Textヽslice(text, I(pos+1), I(pos+match_len)), .index=I(pos+1), .captures={}, }; for (int i = 0; captures[i].occupied; i++) { - Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); + Text_t capture = Textヽslice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); if (recursive) - capture = Pattern$map(capture, pattern, fn, recursive); - List$insert(&m.captures, &capture, I(0), sizeof(Text_t)); + capture = Patternヽmap(capture, pattern, fn, recursive); + Listヽinsert(&m.captures, &capture, I(0), sizeof(Text_t)); } Text_t replacement = text_mapper(m, fn.userdata); if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, replacement); + Text_t before_slice = Textヽslice(text, I(nonmatching_pos+1), I(pos)); + ret = Textヽconcat(ret, before_slice, replacement); } else { - ret = Text$concat(ret, replacement); + ret = Textヽconcat(ret, replacement); } nonmatching_pos = pos + match_len; pos += (match_len - 1); } if (nonmatching_pos < text.length) { - Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); + Text_t last_slice = Textヽslice(text, I(nonmatching_pos+1), I(text.length)); + ret = Textヽconcat(ret, last_slice); } return ret; } -static void Pattern$each(Text_t text, Text_t pattern, Closure_t fn, bool recursive) +static void Patternヽeach(Text_t text, Text_t pattern, Closure_t fn, bool recursive) { - int32_t first_grapheme = Text$get_grapheme(pattern, 0); + int32_t first_grapheme = Textヽget_grapheme(pattern, 0); bool find_first = (first_grapheme != '{' && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); @@ -1139,7 +1139,7 @@ static void Pattern$each(Text_t text, Text_t pattern, Closure_t fn, bool recursi for (int64_t pos = 0; pos < text.length; pos++) { // Optimization: quickly skip ahead to first char in pattern: if (find_first) { - while (pos < text.length && Text$get_grapheme_fast(&text_state, pos) != first_grapheme) + while (pos < text.length && Textヽget_grapheme_fast(&text_state, pos) != first_grapheme) ++pos; } @@ -1148,15 +1148,15 @@ static void Pattern$each(Text_t text, Text_t pattern, Closure_t fn, bool recursi if (match_len < 0) continue; PatternMatch m = { - .text=Text$slice(text, I(pos+1), I(pos+match_len)), + .text=Textヽslice(text, I(pos+1), I(pos+match_len)), .index=I(pos+1), .captures={}, }; for (int i = 0; captures[i].occupied; i++) { - Text_t capture = Text$slice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); + Text_t capture = Textヽslice(text, I(captures[i].index+1), I(captures[i].index+captures[i].length)); if (recursive) - Pattern$each(capture, pattern, fn, recursive); - List$insert(&m.captures, &capture, I(0), sizeof(Text_t)); + Patternヽeach(capture, pattern, fn, recursive); + Listヽinsert(&m.captures, &capture, I(0), sizeof(Text_t)); } action(m, fn.userdata); @@ -1183,14 +1183,14 @@ Text_t replace_list(Text_t text, List_t replacements, Text_t backref_pat, bool r // If we skipped over some non-matching text before finding a match, insert it here: if (pos > nonmatch_pos) { - Text_t before_slice = Text$slice(text, I(nonmatch_pos+1), I(pos)); - ret = Text$concat(ret, before_slice); + Text_t before_slice = Textヽslice(text, I(nonmatch_pos+1), I(pos)); + ret = Textヽconcat(ret, before_slice); } // Concatenate the replacement: Text_t replacement = *(Text_t*)(replacements.data + i*replacements.stride + sizeof(Text_t)); Text_t replacement_text = apply_backrefs(text, recursive ? replacements : (List_t){}, replacement, backref_pat, captures); - ret = Text$concat(ret, replacement_text); + ret = Textヽconcat(ret, replacement_text); pos += MAX(len, 1); nonmatch_pos = pos; goto next_pos; @@ -1202,24 +1202,24 @@ Text_t replace_list(Text_t text, List_t replacements, Text_t backref_pat, bool r } if (nonmatch_pos <= text.length) { - Text_t last_slice = Text$slice(text, I(nonmatch_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); + Text_t last_slice = Textヽslice(text, I(nonmatch_pos+1), I(text.length)); + ret = Textヽconcat(ret, last_slice); } return ret; } -static Text_t Pattern$replace_all(Text_t text, Table_t replacements, Text_t backref_pat, bool recursive) +static Text_t Patternヽreplace_all(Text_t text, Table_t replacements, Text_t backref_pat, bool recursive) { return replace_list(text, replacements.entries, backref_pat, recursive); } -static List_t Pattern$split(Text_t text, Text_t pattern) +static List_t Patternヽsplit(Text_t text, Text_t pattern) { if (text.length == 0) // special case return (List_t){.length=0}; if (pattern.length == 0) // special case - return Text$clusters(text); + return Textヽclusters(text); List_t chunks = {}; @@ -1230,13 +1230,13 @@ static List_t Pattern$split(Text_t text, Text_t pattern) if (found == i && len == 0) found = _find(text, pattern, i + 1, text.length-1, &len, NULL); if (found < 0) break; - Text_t chunk = Text$slice(text, I(i+1), I(found)); - List$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); + Text_t chunk = Textヽslice(text, I(i+1), I(found)); + Listヽinsert(&chunks, &chunk, I_small(0), sizeof(Text_t)); i = MAX(found + len, i + 1); } - Text_t last_chunk = Text$slice(text, I(i+1), I(text.length)); - List$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); + Text_t last_chunk = Textヽslice(text, I(i+1), I(text.length)); + Listヽinsert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); return chunks; } @@ -1259,7 +1259,7 @@ static OptionalText_t next_split(split_iter_state_t *state) } if (state->pattern.length == 0) { // special case - Text_t ret = Text$cluster(text, I(state->i+1)); + Text_t ret = Textヽcluster(text, I(state->i+1)); state->i += 1; return ret; } @@ -1273,14 +1273,14 @@ static OptionalText_t next_split(split_iter_state_t *state) if (found >= 0) { state->i = MAX(found + len, state->i + 1); - return Text$slice(text, I(start+1), I(found)); + return Textヽslice(text, I(start+1), I(found)); } else { state->i = state->state.stack[0].text.length + 1; - return Text$slice(text, I(start+1), I(text.length)); + return Textヽslice(text, I(start+1), I(text.length)); } } -static Closure_t Pattern$by_split(Text_t text, Text_t pattern) +static Closure_t Patternヽby_split(Text_t text, Text_t pattern) { return (Closure_t){ .fn=(void*)next_split, @@ -1288,34 +1288,34 @@ static Closure_t Pattern$by_split(Text_t text, Text_t pattern) }; } -static Text_t Pattern$escape_text(Text_t text) +static Text_t Patternヽescape_text(Text_t text) { // TODO: optimize for spans of non-escaped text Text_t ret = EMPTY_TEXT; TextIter_t state = NEW_TEXT_ITER_STATE(text); for (int64_t i = 0; i < text.length; i++) { - uint32_t g = Text$get_main_grapheme_fast(&state, i); + uint32_t g = Textヽget_main_grapheme_fast(&state, i); if (g == '{') { - ret = Text$concat(ret, Text("{1{}")); + ret = Textヽconcat(ret, Text("{1{}")); } else if (g == '?' || uc_is_property_quotation_mark(g) || (uc_is_property_paired_punctuation(g) && uc_is_property_left_of_pair(g))) { - ret = Text$concat(ret, Text("{1"), Text$slice(text, I(i+1), I(i+1)), Text("}")); + ret = Textヽconcat(ret, Text("{1"), Textヽslice(text, I(i+1), I(i+1)), Text("}")); } else { - ret = Text$concat(ret, Text$slice(text, I(i+1), I(i+1))); + ret = Textヽconcat(ret, Textヽslice(text, I(i+1), I(i+1))); } } return ret; } -static Text_t Pattern$as_text(const void *obj, bool colorize, const TypeInfo_t *info) +static Text_t Patternヽas_text(const void *obj, bool colorize, const TypeInfo_t *info) { (void)info; if (!obj) return Text("Pattern"); Text_t pat = *(Text_t*)obj; - Text_t quote = Pattern$has(pat, Text("/")) && !Pattern$has(pat, Text("|")) ? Text("|") : Text("/"); - return Text$concat(colorize ? Text("\x1b[1m$\033[m") : Text("$"), Text$quoted(pat, colorize, quote)); + Text_t quote = Patternヽhas(pat, Text("/")) && !Patternヽhas(pat, Text("|")) ? Text("|") : Text("/"); + return Textヽconcat(colorize ? Text("\x1b[1mヽ\033[m") : Text("ヽ"), Textヽquoted(pat, colorize, quote)); } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/lib/patterns/patterns.tm b/lib/patterns/patterns.tm index c5444b86..bcc54460 100644 --- a/lib/patterns/patterns.tm +++ b/lib/patterns/patterns.tm @@ -4,7 +4,7 @@ struct PatternMatch(text:Text, index:Int, captures:[Text]) lang Pat convert(text:Text -> Pat) - return C_code:Pat(Pattern$escape_text(@text)) + return C_code:Pat(Patternヽescape_text(@text)) convert(n:Int -> Pat) return Pat.from_text("$n") @@ -12,45 +12,45 @@ lang Pat extend Text func matching_pattern(text:Text, pattern:Pat, pos:Int = 1 -> PatternMatch?) result : PatternMatch - if C_code:Bool(Pattern$match_at(@text, @pattern, @pos, (void*)&@result)) + if C_code:Bool(Patternヽmatch_at(@text, @pattern, @pos, (void*)&@result)) return result return none func matches_pattern(text:Text, pattern:Pat -> Bool) - return C_code:Bool(Pattern$matches(@text, @pattern)) + return C_code:Bool(Patternヽmatches(@text, @pattern)) func pattern_captures(text:Text, pattern:Pat -> [Text]?) - return C_code:[Text]?(Pattern$captures(@text, @pattern)) + return C_code:[Text]?(Patternヽcaptures(@text, @pattern)) func replace_pattern(text:Text, pattern:Pat, replacement:Text, backref="@", recursive=yes -> Text) - return C_code:Text(Pattern$replace(@text, @pattern, @replacement, @backref, @recursive)) + return C_code:Text(Patternヽreplace(@text, @pattern, @replacement, @backref, @recursive)) func translate_patterns(text:Text, replacements:{Pat=Text}, backref="@", recursive=yes -> Text) - return C_code:Text(Pattern$replace_all(@text, @replacements, @backref, @recursive)) + return C_code:Text(Patternヽreplace_all(@text, @replacements, @backref, @recursive)) func has_pattern(text:Text, pattern:Pat -> Bool) - return C_code:Bool(Pattern$has(@text, @pattern)) + return C_code:Bool(Patternヽhas(@text, @pattern)) func find_patterns(text:Text, pattern:Pat -> [PatternMatch]) - return C_code:[PatternMatch](Pattern$find_all(@text, @pattern)) + return C_code:[PatternMatch](Patternヽfind_all(@text, @pattern)) func by_pattern(text:Text, pattern:Pat -> func(->PatternMatch?)) - return C_code:func(->PatternMatch?)(Pattern$by_match(@text, @pattern)) + return C_code:func(->PatternMatch?)(Patternヽby_match(@text, @pattern)) func each_pattern(text:Text, pattern:Pat, fn:func(m:PatternMatch), recursive=yes) - C_code { Pattern$each(@text, @pattern, @fn, @recursive); } + C_code { Patternヽeach(@text, @pattern, @fn, @recursive); } func map_pattern(text:Text, pattern:Pat, fn:func(m:PatternMatch -> Text), recursive=yes -> Text) - return C_code:Text(Pattern$map(@text, @pattern, @fn, @recursive)) + return C_code:Text(Patternヽmap(@text, @pattern, @fn, @recursive)) func split_pattern(text:Text, pattern:Pat -> [Text]) - return C_code:[Text](Pattern$split(@text, @pattern)) + return C_code:[Text](Patternヽsplit(@text, @pattern)) func by_pattern_split(text:Text, pattern:Pat -> func(->Text?)) - return C_code:func(->Text?)(Pattern$by_split(@text, @pattern)) + return C_code:func(->Text?)(Patternヽby_split(@text, @pattern)) func trim_pattern(text:Text, pattern=$Pat"{space}", left=yes, right=yes -> Text) - return C_code:Text(Pattern$trim(@text, @pattern, @left, @right)) + return C_code:Text(Patternヽtrim(@text, @pattern, @left, @right)) func main() >> "Hello world".matching_pattern($Pat'{id}') diff --git a/lib/random/random.tm b/lib/random/random.tm index 107fad66..6e7240e7 100644 --- a/lib/random/random.tm +++ b/lib/random/random.tm @@ -216,7 +216,7 @@ struct RandomNumberGenerator(_chacha:chacha_ctx, _random_bytes:[Byte]=[]; secret mpz_urandomm(r, gmp_rng, range_size); gmp_randclear(gmp_rng); - Int$plus(@min, Int$from_mpz(r)) + Intヽplus(@min, Intヽfrom_mpz(r)) ) diff --git a/lib/time/time.tm b/lib/time/time.tm index 456b5f08..82d7d9eb 100644 --- a/lib/time/time.tm +++ b/lib/time/time.tm @@ -24,7 +24,7 @@ struct Time(tv_sec:Int64, tv_usec:Int64; extern) char *zoneinfo = strstr(buf, "/zoneinfo/"); if (zoneinfo) - _local_timezone = Text$from_str(zoneinfo + strlen("/zoneinfo/")); + _local_timezone = Textヽfrom_str(zoneinfo + strlen("/zoneinfo/")); else fail("Could not resolve local tz!"); } @@ -46,17 +46,17 @@ struct Time(tv_sec:Int64, tv_usec:Int64; extern) WITH_TIMEZONE(@timezone, final_info = localtime_r(&time, &result)); static char buf[256]; size_t len = strftime(buf, sizeof(buf), String(@format), final_info); - Text$from_strn(buf, len) + Textヽfrom_strn(buf, len) ) func new(year,month,day:Int, hour=0, minute=0, second=0.0, timezone=Time.local_timezone() -> Time) return C_code : Time( struct tm info = { - .tm_min=Int32$from_int(@minute, false), - .tm_hour=Int32$from_int(@hour, false), - .tm_mday=Int32$from_int(@day, false), - .tm_mon=Int32$from_int(@month, false) - 1, - .tm_year=Int32$from_int(@year, false) - 1900, + .tm_min=Int32ヽfrom_int(@minute, false), + .tm_hour=Int32ヽfrom_int(@hour, false), + .tm_mday=Int32ヽfrom_int(@day, false), + .tm_mon=Int32ヽfrom_int(@month, false) - 1, + .tm_year=Int32ヽfrom_int(@year, false) - 1900, .tm_isdst=-1, }; @@ -154,9 +154,9 @@ struct Time(tv_sec:Int64, tv_usec:Int64; extern) struct tm info = {}; WITH_TIMEZONE(@timezone, localtime_r(&@t.tv_sec, &info)); - info.tm_mday += Int32$from_int(@days, false) + 7*Int32$from_int(@weeks, false); - info.tm_mon += Int32$from_int(@months, false); - info.tm_year += Int32$from_int(@years, false); + info.tm_mday += Int32ヽfrom_int(@days, false) + 7*Int32ヽfrom_int(@weeks, false); + info.tm_mon += Int32ヽfrom_int(@months, false); + info.tm_year += Int32ヽfrom_int(@years, false); time_t t = mktime(&info); (Time){ @@ -169,8 +169,8 @@ struct Time(tv_sec:Int64, tv_usec:Int64; extern) ret : Time? C_code { struct tm info = {.tm_isdst=-1}; - const char *str = Text$as_c_string(@text); - const char *fmt = Text$as_c_string(@format); + const char *str = Textヽas_c_string(@text); + const char *fmt = Textヽas_c_string(@format); if (strstr(fmt, "%Z")) fail("The %Z specifier is not supported for time parsing!"); diff --git a/lib/time/time_defs.h b/lib/time/time_defs.h index 30000983..ae2d009a 100644 --- a/lib/time/time_defs.h +++ b/lib/time/time_defs.h @@ -10,12 +10,12 @@ static INLINE Text_t num_format(long n, const char *unit) { if (n == 0) return Text("now"); - return Text$from_str(String((int64_t)labs(n), " ", unit, (n == -1 || n == 1) ? "" : "s", n <= 0 ? " ago" : " later")); + return Textヽfrom_str(String((int64_t)labs(n), " ", unit, (n == -1 || n == 1) ? "" : "s", n <= 0 ? " ago" : " later")); } static void set_local_timezone(Text_t tz) { - setenv("TZ", Text$as_c_string(tz), 1); + setenv("TZ", Textヽas_c_string(tz), 1); _local_timezone = tz; tzset(); } -- cgit v1.2.3