all repos — hex @ 21839893b924b6e966f1c66ceb15f6ea37294ead

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

String processing
h3rald h3rald@h3rald.com
Fri, 03 Jan 2025 08:10:33 +0100
commit

21839893b924b6e966f1c66ceb15f6ea37294ead

parent

1db6f7bad71e5e8d6765cc6acc8537d868f8b8b1

2 files changed, 49 insertions(+), 3 deletions(-)

jump to
M src/hex.hsrc/hex.h

@@ -309,6 +309,7 @@ char *hex_bytes_to_string(const uint8_t *bytes, size_t size);

char *hex_process_string(const char *value); size_t hex_min_bytes_to_encode_integer(int32_t value); char *hex_unescape_string(const char *input); +char* hex_normalize_newlines(const char* input); // Native symbols int hex_symbol_store(hex_context_t *ctx);
M src/utils.csrc/utils.c

@@ -307,7 +307,8 @@ *ptr = '\0';

return str; } -char* hex_process_string(const char* input) { +char* hex_process_string(const char* input) +{ size_t len = strlen(input); size_t new_len = 0;

@@ -372,7 +373,8 @@

return 4; // Default to 4 bytes if no smaller size is found. } -char *hex_unescape_string(const char *input) { +char *hex_unescape_string(const char *input) +{ size_t len = strlen(input); char* unescaped = (char*)malloc(len + 1);

@@ -400,6 +402,49 @@ unescaped[j] = '\0'; // Null-terminate the string

return unescaped; } -// Normalize newlines f +char* hex_normalize_newlines(const char* input) +{ + size_t len = strlen(input); + size_t new_len = 0; + + // First pass: Calculate the length of normalized string + for (size_t i = 0; i < len; i++) { + if (input[i] == '\r') { + if (i + 1 < len && input[i + 1] == '\n') { + // Windows-style CRLF -> LF + new_len++; + i++; + } else { + // Mac-style CR -> LF + new_len++; + } + } else { + new_len++; + } + } + + // Allocate memory for the normalized string + char* normalized = (char*)malloc(new_len + 1); + if (!normalized) { + fprintf(stderr, "Memory allocation failed.\n"); + exit(EXIT_FAILURE); + } + + // Second pass: Build the normalized string + size_t j = 0; + for (size_t i = 0; i < len; i++) { + if (input[i] == '\r') { + if (i + 1 < len && input[i + 1] == '\n') { + normalized[j++] = '\n'; + i++; + } else { + normalized[j++] = '\n'; + } + } else { + normalized[j++] = input[i]; + } + } + normalized[j] = '\0'; // Null-terminate the string + return normalized; }