337 lines
12 KiB
C
337 lines
12 KiB
C
/*
|
|
* TinyBlocks - tb_regex.c
|
|
* Categoria "Regex": Regex.Test / Match / Find / Group / Count / Replace /
|
|
* Split, sobre POSIX ERE (<regex.h>).
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <regex.h>
|
|
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include <tinyblocks/tb_json_block.h>
|
|
#include "tb_regex.h"
|
|
|
|
#define MAX_GROUPS 32
|
|
|
|
static char s_err[200] = "";
|
|
const char* tb_regex_last_error(void) { return s_err; }
|
|
|
|
static bool compile(regex_t* re, const char* pattern) {
|
|
s_err[0] = 0;
|
|
if (!pattern) pattern = "";
|
|
int rc = regcomp(re, pattern, REG_EXTENDED);
|
|
if (rc != 0) {
|
|
char msg[160];
|
|
regerror(rc, re, msg, sizeof(msg));
|
|
snprintf(s_err, sizeof(s_err), "%s", msg);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool tb_regex_test(const char* text, const char* pattern) {
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return false;
|
|
bool ok = regexec(&re, text ? text : "", 0, NULL, 0) == 0;
|
|
regfree(&re);
|
|
return ok;
|
|
}
|
|
|
|
bool tb_regex_match(const char* text, const char* pattern) {
|
|
/* Ancora o padrao com ^( ... )$ usando grupos nao-capturantes de ERE. */
|
|
if (!pattern) pattern = "";
|
|
size_t n = strlen(pattern);
|
|
char* anch = (char*)malloc(n + 8);
|
|
if (!anch) return false;
|
|
anch[0] = '^'; anch[1] = '('; memcpy(anch + 2, pattern, n);
|
|
anch[n + 2] = ')'; anch[n + 3] = '$'; anch[n + 4] = 0;
|
|
bool ok = tb_regex_test(text, anch);
|
|
free(anch);
|
|
return ok;
|
|
}
|
|
|
|
char* tb_regex_find(const char* text, const char* pattern) {
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return NULL;
|
|
regmatch_t m;
|
|
char* out = NULL;
|
|
if (regexec(&re, text ? text : "", 1, &m, 0) == 0 && m.rm_so >= 0) {
|
|
size_t len = (size_t)(m.rm_eo - m.rm_so);
|
|
out = (char*)malloc(len + 1);
|
|
if (out) { memcpy(out, text + m.rm_so, len); out[len] = 0; }
|
|
}
|
|
regfree(&re);
|
|
return out;
|
|
}
|
|
|
|
char* tb_regex_group(const char* text, const char* pattern, int group) {
|
|
if (group < 0 || group >= MAX_GROUPS) { snprintf(s_err, sizeof(s_err), "grupo fora do intervalo"); return NULL; }
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return NULL;
|
|
regmatch_t m[MAX_GROUPS];
|
|
char* out = NULL;
|
|
if (regexec(&re, text ? text : "", MAX_GROUPS, m, 0) == 0 &&
|
|
m[group].rm_so >= 0) {
|
|
size_t len = (size_t)(m[group].rm_eo - m[group].rm_so);
|
|
out = (char*)malloc(len + 1);
|
|
if (out) { memcpy(out, text + m[group].rm_so, len); out[len] = 0; }
|
|
}
|
|
regfree(&re);
|
|
return out;
|
|
}
|
|
|
|
int tb_regex_group_count(const char* text, const char* pattern) {
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return -1;
|
|
(void)text;
|
|
int n = (int)re.re_nsub; /* numero de subexpressoes */
|
|
regfree(&re);
|
|
return n;
|
|
}
|
|
|
|
int tb_regex_count(const char* text, const char* pattern) {
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return -1;
|
|
const char* p = text ? text : "";
|
|
regmatch_t m;
|
|
int count = 0;
|
|
while (*p && regexec(&re, p, 1, &m, 0) == 0) {
|
|
count++;
|
|
if (m.rm_eo == m.rm_so) { p++; if (!*p) break; }
|
|
else p += m.rm_eo;
|
|
}
|
|
regfree(&re);
|
|
return count;
|
|
}
|
|
|
|
/* --- Expansao do substituto -------------------------------------------- */
|
|
typedef struct { char* p; size_t n, cap; } Buf;
|
|
static bool b_res(Buf* b, size_t extra) {
|
|
if (b->n + extra + 1 <= b->cap) return true;
|
|
size_t cap = b->cap ? b->cap : 64;
|
|
while (cap < b->n + extra + 1) cap *= 2;
|
|
char* p = (char*)realloc(b->p, cap);
|
|
if (!p) return false;
|
|
b->p = p; b->cap = cap; return true;
|
|
}
|
|
static bool b_put(Buf* b, const char* s, size_t n) {
|
|
if (!b_res(b, n)) return false;
|
|
memcpy(b->p + b->n, s, n); b->n += n; b->p[b->n] = 0; return true;
|
|
}
|
|
|
|
static void expand(Buf* out, const char* repl, const char* text, regmatch_t* m) {
|
|
for (const char* p = repl; *p; p++) {
|
|
if (*p == '$' && p[1] == '$') { b_put(out, "$", 1); p++; continue; }
|
|
if (*p == '$' && p[1] >= '0' && p[1] <= '9') {
|
|
int g = p[1] - '0';
|
|
if (m[g].rm_so >= 0) b_put(out, text + m[g].rm_so, (size_t)(m[g].rm_eo - m[g].rm_so));
|
|
p++;
|
|
continue;
|
|
}
|
|
b_put(out, p, 1);
|
|
}
|
|
}
|
|
|
|
char* tb_regex_replace(const char* text, const char* pattern, const char* repl) {
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return NULL;
|
|
if (!text) text = "";
|
|
if (!repl) repl = "";
|
|
Buf out = {0};
|
|
const char* p = text;
|
|
regmatch_t m[MAX_GROUPS];
|
|
while (*p && regexec(&re, p, MAX_GROUPS, m, 0) == 0) {
|
|
b_put(&out, p, (size_t)m[0].rm_so);
|
|
expand(&out, repl, p, m);
|
|
if (m[0].rm_eo == m[0].rm_so) {
|
|
b_put(&out, p, 1);
|
|
p++;
|
|
if (!*p) break;
|
|
} else {
|
|
p += m[0].rm_eo;
|
|
}
|
|
}
|
|
if (*p) b_put(&out, p, strlen(p));
|
|
regfree(&re);
|
|
if (!out.p) { out.p = (char*)malloc(1); if (out.p) out.p[0] = 0; }
|
|
return out.p;
|
|
}
|
|
|
|
char** tb_regex_split(const char* text, const char* pattern, int* out_count) {
|
|
if (out_count) *out_count = 0;
|
|
regex_t re;
|
|
if (!compile(&re, pattern)) return NULL;
|
|
if (!text) text = "";
|
|
size_t cap = 8, n = 0;
|
|
char** list = (char**)malloc(cap * sizeof(char*));
|
|
if (!list) { regfree(&re); return NULL; }
|
|
const char* p = text;
|
|
regmatch_t m;
|
|
for (;;) {
|
|
if (*p && regexec(&re, p, 1, &m, 0) == 0) {
|
|
size_t len = (size_t)m.rm_so;
|
|
char* item = (char*)malloc(len + 1);
|
|
if (!item) break;
|
|
memcpy(item, p, len); item[len] = 0;
|
|
if (n + 2 > cap) { cap *= 2; list = (char**)realloc(list, cap * sizeof(char*)); }
|
|
list[n++] = item;
|
|
if (m.rm_eo == m.rm_so) {
|
|
if (!p[m.rm_so]) { p += m.rm_so; break; }
|
|
/* casa vazio: emite 1 char e continua */
|
|
char* one = (char*)malloc(2);
|
|
one[0] = p[m.rm_so]; one[1] = 0;
|
|
list[n++] = one;
|
|
p += m.rm_so + 1;
|
|
} else {
|
|
p += m.rm_eo;
|
|
}
|
|
} else {
|
|
size_t len = strlen(p);
|
|
char* item = (char*)malloc(len + 1);
|
|
if (!item) break;
|
|
memcpy(item, p, len); item[len] = 0;
|
|
if (n + 1 > cap) { cap += 1; list = (char**)realloc(list, cap * sizeof(char*)); }
|
|
list[n++] = item;
|
|
break;
|
|
}
|
|
}
|
|
regfree(&re);
|
|
list[n] = NULL;
|
|
if (out_count) *out_count = (int)n;
|
|
return list;
|
|
}
|
|
|
|
void tb_regex_free_list(char** list) {
|
|
if (!list) return;
|
|
for (int i = 0; list[i]; i++) free(list[i]);
|
|
free(list);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- Blocos */
|
|
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
|
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
|
}
|
|
static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
|
if (v.kind == TB_VALUE_REAL) return v.as.real;
|
|
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
|
|
return 0;
|
|
}
|
|
|
|
static void eval_test(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
bool ok = tb_regex_test(in_text(self, rt, 0), in_text(self, rt, 1));
|
|
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
|
|
self->dirty = false;
|
|
}
|
|
static void eval_match(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
bool ok = tb_regex_match(in_text(self, rt, 0), in_text(self, rt, 1));
|
|
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
|
|
self->dirty = false;
|
|
}
|
|
static void eval_find(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char* s = tb_regex_find(in_text(self, rt, 0), in_text(self, rt, 1));
|
|
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
|
free(s);
|
|
self->dirty = false;
|
|
}
|
|
static void eval_group(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char* s = tb_regex_group(in_text(self, rt, 0), in_text(self, rt, 1), (int)in_real(self, rt, 2));
|
|
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
|
free(s);
|
|
self->dirty = false;
|
|
}
|
|
static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
tb_block_set_output(self, 0, tb_value_integer(tb_regex_count(in_text(self, rt, 0), in_text(self, rt, 1))));
|
|
self->dirty = false;
|
|
}
|
|
static void eval_groups(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
tb_block_set_output(self, 0, tb_value_integer(tb_regex_group_count("", in_text(self, rt, 1))));
|
|
self->dirty = false;
|
|
}
|
|
static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char* s = tb_regex_replace(in_text(self, rt, 0), in_text(self, rt, 1), in_text(self, rt, 2));
|
|
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
|
free(s);
|
|
self->dirty = false;
|
|
}
|
|
static void eval_split(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
int n = 0;
|
|
char** parts = tb_regex_split(in_text(self, rt, 0), in_text(self, rt, 1), &n);
|
|
TinyBlocks_JsonValue* arr = tb_json_array();
|
|
if (arr) {
|
|
for (int i = 0; parts && parts[i]; i++) {
|
|
TinyBlocks_JsonValue* s = tb_json_string(parts[i]);
|
|
if (s) { tb_json_array_push(arr, s); tb_json_release(s); }
|
|
}
|
|
tb_block_set_output(self, 0, tb_value_json(arr));
|
|
tb_json_release(arr);
|
|
} else {
|
|
tb_block_set_output(self, 0, tb_value_text(""));
|
|
}
|
|
tb_regex_free_list(parts);
|
|
self->dirty = false;
|
|
}
|
|
|
|
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
|
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
|
const TinyBlocks_SlotDesc* out, uint32_t no,
|
|
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
|
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
|
return t && tb_library_add(lib, &t->base);
|
|
}
|
|
|
|
static bool regex_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
static const TinyBlocks_Category cat = {
|
|
"Regex",
|
|
{ 69/255.f, 90/255.f, 100/255.f, 1.f},
|
|
{ 38/255.f, 50/255.f, 56/255.f, 1.f},
|
|
};
|
|
tb_library_set_category(lib, &cat);
|
|
|
|
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
|
|
TB_BLOCK_META("Regex.Test", "eval_test",
|
|
"Verdadeiro se o padrao ocorre no texto");
|
|
TB_BLOCK_META("Regex.Match", "eval_match",
|
|
"Verdadeiro se o padrao casa o texto inteiro");
|
|
TB_BLOCK_META("Regex.Find", "eval_find",
|
|
"Primeiro trecho do texto que casa o padrao");
|
|
TB_BLOCK_META("Regex.Group", "eval_group",
|
|
"Conteudo do grupo N do primeiro casamento");
|
|
TB_BLOCK_META("Regex.Count", "eval_count",
|
|
"Quantas vezes o padrao ocorre no texto");
|
|
TB_BLOCK_META("Regex.Groups", "eval_groups",
|
|
"Numero de grupos do padrao");
|
|
TB_BLOCK_META("Regex.Replace", "eval_replace",
|
|
"Substitui as ocorrencias ($1..$9 como retorno)");
|
|
TB_BLOCK_META("Regex.Split", "eval_split",
|
|
"Divide o texto pelo padrao (lista JSON)");
|
|
|
|
static const TinyBlocks_SlotDesc two[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc three[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"com", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc grp[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"grupo", TB_VALUE_REAL}};
|
|
static const TinyBlocks_SlotDesc pat[] = {{"padrao", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc out_text[] = {{"texto", TB_VALUE_TEXT}};
|
|
static const TinyBlocks_SlotDesc out_bool[] = {{"verdadeiro", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc out_int[] = {{"numero", TB_VALUE_INTEGER}};
|
|
static const TinyBlocks_SlotDesc out_list[] = {{"lista", TB_VALUE_JSON}};
|
|
|
|
return reg(rt, lib, "Regex.Test", two, 2, out_bool, 1, eval_test)
|
|
&& reg(rt, lib, "Regex.Match", two, 2, out_bool, 1, eval_match)
|
|
&& reg(rt, lib, "Regex.Find", two, 2, out_text, 1, eval_find)
|
|
&& reg(rt, lib, "Regex.Group", grp, 3, out_text, 1, eval_group)
|
|
&& reg(rt, lib, "Regex.Count", two, 2, out_int, 1, eval_count)
|
|
&& reg(rt, lib, "Regex.Groups", pat, 1, out_int, 1, eval_groups)
|
|
&& reg(rt, lib, "Regex.Replace", three, 3, out_text, 1, eval_replace)
|
|
&& reg(rt, lib, "Regex.Split", two, 2, out_list, 1, eval_split);
|
|
}
|
|
|
|
TINYBLOCKS_DEFINE_LIBRARY(regex, "tinyblocks-regex", "1.0.0", regex_init, NULL)
|
|
|
|
const TinyBlocks_LibraryDescriptor* tb_regex_get_library(void) {
|
|
return &_tb_lib_desc_regex;
|
|
}
|