Libraries os, string, number e vector (listas); pastas e booleanos no fs
- tinyblocks-fs: itens da pasta (lista), quantidade/item N da pasta e os booleanos "é arquivo?", "é pasta?" e "existe?". - tinyblocks-os (categoria Sistema): sistema operacional, distribuição, versão do kernel, arquitetura, computador, usuário, pastas, CPUs, memória, tempo ligado e "o sistema é [ ]?". - tinyblocks-string (Texto): maiúsculas/minúsculas, trecho, letra N, posição, troca, apara, repete, inverte, divide em lista e testes; conta letras UTF-8. - tinyblocks-number (Números): reais, potência, raiz, arredondamentos, trigonometria em graus, logaritmos, pi, aleatório, limite, formatação, par/primo/inteiro. - tinyblocks-vector (Listas, blocos List.*): listas JSON imutáveis com adicionar, inserir, remover, trocar, item N, posição, fatia, ordenar, juntar, soma; campos aceitam "[1,2]" ou "a, b". O exemplo de desenho vetorial passa a se chamar tinyblocks-vector-draw. - Editor: saída única cujo nome termina em '?' vira bloco booleano (hexágono); listas JSON aparecem como texto no "escreva"; rótulos PT. - Testes: make test-os e make test-stdlib. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f0abc7593a
commit
33705b2903
25 files changed
+2262
-9
No files matched your search
@@ -0,0 +1,29 @@
|
||||
# Makefile da library string (categoria "Texto")
|
||||
# Constroi build/libtinyblocks-string.a e build/libtinyblocks-string.so
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_string.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-string.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-string.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_string.c include/tb_string.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* TinyBlocks - tb_string.h
|
||||
* Library tinyblocks-string: blocos de texto (maiusculas/minusculas, trecho,
|
||||
* letra N, contem/comeca/termina, posicao, troca, apara, repete, inverte,
|
||||
* divide em lista, testes de numero/vazio). Ciente de UTF-8: posicoes e
|
||||
* tamanhos contam letras, nao bytes. Registra a categoria "Texto".
|
||||
*/
|
||||
#ifndef TINYBLOCKS_STRING_H
|
||||
#define TINYBLOCKS_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_string_get_library(void);
|
||||
|
||||
/* Funcoes usadas pelos blocos (uteis tambem direto de C). As que devolvem
|
||||
char* alocam com malloc (libere com free). Posicoes comecam em 1. */
|
||||
TB_API size_t tb_string_length (const char* s); /* em letras */
|
||||
TB_API char* tb_string_upper (const char* s);
|
||||
TB_API char* tb_string_lower (const char* s);
|
||||
TB_API char* tb_string_sub (const char* s, long long from, long long to);
|
||||
TB_API long long tb_string_find(const char* s, const char* what); /* 0 = nao achou */
|
||||
TB_API char* tb_string_replace(const char* s, const char* what, const char* with);
|
||||
TB_API char* tb_string_trim (const char* s);
|
||||
TB_API char* tb_string_reverse(const char* s);
|
||||
TB_API bool tb_string_is_number(const char* s);
|
||||
|
||||
#endif /* TINYBLOCKS_STRING_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library string. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-string"
|
||||
version = "1.0.0"
|
||||
category = "Texto"
|
||||
description = "Texto - maiusculas, trecho, letra N, contem, troca, divide em lista"
|
||||
color = [207, 99, 207, 255]
|
||||
dark = [170, 68, 170, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_string.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-string.so"
|
||||
static = "build/libtinyblocks-string.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,396 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* Tudo conta LETRAS (pontos de codigo UTF-8), nao bytes: "maçã" tem 4.
|
||||
* Maiusculas/minusculas cobrem ASCII e Latin-1 (á, ç, õ, ...), o que basta
|
||||
* para portugues. "contém" ignora maiusculas, como no Scratch.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#include "tb_string.h"
|
||||
|
||||
#define STR_MAX_OUT (1u << 20) /* teto de 1 MB para "repita" */
|
||||
|
||||
/* -- UTF-8 ---------------------------------------------------------------- */
|
||||
|
||||
static size_t u8_len(unsigned char c) {
|
||||
if (c < 0x80) return 1;
|
||||
if ((c & 0xE0) == 0xC0) return 2;
|
||||
if ((c & 0xF0) == 0xE0) return 3;
|
||||
if ((c & 0xF8) == 0xF0) return 4;
|
||||
return 1; /* byte invalido: conta 1 */
|
||||
}
|
||||
|
||||
/* Byte onde comeca a letra `idx` (0-based); strlen se passar do fim */
|
||||
static size_t u8_offset(const char* s, size_t idx) {
|
||||
size_t o = 0, n = strlen(s);
|
||||
while (idx > 0 && o < n) {
|
||||
size_t l = u8_len((unsigned char)s[o]);
|
||||
o = o + l > n ? n : o + l;
|
||||
idx--;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
static char* dup_n(const char* s, size_t n) {
|
||||
char* r = malloc(n + 1);
|
||||
if (!r) return NULL;
|
||||
memcpy(r, s, n);
|
||||
r[n] = 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Muda a caixa: up = maiusculas. ASCII + Latin-1 (C3 80..9E <-> C3 A0..BE) */
|
||||
static char* change_case(const char* s, bool up) {
|
||||
size_t n = strlen(s);
|
||||
char* r = dup_n(s, n);
|
||||
if (!r) return NULL;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char)r[i];
|
||||
if (c < 0x80) {
|
||||
r[i] = (char)(up ? toupper(c) : tolower(c));
|
||||
} else if (c == 0xC3 && i + 1 < n) {
|
||||
unsigned char d = (unsigned char)r[i + 1];
|
||||
if (up && d >= 0xA0 && d <= 0xBE && d != 0xB7) r[i + 1] = (char)(d - 0x20);
|
||||
else if (!up && d >= 0x80 && d <= 0x9E && d != 0x97) r[i + 1] = (char)(d + 0x20);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Quantas letras ha nos primeiros `nbytes` bytes */
|
||||
static size_t u8_count(const char* s, size_t nbytes) {
|
||||
size_t n = 0, o = 0;
|
||||
while (o < nbytes && s[o]) { o += u8_len((unsigned char)s[o]); n++; }
|
||||
return n;
|
||||
}
|
||||
|
||||
/* -- API ------------------------------------------------------------------ */
|
||||
|
||||
TB_API size_t tb_string_length(const char* s) {
|
||||
return u8_count(s, strlen(s));
|
||||
}
|
||||
|
||||
TB_API char* tb_string_upper(const char* s) { return change_case(s, true); }
|
||||
TB_API char* tb_string_lower(const char* s) { return change_case(s, false); }
|
||||
|
||||
/* Letras `from` ate `to` (1-based, inclusivo), cortadas aos limites */
|
||||
TB_API char* tb_string_sub(const char* s, long long from, long long to) {
|
||||
long long n = (long long)tb_string_length(s);
|
||||
if (from < 1) from = 1;
|
||||
if (to > n) to = n;
|
||||
if (from > to) return dup_n("", 0);
|
||||
size_t a = u8_offset(s, (size_t)(from - 1));
|
||||
size_t b = u8_offset(s, (size_t)to);
|
||||
return dup_n(s + a, b - a);
|
||||
}
|
||||
|
||||
TB_API long long tb_string_find(const char* s, const char* what) {
|
||||
if (!what[0]) return 0;
|
||||
const char* p = strstr(s, what);
|
||||
if (!p) return 0;
|
||||
return (long long)u8_count(s, (size_t)(p - s)) + 1;
|
||||
}
|
||||
|
||||
TB_API char* tb_string_replace(const char* s, const char* what, const char* with) {
|
||||
size_t wl = strlen(what), rl = strlen(with), n = strlen(s);
|
||||
if (!wl) return dup_n(s, n);
|
||||
size_t count = 0;
|
||||
for (const char* p = strstr(s, what); p; p = strstr(p + wl, what)) count++;
|
||||
size_t out = n + count * rl - count * wl;
|
||||
char* r = malloc(out + 1);
|
||||
if (!r) return NULL;
|
||||
char* o = r;
|
||||
const char* cur = s;
|
||||
for (const char* p = strstr(s, what); p; p = strstr(cur, what)) {
|
||||
memcpy(o, cur, (size_t)(p - cur)); o += p - cur;
|
||||
memcpy(o, with, rl); o += rl;
|
||||
cur = p + wl;
|
||||
}
|
||||
strcpy(o, cur);
|
||||
return r;
|
||||
}
|
||||
|
||||
TB_API char* tb_string_trim(const char* s) {
|
||||
const char* a = s;
|
||||
while (*a && isspace((unsigned char)*a)) a++;
|
||||
const char* b = a + strlen(a);
|
||||
while (b > a && isspace((unsigned char)b[-1])) b--;
|
||||
return dup_n(a, (size_t)(b - a));
|
||||
}
|
||||
|
||||
TB_API char* tb_string_reverse(const char* s) {
|
||||
size_t n = strlen(s);
|
||||
char* r = malloc(n + 1);
|
||||
if (!r) return NULL;
|
||||
size_t o = 0;
|
||||
while (o < n) {
|
||||
size_t l = u8_len((unsigned char)s[o]);
|
||||
if (o + l > n) l = n - o;
|
||||
memcpy(r + (n - o - l), s + o, l); /* letra inteira, de tras pra frente */
|
||||
o += l;
|
||||
}
|
||||
r[n] = 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
TB_API bool tb_string_is_number(const char* s) {
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (!*s) return false;
|
||||
char* e;
|
||||
strtod(s, &e);
|
||||
if (e == s) return false;
|
||||
while (isspace((unsigned char)*e)) e++;
|
||||
return *e == 0;
|
||||
}
|
||||
|
||||
/* -- Entradas ------------------------------------------------------------- */
|
||||
|
||||
/* Texto da entrada i (numeros viram texto). Fica valido ate o proximo uso. */
|
||||
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* buf, size_t cap) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
switch (v.kind) {
|
||||
case TB_VALUE_TEXT: return v.as.text ? v.as.text : "";
|
||||
case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v.as.integer); return buf;
|
||||
case TB_VALUE_REAL: snprintf(buf, cap, "%g", v.as.real); return buf;
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
static long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
|
||||
if (v.kind == TB_VALUE_REAL) return (long long)v.as.real;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void out_owned(TinyBlocks_Block* self, char* s) {
|
||||
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
||||
free(s);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void out_bool(TinyBlocks_Block* self, bool b) {
|
||||
tb_block_set_output(self, 0, tb_value_integer(b ? 1 : 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
#define TXT(i) in_text(self, rt, (i), b##i, sizeof(b##i))
|
||||
|
||||
/* -- Blocos --------------------------------------------------------------- */
|
||||
|
||||
static void eval_upper(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_upper(TXT(0))); }
|
||||
static void eval_lower(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_lower(TXT(0))); }
|
||||
static void eval_trim(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_trim(TXT(0))); }
|
||||
static void eval_reverse(TinyBlocks_Block* self, TinyBlocks* rt) { char b0[64]; out_owned(self, tb_string_reverse(TXT(0))); }
|
||||
|
||||
static void eval_sub(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
out_owned(self, tb_string_sub(TXT(0), in_int(self, rt, 1), in_int(self, rt, 2)));
|
||||
}
|
||||
|
||||
static void eval_letter(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
long long k = in_int(self, rt, 1);
|
||||
out_owned(self, tb_string_sub(TXT(0), k, k));
|
||||
}
|
||||
|
||||
static void eval_length(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
tb_block_set_output(self, 0, tb_value_integer((int64_t)tb_string_length(TXT(0))));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_find(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64];
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_string_find(TXT(0), TXT(1))));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64], b2[64];
|
||||
out_owned(self, tb_string_replace(TXT(0), TXT(1), TXT(2)));
|
||||
}
|
||||
|
||||
static void eval_repeat(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
const char* s = TXT(0);
|
||||
long long k = in_int(self, rt, 1);
|
||||
size_t n = strlen(s);
|
||||
if (k < 0) k = 0;
|
||||
if (n && (size_t)k > STR_MAX_OUT / n) k = (long long)(STR_MAX_OUT / n);
|
||||
char* r = malloc(n * (size_t)k + 1);
|
||||
if (r) {
|
||||
for (long long i = 0; i < k; i++) memcpy(r + (size_t)i * n, s, n);
|
||||
r[n * (size_t)k] = 0;
|
||||
}
|
||||
out_owned(self, r);
|
||||
}
|
||||
|
||||
static void push_piece(TinyBlocks_JsonValue* arr, const char* s, size_t n) {
|
||||
char* piece = dup_n(s, n);
|
||||
TinyBlocks_JsonValue* item = piece ? tb_json_string(piece) : NULL;
|
||||
free(piece);
|
||||
if (item) { tb_json_array_push(arr, item); tb_json_release(item); }
|
||||
}
|
||||
|
||||
/* Divide em lista JSON; separador vazio = uma letra por item.
|
||||
"a,,b" -> ["a","","b"]; "a," -> ["a",""]; "" -> [] */
|
||||
static void eval_split(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64];
|
||||
const char* s = TXT(0);
|
||||
const char* sep = TXT(1);
|
||||
size_t sl = strlen(sep), n = strlen(s);
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
if (arr && n) {
|
||||
if (!sl) {
|
||||
for (size_t o = 0; o < n; ) {
|
||||
size_t l = u8_len((unsigned char)s[o]);
|
||||
if (o + l > n) l = n - o;
|
||||
push_piece(arr, s + o, l);
|
||||
o += l;
|
||||
}
|
||||
} else {
|
||||
const char* cur = s;
|
||||
for (;;) {
|
||||
const char* p = strstr(cur, sep);
|
||||
push_piece(arr, cur, p ? (size_t)(p - cur) : strlen(cur));
|
||||
if (!p) break;
|
||||
cur = p + sl;
|
||||
}
|
||||
}
|
||||
}
|
||||
tb_block_set_output(self, 0, tb_value_json(arr));
|
||||
tb_json_release(arr);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_contains(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64];
|
||||
char* a = tb_string_lower(TXT(0));
|
||||
char* w = tb_string_lower(TXT(1));
|
||||
out_bool(self, a && w && strstr(a, w) != NULL);
|
||||
free(a); free(w);
|
||||
}
|
||||
|
||||
static void eval_starts(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64];
|
||||
const char* s = TXT(0);
|
||||
const char* w = TXT(1);
|
||||
out_bool(self, strncmp(s, w, strlen(w)) == 0);
|
||||
}
|
||||
|
||||
static void eval_ends(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64], b1[64];
|
||||
const char* s = TXT(0);
|
||||
const char* w = TXT(1);
|
||||
size_t n = strlen(s), m = strlen(w);
|
||||
out_bool(self, m <= n && strcmp(s + n - m, w) == 0);
|
||||
}
|
||||
|
||||
static void eval_isnum(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
out_bool(self, tb_string_is_number(TXT(0)));
|
||||
}
|
||||
|
||||
static void eval_isempty(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char b0[64];
|
||||
char* t = tb_string_trim(TXT(0));
|
||||
out_bool(self, !t || !t[0]);
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------- */
|
||||
|
||||
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 string_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Texto",
|
||||
{207/255.f, 99/255.f, 207/255.f, 1.f},
|
||||
{170/255.f, 68/255.f, 170/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
TB_BLOCK_META("String.Upper", "tb_string_upper", "Texto em letras maiusculas");
|
||||
TB_BLOCK_META("String.Lower", "tb_string_lower", "Texto em letras minusculas");
|
||||
TB_BLOCK_META("String.Sub", "tb_string_sub", "Pedaco do texto da letra A ate a letra B");
|
||||
TB_BLOCK_META("String.Letter", "eval_letter", "A letra N do texto (1 = primeira)");
|
||||
TB_BLOCK_META("String.Length", "tb_string_length", "Quantas letras o texto tem");
|
||||
TB_BLOCK_META("String.Find", "tb_string_find", "Posicao do trecho no texto (0 = nao tem)");
|
||||
TB_BLOCK_META("String.Replace", "tb_string_replace", "Troca todas as ocorrencias de um trecho");
|
||||
TB_BLOCK_META("String.Trim", "tb_string_trim", "Tira os espacos do comeco e do fim");
|
||||
TB_BLOCK_META("String.Repeat", "eval_repeat", "Repete o texto N vezes");
|
||||
TB_BLOCK_META("String.Reverse", "tb_string_reverse", "Texto de tras para frente");
|
||||
TB_BLOCK_META("String.Split", "eval_split", "Divide o texto em uma lista pelo separador");
|
||||
TB_BLOCK_META("String.Contains", "eval_contains", "Verdadeiro se o texto contem o trecho (ignora maiusculas)");
|
||||
TB_BLOCK_META("String.StartsWith", "eval_starts", "Verdadeiro se o texto comeca com o trecho");
|
||||
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)");
|
||||
|
||||
static const TinyBlocks_SlotDesc t1[] = {{"texto", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc t2[] = {{"texto", TB_VALUE_TEXT}, {"trecho", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc t3[] = {{"texto", TB_VALUE_TEXT}, {"trecho", TB_VALUE_TEXT}, {"por", TB_VALUE_TEXT}};
|
||||
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 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}};
|
||||
/* saidas terminadas em '?' = blocos booleanos (hexagonais no editor) */
|
||||
static const TinyBlocks_SlotDesc has_out[] = {{"contém?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc beg_out[] = {{"começa?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc end_out[] = {{"termina?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc num_out[] = {{"é número?", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc emp_out[] = {{"vazio?", TB_VALUE_INTEGER}};
|
||||
|
||||
return reg(rt, lib, "String.Upper", t1, 1, text_out, 1, eval_upper)
|
||||
&& reg(rt, lib, "String.Lower", t1, 1, text_out, 1, eval_lower)
|
||||
&& reg(rt, lib, "String.Sub", sub, 3, text_out, 1, eval_sub)
|
||||
&& reg(rt, lib, "String.Letter", tn, 2, text_out, 1, eval_letter)
|
||||
&& reg(rt, lib, "String.Length", t1, 1, int_out, 1, eval_length)
|
||||
&& reg(rt, lib, "String.Find", t2, 2, int_out, 1, eval_find)
|
||||
&& reg(rt, lib, "String.Replace", t3, 3, text_out, 1, eval_replace)
|
||||
&& reg(rt, lib, "String.Trim", t1, 1, text_out, 1, eval_trim)
|
||||
&& reg(rt, lib, "String.Repeat", tn, 2, text_out, 1, eval_repeat)
|
||||
&& reg(rt, lib, "String.Reverse", t1, 1, text_out, 1, eval_reverse)
|
||||
&& reg(rt, lib, "String.Split", split, 2, list_out, 1, eval_split)
|
||||
&& reg(rt, lib, "String.Contains", t2, 2, has_out, 1, eval_contains)
|
||||
&& 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);
|
||||
}
|
||||
|
||||
static void string_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(string,
|
||||
"tinyblocks-string",
|
||||
"1.0.0",
|
||||
string_init,
|
||||
string_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_string_get_library(void) {
|
||||
return &_tb_lib_desc_string;
|
||||
}
|
||||
Reference in new issue
Block a user