One commit, because the parts below only build together. Allocator: an interval needed at the current instruction is never evicted, and the allocated-program verifier checks that every operand sits where the allocation says. AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the shared ARM byte helpers, maps the function-address relocation, sign-extends signed 8- and 16-bit loads and rejects what it cannot encode with a named diagnostic. It has no AOT ELF writer yet. Native default with checked pointers and no fallback: on linux-x86_64, `ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon players run a bundle's verified program as native code in W^X memory. Stray and freed pointers, double free, bounds and read-only stores trap. A native gap is a named preflight error, nothing falls back to the engine, and `--backend engine` picks it explicitly. zx2native uses the same preflight. Native code exchanges module state for hot reload, checks the stack, zeroes fresh locals, spends fuel at the engine's checkpoints and returns records, arrays and strings from host calls. `zi2zx --debug` source origins name lines in native traps and `zx2native --emit map`. The host value API drops its VM prefix (HostValue, HOST_*). A call with a record result loads the hidden result address into rax only after its stack arguments are stored, and an indirect target stays in r10: rax could hold a stack argument (Kryon's text-field clip guard arrived as an address) or the target (indirect calls returning records crashed). Self-hosting ports: compiler_backend_process, compiler_backend_cache, compiler_native_names and compiler_runtime_emit replace the zir_c_build, zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile rules. zir_c.toolchain again reports "compiling OUT failed:" with the head of the compiler output and a truncation marker. The parallel scheduler no longer stalls on a held compiler after reaping a finished job, and poll or read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build) builds: generated files have pattern rules, objects wait for the headers they include, and engine flags are per object instead of leaking into zi2c. Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec tests, vec_capacity, the zx_metadata and native_cache_names benchmarks, build-android-host.sh and two shell wrappers are deleted; zx2native is machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the remaining-work plan record the current position. Co-Authored-By: Claude Opus 5.5 <[email protected]>
644 lines
23 KiB
C
644 lines
23 KiB
C
/* Generated by zi2c from text_tools.zi. */
|
|
#define ZIRAN_TEXT_TOOLS_H_SOURCE 1
|
|
#include <stdbool.h>
|
|
#include "zir_bounds.h"
|
|
#include "zir_string.h"
|
|
#include "zir_vec.h"
|
|
#include "text_tools.h"
|
|
|
|
int64_t
|
|
text_tools_TextFind(String text, String needle, int64_t from)
|
|
{
|
|
if (from < 0LL) {
|
|
return -1LL;
|
|
}
|
|
int64_t last = (int64_t)((uint64_t)((int64_t)(text).length) - (uint64_t)((int64_t)(needle).length));
|
|
int64_t at = from;
|
|
while (at <= last) {
|
|
int64_t index = 0LL;
|
|
while (index < (int64_t)(needle).length && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + (uint64_t)index))) == (uint8_t)ZIRAN_INDEX(needle.data, needle.length, index)) {
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
if (index == (int64_t)(needle).length) {
|
|
return at;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
int64_t
|
|
text_tools_TextFindLast(String text, String needle)
|
|
{
|
|
int64_t at = (int64_t)((uint64_t)((int64_t)(text).length) - (uint64_t)((int64_t)(needle).length));
|
|
while (at >= 0LL) {
|
|
int64_t index = 0LL;
|
|
while (index < (int64_t)(needle).length && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + (uint64_t)index))) == (uint8_t)ZIRAN_INDEX(needle.data, needle.length, index)) {
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
if (index == (int64_t)(needle).length) {
|
|
return at;
|
|
}
|
|
at = (int64_t)((uint64_t)at - UINT64_C(1));
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextContains(String text, String needle)
|
|
{
|
|
int64_t value_0 = text_tools_TextFind(text, needle, 0LL);
|
|
return value_0 >= 0LL;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextHasPrefix(String text, String prefix)
|
|
{
|
|
if ((int64_t)(prefix).length > (int64_t)(text).length) {
|
|
return false;
|
|
}
|
|
return StringEqual(StringRange(text, (int64_t)0LL, (int64_t)(int64_t)(prefix).length), prefix);
|
|
}
|
|
|
|
bool
|
|
text_tools_TextHasSuffix(String text, String suffix)
|
|
{
|
|
if ((int64_t)(suffix).length > (int64_t)(text).length) {
|
|
return false;
|
|
}
|
|
return StringEqual(StringRange(text, (int64_t)((int64_t)((uint64_t)((int64_t)(text).length) - (uint64_t)((int64_t)(suffix).length))), (int64_t)(int64_t)(text).length), suffix);
|
|
}
|
|
|
|
bool
|
|
text_tools_TextIsSpace(uint8_t byte)
|
|
{
|
|
return byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
|
|
String
|
|
text_tools_TextTrim(String text)
|
|
{
|
|
int64_t start = 0LL;
|
|
int64_t end = (int64_t)(text).length;
|
|
while (true) {
|
|
bool value_0 = start < end;
|
|
if (value_0) {
|
|
uint8_t value_1 = (uint8_t)ZIRAN_INDEX(text.data, text.length, start);
|
|
bool value_2 = false;
|
|
{
|
|
uint8_t byte = value_1;
|
|
value_2 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_0 = value_2;
|
|
}
|
|
if (!value_0) { break; }
|
|
start = (int64_t)((uint64_t)start + UINT64_C(1));
|
|
}
|
|
while (true) {
|
|
bool value_4 = end > start;
|
|
if (value_4) {
|
|
uint8_t value_5 = (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)end - UINT64_C(1))));
|
|
bool value_6 = false;
|
|
{
|
|
uint8_t byte = value_5;
|
|
value_6 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_4 = value_6;
|
|
}
|
|
if (!value_4) { break; }
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
return StringRange(text, (int64_t)start, (int64_t)end);
|
|
}
|
|
|
|
String
|
|
text_tools_TextTrimEnd(String text)
|
|
{
|
|
int64_t end = (int64_t)(text).length;
|
|
while (true) {
|
|
bool value_0 = end > 0LL;
|
|
if (value_0) {
|
|
uint8_t value_1 = (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)end - UINT64_C(1))));
|
|
bool value_2 = false;
|
|
{
|
|
uint8_t byte = value_1;
|
|
value_2 = byte == 32 || (byte >= 9 && byte <= 13);
|
|
}
|
|
value_0 = value_2;
|
|
}
|
|
if (!value_0) { break; }
|
|
end = (int64_t)((uint64_t)end - UINT64_C(1));
|
|
}
|
|
return StringRange(text, (int64_t)0LL, (int64_t)end);
|
|
}
|
|
|
|
__type_c53c2d7c4913dc02
|
|
text_tools_TextSplit(String text, String separator)
|
|
{
|
|
__type_c53c2d7c4913dc02 pieces = {0};
|
|
__type_c53c2d7c4913dc02* value_0 = &pieces;
|
|
if ((int64_t)(separator).length == 0LL) {
|
|
String value_1 = text;
|
|
bool value_2 = false;
|
|
void *value_3 = ZirVecGrow((void *)(pieces).data, &(pieces).capacity, (pieces).count, sizeof(*(pieces).data));
|
|
if (value_3 != NULL) {
|
|
(pieces).data = (String *)value_3;
|
|
(pieces).data[(pieces).count++] = value_1;
|
|
value_2 = true;
|
|
}
|
|
(void)value_2;
|
|
__type_c53c2d7c4913dc02 value_4 = pieces;
|
|
pieces.data = NULL;
|
|
pieces.capacity = 0;
|
|
pieces.count = 0;
|
|
__type_c53c2d7c4913dc02 value_5 = value_4;
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_5;
|
|
}
|
|
int64_t start = 0LL;
|
|
int64_t at = text_tools_TextFind(text, separator, 0LL);
|
|
while (at >= 0LL) {
|
|
String value_6 = StringRange(text, (int64_t)start, (int64_t)at);
|
|
bool value_7 = false;
|
|
void *value_8 = ZirVecGrow((void *)(pieces).data, &(pieces).capacity, (pieces).count, sizeof(*(pieces).data));
|
|
if (value_8 != NULL) {
|
|
(pieces).data = (String *)value_8;
|
|
(pieces).data[(pieces).count++] = value_6;
|
|
value_7 = true;
|
|
}
|
|
(void)value_7;
|
|
start = (int64_t)((uint64_t)at + (uint64_t)((int64_t)(separator).length));
|
|
at = text_tools_TextFind(text, separator, start);
|
|
}
|
|
String value_9 = StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length);
|
|
bool value_10 = false;
|
|
void *value_11 = ZirVecGrow((void *)(pieces).data, &(pieces).capacity, (pieces).count, sizeof(*(pieces).data));
|
|
if (value_11 != NULL) {
|
|
(pieces).data = (String *)value_11;
|
|
(pieces).data[(pieces).count++] = value_9;
|
|
value_10 = true;
|
|
}
|
|
(void)value_10;
|
|
__type_c53c2d7c4913dc02 value_12 = pieces;
|
|
pieces.data = NULL;
|
|
pieces.capacity = 0;
|
|
pieces.count = 0;
|
|
__type_c53c2d7c4913dc02 value_13 = value_12;
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_13;
|
|
}
|
|
|
|
__type_c53c2d7c4913dc02
|
|
text_tools_TextLines(String text)
|
|
{
|
|
__type_c53c2d7c4913dc02 lines = {0};
|
|
__type_c53c2d7c4913dc02* value_0 = &lines;
|
|
int64_t start = 0LL;
|
|
int64_t at = 0LL;
|
|
while (at < (int64_t)(text).length) {
|
|
uint8_t byte = (uint8_t)ZIRAN_INDEX(text.data, text.length, at);
|
|
if (byte == 10 || byte == 13) {
|
|
String value_1 = StringRange(text, (int64_t)start, (int64_t)at);
|
|
bool value_2 = false;
|
|
void *value_3 = ZirVecGrow((void *)(lines).data, &(lines).capacity, (lines).count, sizeof(*(lines).data));
|
|
if (value_3 != NULL) {
|
|
(lines).data = (String *)value_3;
|
|
(lines).data[(lines).count++] = value_1;
|
|
value_2 = true;
|
|
}
|
|
(void)value_2;
|
|
if (byte == 13 && (int64_t)((uint64_t)at + UINT64_C(1)) < (int64_t)(text).length && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + UINT64_C(1)))) == 10) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
start = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
if (start < (int64_t)(text).length) {
|
|
String value_4 = StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length);
|
|
bool value_5 = false;
|
|
void *value_6 = ZirVecGrow((void *)(lines).data, &(lines).capacity, (lines).count, sizeof(*(lines).data));
|
|
if (value_6 != NULL) {
|
|
(lines).data = (String *)value_6;
|
|
(lines).data[(lines).count++] = value_4;
|
|
value_5 = true;
|
|
}
|
|
(void)value_5;
|
|
}
|
|
__type_c53c2d7c4913dc02 value_7 = lines;
|
|
lines.data = NULL;
|
|
lines.capacity = 0;
|
|
lines.count = 0;
|
|
__type_c53c2d7c4913dc02 value_8 = value_7;
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_8;
|
|
}
|
|
|
|
Text
|
|
text_tools_TextReplace(String text, String old, String new)
|
|
{
|
|
if ((int64_t)(old).length == 0LL) {
|
|
return vec_TextCopy(text);
|
|
}
|
|
int64_t at = text_tools_TextFind(text, old, 0LL);
|
|
if (at < 0LL) {
|
|
return vec_TextCopy(text);
|
|
}
|
|
__type_838711075b90279a result = {0};
|
|
__type_838711075b90279a* value_0 = &result;
|
|
int64_t start = 0LL;
|
|
while (at >= 0LL) {
|
|
bool value_1 = false;
|
|
if ((StringRange(text, (int64_t)start, (int64_t)at)).length == 0) {
|
|
value_1 = true;
|
|
} else {
|
|
void *value_2 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(StringRange(text, (int64_t)start, (int64_t)at)).length, sizeof(*(result).data));
|
|
if (value_2 != NULL) {
|
|
(result).data = (uint8_t *)value_2;
|
|
memcpy((result).data + (result).count, (StringRange(text, (int64_t)start, (int64_t)at)).data, (size_t)(StringRange(text, (int64_t)start, (int64_t)at)).length);
|
|
(result).count += (int64_t)(StringRange(text, (int64_t)start, (int64_t)at)).length;
|
|
value_1 = true;
|
|
}
|
|
}
|
|
(void)value_1;
|
|
bool value_3 = false;
|
|
if ((new).length == 0) {
|
|
value_3 = true;
|
|
} else {
|
|
void *value_4 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(new).length, sizeof(*(result).data));
|
|
if (value_4 != NULL) {
|
|
(result).data = (uint8_t *)value_4;
|
|
memcpy((result).data + (result).count, (new).data, (size_t)(new).length);
|
|
(result).count += (int64_t)(new).length;
|
|
value_3 = true;
|
|
}
|
|
}
|
|
(void)value_3;
|
|
start = (int64_t)((uint64_t)at + (uint64_t)((int64_t)(old).length));
|
|
at = text_tools_TextFind(text, old, start);
|
|
}
|
|
bool value_5 = false;
|
|
if ((StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length)).length == 0) {
|
|
value_5 = true;
|
|
} else {
|
|
void *value_6 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length)).length, sizeof(*(result).data));
|
|
if (value_6 != NULL) {
|
|
(result).data = (uint8_t *)value_6;
|
|
memcpy((result).data + (result).count, (StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length)).data, (size_t)(StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length)).length);
|
|
(result).count += (int64_t)(StringRange(text, (int64_t)start, (int64_t)(int64_t)(text).length)).length;
|
|
value_5 = true;
|
|
}
|
|
}
|
|
(void)value_5;
|
|
__type_838711075b90279a value_7 = result;
|
|
result.data = NULL;
|
|
result.capacity = 0;
|
|
result.count = 0;
|
|
Text value_8 = vec_TextFromBuilder(value_7);
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_8;
|
|
}
|
|
|
|
Text
|
|
text_tools_TextJoin(Slice parts, String separator)
|
|
{
|
|
__type_838711075b90279a result = {0};
|
|
__type_838711075b90279a* value_0 = &result;
|
|
int64_t index = 0LL;
|
|
while (index < (int64_t)(parts).length) {
|
|
if (index > 0LL) {
|
|
bool value_1 = false;
|
|
if ((separator).length == 0) {
|
|
value_1 = true;
|
|
} else {
|
|
void *value_2 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(separator).length, sizeof(*(result).data));
|
|
if (value_2 != NULL) {
|
|
(result).data = (uint8_t *)value_2;
|
|
memcpy((result).data + (result).count, (separator).data, (size_t)(separator).length);
|
|
(result).count += (int64_t)(separator).length;
|
|
value_1 = true;
|
|
}
|
|
}
|
|
(void)value_1;
|
|
}
|
|
bool value_3 = false;
|
|
if ((((String *)parts.data)[index]).length == 0) {
|
|
value_3 = true;
|
|
} else {
|
|
void *value_4 = ZirVecReserve((void *)(result).data, &(result).capacity, (result).count, (int64_t)(((String *)parts.data)[index]).length, sizeof(*(result).data));
|
|
if (value_4 != NULL) {
|
|
(result).data = (uint8_t *)value_4;
|
|
memcpy((result).data + (result).count, (((String *)parts.data)[index]).data, (size_t)(((String *)parts.data)[index]).length);
|
|
(result).count += (int64_t)(((String *)parts.data)[index]).length;
|
|
value_3 = true;
|
|
}
|
|
}
|
|
(void)value_3;
|
|
index = (int64_t)((uint64_t)index + UINT64_C(1));
|
|
}
|
|
__type_838711075b90279a value_5 = result;
|
|
result.data = NULL;
|
|
result.capacity = 0;
|
|
result.count = 0;
|
|
Text value_6 = vec_TextFromBuilder(value_5);
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_6;
|
|
}
|
|
|
|
Text
|
|
text_tools_TextLower(String text)
|
|
{
|
|
__type_838711075b90279a result = {0};
|
|
__type_838711075b90279a* value_0 = &result;
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(text).length; it_index++) {
|
|
uint8_t it = (uint8_t)text.data[it_index];
|
|
uint8_t byte = it;
|
|
if (byte >= 65 && byte <= 90) {
|
|
byte = (uint8_t)((uint32_t)byte + 32u);
|
|
}
|
|
uint8_t value_1 = byte;
|
|
bool value_2 = false;
|
|
void *value_3 = ZirVecGrow((void *)(result).data, &(result).capacity, (result).count, sizeof(*(result).data));
|
|
if (value_3 != NULL) {
|
|
(result).data = (uint8_t *)value_3;
|
|
(result).data[(result).count++] = value_1;
|
|
value_2 = true;
|
|
}
|
|
(void)value_2;
|
|
}
|
|
__type_838711075b90279a value_4 = result;
|
|
result.data = NULL;
|
|
result.capacity = 0;
|
|
result.count = 0;
|
|
Text value_5 = vec_TextFromBuilder(value_4);
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_5;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextIsDecimal(String text)
|
|
{
|
|
if ((int64_t)(text).length == 0LL) {
|
|
return false;
|
|
}
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(text).length; it_index++) {
|
|
uint8_t it = (uint8_t)text.data[it_index];
|
|
if (it < 48 || it > 57) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextIsLowerHex(String text)
|
|
{
|
|
if ((int64_t)(text).length == 0LL) {
|
|
return false;
|
|
}
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(text).length; it_index++) {
|
|
uint8_t it = (uint8_t)text.data[it_index];
|
|
if (!((it >= 48 && it <= 57) || (it >= 97 && it <= 102))) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextOnly(String text, String allowed)
|
|
{
|
|
if ((int64_t)(text).length == 0LL) {
|
|
return false;
|
|
}
|
|
int64_t at = 0LL;
|
|
while (at < (int64_t)(text).length) {
|
|
int64_t value_0 = text_tools_TextFind(allowed, StringRange(text, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + UINT64_C(1)))), 0LL);
|
|
if (value_0 < 0LL) {
|
|
return false;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Results__s64__bool
|
|
text_tools_TextInteger(String text)
|
|
{
|
|
if ((int64_t)(text).length == 0LL) {
|
|
return (Results__s64__bool){.value_0 = 0LL, .value_1 = false};
|
|
}
|
|
bool negative = (uint8_t)text.data[0] == 45;
|
|
int64_t value_2 = 0;
|
|
if (negative || (uint8_t)text.data[0] == 43) {
|
|
value_2 = 1LL;
|
|
} else {
|
|
value_2 = 0LL;
|
|
}
|
|
int64_t start = value_2;
|
|
if (start == (int64_t)(text).length || (int64_t)((uint64_t)((int64_t)(text).length) - (uint64_t)start) > 18LL) {
|
|
return (Results__s64__bool){.value_0 = 0LL, .value_1 = false};
|
|
}
|
|
int64_t value = 0LL;
|
|
int64_t at = start;
|
|
while (at < (int64_t)(text).length) {
|
|
uint8_t byte = (uint8_t)text.data[at];
|
|
if (byte < 48 || byte > 57) {
|
|
return (Results__s64__bool){.value_0 = 0LL, .value_1 = false};
|
|
}
|
|
value = (int64_t)(((uint64_t)value * UINT64_C(10)) + (uint64_t)(((uint8_t)((uint32_t)byte - 48u))));
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
int64_t value_3 = 0;
|
|
if (negative) {
|
|
value_3 = (int64_t)(UINT64_C(0) - (uint64_t)value);
|
|
} else {
|
|
value_3 = value;
|
|
}
|
|
return (Results__s64__bool){.value_0 = value_3, .value_1 = true};
|
|
}
|
|
|
|
int32_t
|
|
text_tools_TextHexDigit(uint8_t byte)
|
|
{
|
|
if (byte >= 48 && byte <= 57) {
|
|
return (int32_t)(((uint8_t)((uint32_t)byte - 48u)));
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
return (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 97u))) + 10u);
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
return (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 65u))) + 10u);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool
|
|
text_tools_TextHexDecodeInto(String value, __type_838711075b90279a* output, int64_t max_bytes)
|
|
{
|
|
bool value_0 = max_bytes < 0LL;
|
|
if (!value_0) {
|
|
int64_t value_1 = (int64_t)(value).length % 2LL;
|
|
value_0 = (value_1 != 0LL);
|
|
}
|
|
if (value_0) {
|
|
return false;
|
|
}
|
|
int64_t decoded_count = (int64_t)(value).length / 2LL;
|
|
if (decoded_count > max_bytes) {
|
|
return false;
|
|
}
|
|
__type_838711075b90279a decoded = {0};
|
|
__type_838711075b90279a* value_2 = &decoded;
|
|
int64_t at = 0LL;
|
|
while (at < (int64_t)(value).length) {
|
|
uint8_t value_3 = (uint8_t)ZIRAN_INDEX(value.data, value.length, at);
|
|
int32_t value_4 = 0;
|
|
{
|
|
uint8_t byte = value_3;
|
|
if (byte >= 48 && byte <= 57) {
|
|
value_4 = (int32_t)(((uint8_t)((uint32_t)byte - 48u)));
|
|
goto value_5;
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
value_4 = (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 97u))) + 10u);
|
|
goto value_5;
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
value_4 = (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 65u))) + 10u);
|
|
goto value_5;
|
|
}
|
|
value_4 = -1;
|
|
value_5: ;
|
|
}
|
|
int32_t high = value_4;
|
|
uint8_t value_6 = (uint8_t)ZIRAN_INDEX(value.data, value.length, ((int64_t)((uint64_t)at + UINT64_C(1))));
|
|
int32_t value_7 = 0;
|
|
{
|
|
uint8_t byte = value_6;
|
|
if (byte >= 48 && byte <= 57) {
|
|
value_7 = (int32_t)(((uint8_t)((uint32_t)byte - 48u)));
|
|
goto value_8;
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
value_7 = (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 97u))) + 10u);
|
|
goto value_8;
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
value_7 = (int32_t)((uint32_t)(((uint8_t)((uint32_t)byte - 65u))) + 10u);
|
|
goto value_8;
|
|
}
|
|
value_7 = -1;
|
|
value_8: ;
|
|
}
|
|
int32_t low = value_7;
|
|
bool value_9 = high < 0 || low < 0;
|
|
if (!value_9) {
|
|
uint8_t value_10 = (uint8_t)(((int32_t)(((uint32_t)high * 16u) + (uint32_t)low)));
|
|
bool value_11 = false;
|
|
void *value_12 = ZirVecGrow((void *)(decoded).data, &(decoded).capacity, (decoded).count, sizeof(*(decoded).data));
|
|
if (value_12 != NULL) {
|
|
(decoded).data = (uint8_t *)value_12;
|
|
(decoded).data[(decoded).count++] = value_10;
|
|
value_11 = true;
|
|
}
|
|
value_9 = !value_11;
|
|
}
|
|
if (value_9) {
|
|
free((decoded).data);
|
|
(decoded).data = NULL;
|
|
decoded.capacity = 0;
|
|
decoded.count = 0;
|
|
bool value_13 = false;
|
|
free((*value_2).data);
|
|
(*value_2).data = NULL;
|
|
(*value_2).capacity = 0;
|
|
(*value_2).count = 0;
|
|
return value_13;
|
|
}
|
|
at = (int64_t)((uint64_t)at + UINT64_C(2));
|
|
}
|
|
Slice value_14 = {(decoded).data, (decoded).count};
|
|
Slice value_15 = SliceRange(value_14, 0LL, decoded.count, sizeof(*(decoded).data));
|
|
bool value_16 = false;
|
|
if ((StringView((const char *)(value_15).data, (size_t)(value_15).length)).length == 0) {
|
|
value_16 = true;
|
|
} else {
|
|
void *value_17 = ZirVecReserve((void *)((*(output))).data, &((*(output))).capacity, ((*(output))).count, (int64_t)(StringView((const char *)(value_15).data, (size_t)(value_15).length)).length, sizeof(*((*(output))).data));
|
|
if (value_17 != NULL) {
|
|
((*(output))).data = (uint8_t *)value_17;
|
|
memcpy(((*(output))).data + ((*(output))).count, (StringView((const char *)(value_15).data, (size_t)(value_15).length)).data, (size_t)(StringView((const char *)(value_15).data, (size_t)(value_15).length)).length);
|
|
((*(output))).count += (int64_t)(StringView((const char *)(value_15).data, (size_t)(value_15).length)).length;
|
|
value_16 = true;
|
|
}
|
|
}
|
|
bool appended = value_16;
|
|
free((decoded).data);
|
|
(decoded).data = NULL;
|
|
decoded.capacity = 0;
|
|
decoded.count = 0;
|
|
bool value_18 = appended;
|
|
free((*value_2).data);
|
|
(*value_2).data = NULL;
|
|
(*value_2).capacity = 0;
|
|
(*value_2).count = 0;
|
|
return value_18;
|
|
}
|
|
|
|
Text
|
|
text_tools_TextHex(Slice bytes)
|
|
{
|
|
String digits = StringLiteral("0123456789abcdef");
|
|
__type_838711075b90279a result = {0};
|
|
__type_838711075b90279a* value_0 = &result;
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(bytes).length; it_index++) {
|
|
uint8_t it = ((uint8_t *)bytes.data)[it_index];
|
|
uint8_t value_1 = (uint8_t)ZIRAN_INDEX(digits.data, digits.length, ((uint8_t)(it >> 4)));
|
|
bool value_2 = false;
|
|
void *value_3 = ZirVecGrow((void *)(result).data, &(result).capacity, (result).count, sizeof(*(result).data));
|
|
if (value_3 != NULL) {
|
|
(result).data = (uint8_t *)value_3;
|
|
(result).data[(result).count++] = value_1;
|
|
value_2 = true;
|
|
}
|
|
(void)value_2;
|
|
uint8_t value_4 = (uint8_t)ZIRAN_INDEX(digits.data, digits.length, ((uint8_t)((uint32_t)it & 15u)));
|
|
bool value_5 = false;
|
|
void *value_6 = ZirVecGrow((void *)(result).data, &(result).capacity, (result).count, sizeof(*(result).data));
|
|
if (value_6 != NULL) {
|
|
(result).data = (uint8_t *)value_6;
|
|
(result).data[(result).count++] = value_4;
|
|
value_5 = true;
|
|
}
|
|
(void)value_5;
|
|
}
|
|
__type_838711075b90279a value_7 = result;
|
|
result.data = NULL;
|
|
result.capacity = 0;
|
|
result.count = 0;
|
|
Text value_8 = vec_TextFromBuilder(value_7);
|
|
free((*value_0).data);
|
|
(*value_0).data = NULL;
|
|
(*value_0).capacity = 0;
|
|
(*value_0).count = 0;
|
|
return value_8;
|
|
}
|