Conversões de tipo em Texto, Números, Listas e Imagem

- Texto: "texto de ( )" (qualquer valor; lista vira JSON), "código da
  letra ( )" e "letra do código ( )" (Unicode).
- Números: "número de ( )" (aceita vírgula decimal; não-número vira 0),
  "( ) na base ( )" e "( ) da base ( ) para número"; tb_number_parse na API.
- Listas: "lista de ( )" (texto com vírgulas ou JSON; objeto vira a lista
  dos valores).
- Imagem: "pixels da imagem ( )" ([r,g,b,a] linha a linha), "imagem de
  largura ( ) com os pixels ( )", "imagem ( ) em tons de cinza" e
  "desenho em texto da imagem ( ) com ( ) colunas" (arte ASCII).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:39:01 -03:00
1 parent 0c73e81c1a
commit 0103f39ffe
7 files changed
+426 -7

No files matched your search

+73 -2
View File
@@ -2,7 +2,8 @@
* TinyBlocks - tb_string.c
* Categoria "Texto": String.Upper / Lower / Sub / Letter / Length / Find /
* Replace / Trim / Repeat / Reverse / Split e os booleanos String.Contains /
* StartsWith / EndsWith / IsNumber / IsEmpty.
* StartsWith / EndsWith / IsNumber / IsEmpty e as conversoes String.From
* (qualquer valor -> texto), String.Code / FromCode (letra <-> codigo).
*
* Tudo conta LETRAS (pontos de codigo UTF-8), nao bytes: "maçã" tem 4.
* Maiusculas/minusculas cobrem ASCII e Latin-1 (á, ç, õ, ...), o que basta
@@ -312,6 +313,68 @@ static void eval_isempty(TinyBlocks_Block* self, TinyBlocks* rt) {
free(t);
}
/* -- Conversoes ----------------------------------------------------------- */
/* "texto de (x)": numero sem ".0", lista/JSON como JSON, imagem descrita */
static void eval_from(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
char buf[64];
switch (v.kind) {
case TB_VALUE_TEXT: out_owned(self, dup_n(v.as.text ? v.as.text : "", strlen(v.as.text ? v.as.text : ""))); return;
case TB_VALUE_INTEGER: snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer); break;
case TB_VALUE_REAL: snprintf(buf, sizeof(buf), "%.15g", v.as.real); break;
case TB_VALUE_JSON: {
const char* s = tb_json_as_string(v.as.json); /* texto JSON sai sem aspas */
out_owned(self, s ? dup_n(s, strlen(s)) : tb_json_stringify(v.as.json));
return;
}
case TB_VALUE_IMAGE:
snprintf(buf, sizeof(buf), "imagem %dx%d", tb_image_width(v.as.image), tb_image_height(v.as.image));
break;
default: buf[0] = 0; break;
}
out_owned(self, dup_n(buf, strlen(buf)));
}
/* Ponto de codigo da primeira letra (UTF-8); 0 se vazio */
static long u8_decode(const char* s) {
const unsigned char* p = (const unsigned char*)s;
size_t l = u8_len(*p);
if (!*p) return 0;
if (l == 1) return *p;
long c = *p & (0xFF >> (l + 1));
for (size_t i = 1; i < l; i++) {
if ((p[i] & 0xC0) != 0x80) return *p;
c = (c << 6) | (p[i] & 0x3F);
}
return c;
}
static size_t u8_encode(long c, char* out) {
if (c < 0 || c > 0x10FFFF || (c >= 0xD800 && c <= 0xDFFF)) return 0;
if (c < 0x80) { out[0] = (char)c; return 1; }
if (c < 0x800) { out[0] = (char)(0xC0 | (c >> 6)); out[1] = (char)(0x80 | (c & 0x3F)); return 2; }
if (c < 0x10000) { out[0] = (char)(0xE0 | (c >> 12)); out[1] = (char)(0x80 | ((c >> 6) & 0x3F));
out[2] = (char)(0x80 | (c & 0x3F)); return 3; }
out[0] = (char)(0xF0 | (c >> 18)); out[1] = (char)(0x80 | ((c >> 12) & 0x3F));
out[2] = (char)(0x80 | ((c >> 6) & 0x3F)); out[3] = (char)(0x80 | (c & 0x3F));
return 4;
}
/* "código da letra (A)" -> 65 (Unicode, "ç" = 231) */
static void eval_code(TinyBlocks_Block* self, TinyBlocks* rt) {
char b0[64];
tb_block_set_output(self, 0, tb_value_integer(u8_decode(TXT(0))));
self->dirty = false;
}
/* "letra do código (65)" -> "A" */
static void eval_from_code(TinyBlocks_Block* self, TinyBlocks* rt) {
char buf[5];
size_t n = u8_encode((long)in_int(self, rt, 0), buf);
out_owned(self, dup_n(buf, n));
}
/* -- Registro ------------------------------------------------------------- */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
@@ -346,6 +409,9 @@ static bool string_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
TB_BLOCK_META("String.EndsWith", "eval_ends", "Verdadeiro se o texto termina com o trecho");
TB_BLOCK_META("String.IsNumber", "tb_string_is_number", "Verdadeiro se o texto for um numero");
TB_BLOCK_META("String.IsEmpty", "eval_isempty", "Verdadeiro se o texto estiver vazio (ou so espacos)");
TB_BLOCK_META("String.From", "eval_from", "Converte qualquer valor em texto (lista vira JSON)");
TB_BLOCK_META("String.Code", "eval_code", "Codigo Unicode da primeira letra (A = 65)");
TB_BLOCK_META("String.FromCode", "eval_from_code", "Letra com o codigo Unicode dado (65 = A)");
static const TinyBlocks_SlotDesc t1[] = {{"texto", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc t2[] = {{"texto", TB_VALUE_TEXT}, {"trecho", TB_VALUE_TEXT}};
@@ -353,6 +419,8 @@ static bool string_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_SlotDesc sub[] = {{"texto", TB_VALUE_TEXT}, {"de", TB_VALUE_INTEGER}, {"ate", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc tn[] = {{"texto", TB_VALUE_TEXT}, {"n", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc split[]= {{"texto", TB_VALUE_TEXT}, {"separador", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc any1[] = {{"valor", TB_VALUE_NONE}};
static const TinyBlocks_SlotDesc n1[] = {{"código", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc text_out[] = {{"texto", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc int_out[] = {{"numero", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc list_out[] = {{"lista", TB_VALUE_JSON}};
@@ -378,7 +446,10 @@ static bool string_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
&& reg(rt, lib, "String.StartsWith", t2, 2, beg_out, 1, eval_starts)
&& reg(rt, lib, "String.EndsWith", t2, 2, end_out, 1, eval_ends)
&& reg(rt, lib, "String.IsNumber", t1, 1, num_out, 1, eval_isnum)
&& reg(rt, lib, "String.IsEmpty", t1, 1, emp_out, 1, eval_isempty);
&& reg(rt, lib, "String.IsEmpty", t1, 1, emp_out, 1, eval_isempty)
&& reg(rt, lib, "String.From", any1, 1, text_out, 1, eval_from)
&& reg(rt, lib, "String.Code", t1, 1, int_out, 1, eval_code)
&& reg(rt, lib, "String.FromCode", n1, 1, text_out, 1, eval_from_code);
}
static void string_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {