"lista [nome]" copiava a lista inteira a cada leitura e adicione / insira /
apague / substitua reconstruíam a lista a cada operação: um Z-buffer ou
qualquer lista grande ficava O(n) por acesso. Agora a lista com nome é um
objeto mutável, como no Scratch ("mude a lista para" continua copiando).
O JSON do core ganha tb_json_array_set / insert / remove / clear.
20 mil leituras + escritas numa lista de 10 mil: 13 ms (test-stdlib).
"cor da imagem" (Image e PPM) vazava os números de cada pixel lido.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
3.9 KiB
C
78 lines
3.9 KiB
C
/*
|
|
* TinyBlocks - tb_json_block.h
|
|
* TinyBlocks_JsonValue: árvore JSON imutável e ref-counted que flui como valor
|
|
* entre blocos (TB_VALUE_JSON).
|
|
*
|
|
* Fluxo típico de uso:
|
|
* TinyBlocks_JsonValue* root = tb_json_parse(text, NULL, 0);
|
|
* TinyBlocks_JsonValue* name = tb_json_object_get(root, "name"); // ref nova
|
|
* const char* s = tb_json_as_string(name);
|
|
* tb_json_release(name);
|
|
* tb_json_release(root);
|
|
*
|
|
* Mutações (array_push/object_set) são feitas em árvores recém-criadas (não
|
|
* compartilhadas); para "modificar" um valor já em uso, clone primeiro com
|
|
* tb_json_clone.
|
|
*/
|
|
#ifndef TINYBLOCKS_JSON_BLOCK_H
|
|
#define TINYBLOCKS_JSON_BLOCK_H
|
|
|
|
#include "tb_block.h"
|
|
|
|
typedef enum TinyBlocks_JsonKind {
|
|
TB_JSON_NULL = 0,
|
|
TB_JSON_BOOL = 1,
|
|
TB_JSON_NUMBER = 2,
|
|
TB_JSON_STRING = 3,
|
|
TB_JSON_ARRAY = 4,
|
|
TB_JSON_OBJECT = 5,
|
|
} TinyBlocks_JsonKind;
|
|
|
|
/* -- Ciclo de vida --- */
|
|
TB_API TinyBlocks_JsonValue* tb_json_null (void);
|
|
TB_API TinyBlocks_JsonValue* tb_json_bool (bool b);
|
|
TB_API TinyBlocks_JsonValue* tb_json_number(double d);
|
|
TB_API TinyBlocks_JsonValue* tb_json_string(const char* s); /* copia a string */
|
|
TB_API TinyBlocks_JsonValue* tb_json_array (void); /* array vazio */
|
|
TB_API TinyBlocks_JsonValue* tb_json_object(void); /* objeto vazio */
|
|
|
|
TB_API void tb_json_retain (TinyBlocks_JsonValue* j);
|
|
TB_API void tb_json_release(TinyBlocks_JsonValue* j);
|
|
TB_API TinyBlocks_JsonValue* tb_json_clone (const TinyBlocks_JsonValue* j); /* deep copy */
|
|
|
|
/* -- Inspeção --- */
|
|
TB_API TinyBlocks_JsonKind tb_json_kind (const TinyBlocks_JsonValue* j);
|
|
TB_API const char* tb_json_kind_name(const TinyBlocks_JsonValue* j);
|
|
|
|
TB_API bool tb_json_as_bool (const TinyBlocks_JsonValue* j, bool def);
|
|
TB_API double tb_json_as_number(const TinyBlocks_JsonValue* j, double def);
|
|
TB_API const char* tb_json_as_string(const TinyBlocks_JsonValue* j); /* NULL se não for string */
|
|
|
|
/* -- Arrays (mutação in-place; use em árvores não compartilhadas) --- */
|
|
TB_API bool tb_json_array_push (TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item);
|
|
TB_API uint32_t tb_json_array_length(const TinyBlocks_JsonValue* arr);
|
|
TB_API TinyBlocks_JsonValue* tb_json_array_get(const TinyBlocks_JsonValue* arr, uint32_t idx); /* ref nova, ou NULL */
|
|
/* Troca / insere (idx == length acrescenta) / remove o item idx; retem item */
|
|
TB_API bool tb_json_array_set (TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item);
|
|
TB_API bool tb_json_array_insert(TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item);
|
|
TB_API bool tb_json_array_remove(TinyBlocks_JsonValue* arr, uint32_t idx);
|
|
TB_API void tb_json_array_clear (TinyBlocks_JsonValue* arr);
|
|
|
|
/* -- Objetos (mutação in-place; use em árvores não compartilhadas) --- */
|
|
TB_API bool tb_json_object_set (TinyBlocks_JsonValue* obj, const char* key, TinyBlocks_JsonValue* val);
|
|
TB_API uint32_t tb_json_object_length (const TinyBlocks_JsonValue* obj);
|
|
TB_API const char* tb_json_object_key_at (const TinyBlocks_JsonValue* obj, uint32_t idx);
|
|
TB_API TinyBlocks_JsonValue* tb_json_object_get(const TinyBlocks_JsonValue* obj, const char* key); /* ref nova, ou NULL */
|
|
|
|
/* -- Parsing / serialização --- */
|
|
/* Retorna NULL em erro de sintaxe; se errbuf não for NULL, escreve a mensagem. */
|
|
TB_API TinyBlocks_JsonValue* tb_json_parse(const char* text, char* errbuf, size_t errbuf_size);
|
|
/* Serializa para texto; retorna buffer heap-alocado (free() do chamador). */
|
|
TB_API char* tb_json_stringify(const TinyBlocks_JsonValue* j);
|
|
|
|
/* -- Helpers para TinyBlocks_Value --- */
|
|
TB_API TinyBlocks_Value tb_value_json (TinyBlocks_JsonValue* j); /* retém ref */
|
|
TB_API TinyBlocks_JsonValue* tb_value_as_json(TinyBlocks_Value v);
|
|
|
|
#endif /* TINYBLOCKS_JSON_BLOCK_H */
|