Compare commits

...

8 Commits

Author SHA1 Message Date
715a2cd82e temporarely disabled variable size reading 2025-02-05 17:26:01 +05:00
3fd45311c5 constant data names linking 2025-02-05 17:25:12 +05:00
b1b20d336d compile binary 2025-02-05 12:08:40 +05:00
53ca7e1b49 writeBinaryFile 2025-02-04 13:31:19 +05:00
fe6d690251 registers.h 2025-02-04 10:45:37 +05:00
51ef24bb53 fixed memory issues 2025-02-03 22:30:56 +05:00
422d967165 toUpper, toLower 2025-02-03 22:30:43 +05:00
5d275c8dd1 added compatibility with linux 2025-02-03 22:30:13 +05:00
23 changed files with 442 additions and 44 deletions

View File

@@ -77,7 +77,9 @@ i32 VM_boot(VM* vm){
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
if(pos + size >= vm->data_size){
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable",
VM_setError(vm,
"can't read " IFWIN("%lli", "%li") " bytes from 0x%x, because only "
IFWIN("%lli", "%li") " are avaliable",
size, (u32)pos, vm->data_size - size);
return false;
}

View File

@@ -30,17 +30,22 @@
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
/* branchless version of max(max_len, __List_min_size) */\
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
return List_##T##_construct((T*)malloc(initial_len * sizeof(T)), 0, max_len);\
return List_##T##_construct((T*)malloc(max_len * sizeof(T)), 0, max_len);\
}\
\
T* List_##T##_expand(List_##T* ptr, u32 count){\
u32 occupied_len = ptr->len;\
u32 expanded_max_len = ptr->max_len;\
expanded_max_len += (expanded_max_len < __List_min_size) * (__List_min_size - expanded_max_len);\
ptr->len += count;\
while(ptr->len > ptr->max_len){\
while(ptr->len > expanded_max_len){\
expanded_max_len *= 2;\
}\
ptr->data = (T*)realloc(ptr->data, expanded_max_len * sizeof(T));\
u32 alloc_size = expanded_max_len * sizeof(T);\
if(ptr->data == NULL)\
ptr->data = (T*)malloc(alloc_size);\
else ptr->data = (T*)realloc(ptr->data, alloc_size);\
ptr->max_len = expanded_max_len;\
return ptr->data + occupied_len;\
}\
\

View File

@@ -20,18 +20,6 @@ str ArgumentType_toString(ArgumentType t){
return _ArgumentType_str[t];
}
RegisterCode RegisterCode_parse(str r){
if(str_equals(r, STR("ax")))
return RegisterCode_ax;
if(str_equals(r, STR("bx")))
return RegisterCode_bx;
if(str_equals(r, STR("cx")))
return RegisterCode_cx;
if(str_equals(r, STR("dx")))
return RegisterCode_dx;
return RegisterCode_Unset;
}
void Section_init(Section* sec, str name){
sec->name = name;
@@ -40,7 +28,14 @@ void Section_init(Section* sec, str name){
}
void Section_free(Section* sec){
for(u32 i = 0; i < sec->data.len; i++){
free(sec->data.data[i].data.data);
}
free(sec->data.data);
for(u32 i = 0; i < sec->code.len; i++){
free(sec->code.data[i].args.data);
}
free(sec->code.data);
}

View File

@@ -2,6 +2,7 @@
#include "../std.h"
#include "../string/str.h"
#include "../instructions/instructions.h"
#include "../instructions/registers.h"
#include "../collections/List.h"
typedef enum ArgumentType {
@@ -15,16 +16,6 @@ typedef enum ArgumentType {
str ArgumentType_toString(ArgumentType t);
typedef enum RegisterCode {
RegisterCode_Unset,
RegisterCode_ax,
RegisterCode_bx,
RegisterCode_cx,
RegisterCode_dx
} RegisterCode;
RegisterCode RegisterCode_parse(str register_name);
typedef struct Argument {
ArgumentType type;
union {

40
src/compiler/Binary.c Normal file
View File

@@ -0,0 +1,40 @@
#include "Binary.h"
List_define(ConstDataProps);
HashMap_define(ConstDataProps, HashMap_DESTROY_VALUE_FUNC_NULL);
List_define(NamedRef);
List_define(CompiledSection);
HashMap_define(CompiledSectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
void CompiledSection_construct(CompiledSection* ptr, str name){
ptr->name = name;
ptr->next = NULL;
ptr->offset = 0;
ptr->const_data_props_list = List_ConstDataProps_construct(NULL, 0, 0);
ptr->named_refs = List_NamedRef_construct(NULL, 0, 0);
ptr->bytes = List_u8_alloc(64);
}
void CompiledSection_free(CompiledSection* ptr){
free(ptr->const_data_props_list.data);
free(ptr->named_refs.data);
free(ptr->bytes.data);
}
void BinaryObject_construct(BinaryObject* ptr){
ptr->section_list = List_CompiledSection_alloc(64);
HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
}
void BinaryObject_free(BinaryObject* ptr){
for(u32 i = 0; i < ptr->section_list.len; i++){
CompiledSection_free(&ptr->section_list.data[i]);
}
free(ptr->section_list.data);
HashMap_CompiledSectionPtr_free(&ptr->section_map);
HashMap_ConstDataProps_free(&ptr->const_data_map);
}

65
src/compiler/Binary.h Normal file
View File

@@ -0,0 +1,65 @@
#pragma once
#include "../std.h"
#include "../string/str.h"
#include "../instructions/instructions.h"
#include "../instructions/registers.h"
#include "../collections/List.h"
#include "../collections/HashMap.h"
#include "AST.h"
typedef struct CompiledSection CompiledSection;
typedef struct ConstDataProps {
str name;
u32 size; // size in bytes
u32 offset; // offset in bytes from section start
} ConstDataProps;
#define ConstDataProps_construct(NAME, SIZE, OFFSET) ((ConstDataProps){ .name = NAME, .size = SIZE, .offset = OFFSET})
List_declare(ConstDataProps);
HashMap_declare(ConstDataProps);
typedef enum NamedRefType {
NamedRefType_Unset,
NamedRefType_Ptr,
NamedRefType_Size,
} NamedRefType;
typedef struct NamedRef {
str name;
NamedRefType type;
u32 offset; // offset in bytes from section start
} NamedRef;
#define NamedRef_construct(NAME, TYPE, OFFSET) ((NamedRef){ .name = NAME, .type = TYPE, .offset = OFFSET})
List_declare(NamedRef);
typedef struct CompiledSection {
str name;
CompiledSection* next;
u32 offset;
List_ConstDataProps const_data_props_list;
List_NamedRef named_refs;
List_u8 bytes;
} CompiledSection;
void CompiledSection_construct(CompiledSection* ptr, str name);
void CompiledSection_free(CompiledSection* ptr);
List_declare(CompiledSection);
typedef CompiledSection* CompiledSectionPtr;
HashMap_declare(CompiledSectionPtr);
typedef struct BinaryObject {
List_CompiledSection section_list;
HashMap_CompiledSectionPtr section_map;
HashMap_ConstDataProps const_data_map;
u32 total_size;
} BinaryObject;
void BinaryObject_construct(BinaryObject* ptr);
void BinaryObject_free(BinaryObject* ptr);

View File

@@ -1,11 +1,14 @@
#include "Compiler_internal.h"
HashMap_define(SectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
void Compiler_init(Compiler* cmp){
memset(cmp, 0, sizeof(Compiler));
cmp->state = CompilerState_Initial;
cmp->tokens = List_Token_alloc(4096);
cmp->line_lengths = List_u32_alloc(1024);
AST_init(&cmp->ast);
BinaryObject_construct(&cmp->binary);
}
void Compiler_free(Compiler* cmp){
@@ -13,6 +16,7 @@ void Compiler_free(Compiler* cmp){
free(cmp->tokens.data);
free(cmp->line_lengths.data);
AST_free(&cmp->ast);
BinaryObject_free(&cmp->binary);
}
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
@@ -60,10 +64,156 @@ str Compiler_constructTokenStr(Compiler* cmp, Token t){
return s;
}
static bool compileFile(Compiler* cmp, FILE* f){
static bool compileSection(Compiler* cmp, Section* sec){
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
CompiledSection_construct(cs, sec->name);
if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){
returnError("duplicate section '%s'", str_copy(sec->name));
}
// compile code
u8 zeroes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
for(u32 i = 0; i < sec->code.len; i++){
Operation* op = &sec->code.data[i];
List_u8_pushMany(&cs->bytes, (void*)&op->opcode, sizeof(op->opcode));
for(u32 j = 0; j < op->args.len; j++){
Argument* arg = &op->args.data[j];
switch(arg->type){
case ArgumentType_VarDataName:
returnError("argument type 'VarDataName' is not supported yet");
case ArgumentType_Unset:
returnError("ArgumentType is not set");
default:
returnError("invalid ArgumentType %i", arg->type);
case ArgumentType_Register:
List_u8_push(&cs->bytes, arg->value.register_code);
break;
case ArgumentType_ConstValue:
//TODO: add const value size parsing
List_u8_pushMany(&cs->bytes, (void*)&arg->value.i, 4);
break;
case ArgumentType_ConstDataPointer:
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
arg->value.data_name,
NamedRefType_Ptr,
cs->bytes.len));
List_u8_pushMany(&cs->bytes, zeroes, 4);
break;
case ArgumentType_ConstDataSize:
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
arg->value.data_name,
NamedRefType_Size,
cs->bytes.len));
List_u8_pushMany(&cs->bytes, zeroes, 4);
break;
}
}
}
// compile data
for(u32 i = 0; i < sec->data.len; i++){
DataDefinition* dd = &sec->data.data[i];
List_ConstDataProps_push(&cs->const_data_props_list, ConstDataProps_construct(dd->name, dd->data.len, cs->bytes.len));
List_u8_pushMany(&cs->bytes, dd->data.data, dd->data.len);
}
// TODO: push padding
return true;
}
static bool compileBinary(Compiler* cmp){
for(u32 i = 0; i < cmp->ast.sections.len; i++){
SectionPtr sec = &cmp->ast.sections.data[i];
if(!compileSection(cmp, sec)){
return false;
}
}
// find main section
str main_sec_name = STR("main");
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, main_sec_name);
if(main_sec_ptrptr == NULL){
returnError("no 'main' section was defined");
}
// create linked list of CompiledSection where main is the first
CompiledSection* prev_sec = *main_sec_ptrptr;
u32 total_size = 0;
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
CompiledSection* sec = &cmp->binary.section_list.data[i];
total_size += sec->bytes.len;
if(str_equals(sec->name, main_sec_name))
continue;
prev_sec->next = sec;
sec->offset = prev_sec->offset + prev_sec->bytes.len;
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
returnError("duplicate named data '%s'", str_copy(cd.name).data);
}
for(u32 j = 0; j < sec->const_data_props_list.len; j++){
cd = sec->const_data_props_list.data[j];
cd.offset += sec->offset;
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
returnError("duplicate named data '%s'", str_copy(cd.name).data);
}
}
}
// insert calculated offsets into sections
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
CompiledSection* sec = &cmp->binary.section_list.data[i];
for(u32 j = 0; j < sec->named_refs.len; j++){
NamedRef* ref = &sec->named_refs.data[j];
ConstDataProps* target_data = HashMap_ConstDataProps_tryGetPtr(
&cmp->binary.const_data_map, ref->name);
if(target_data == NULL){
returnError("can't find named data '%s'", str_copy(ref->name).data);
}
void* ref_value_ptr = sec->bytes.data + ref->offset;
switch(ref->type){
default:
returnError("invalid NamedRefType %i", ref->type);
case NamedRefType_Size:
*((u32*)ref_value_ptr) = target_data->size;
break;
case NamedRefType_Ptr:
*((u32*)ref_value_ptr) = target_data->offset;
break;
}
}
}
cmp->binary.total_size = total_size;
return true;
}
static bool writeBinaryFile(Compiler* cmp, FILE* f){
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
cmp->state = CompilerState_Compiling;
if(!compileBinary(cmp)){
return false;
}
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, STR("main"));
if(main_sec_ptrptr == NULL){
returnError("no 'main' section was defined");
}
CompiledSection* sec = *main_sec_ptrptr;
while(sec){
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
sec = sec->next;
}
//TODO: print warnings for unused sections
return true;
}
@@ -98,17 +248,21 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
}
if(debug_log){
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
printf("===========================[%s]===========================\n", source_file_name);
fputs(cmp->code.data, stdout);
fputc('\n', stdout);
}
if(debug_log)
printf("===================================[lexing]===================================\n");
bool success = Compiler_lex(cmp);
if(debug_log){
printf("------------------------------------[lines]-----------------------------------\n");
printf("------------------------------------[lines]------------------------------------\n");
for(u32 i = 0; i < cmp->line_lengths.len; i++){
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
}
printf("------------------------------------[tokens]-----------------------------------\n");
for(u32 i = 0; i < cmp->tokens.len; i++){
Token t = cmp->tokens.data[i];
@@ -126,18 +280,82 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
free(tokstr);
}
}
if(!success){
fclose(f);
return false;
}
if(debug_log)
printf("===================================[parsing]===================================\n");
success = Compiler_parse(cmp);
if (debug_log){
printf("-------------------------------------[AST]-------------------------------------\n");
for(u32 i = 0; i < cmp->ast.sections.len; i++){
Section* sec = &cmp->ast.sections.data[i];
str tmpstr = str_copy(sec->name);
printf("section '%s'\n", tmpstr.data);
free(tmpstr.data);
for(u32 j = 0; j < sec->data.len; j++){
DataDefinition* dd = &sec->data.data[j];
tmpstr = str_copy(dd->name);
printf(" const%u %s (len %u)\n", dd->element_size * 8, tmpstr.data, dd->data.len/dd->element_size);
free(tmpstr.data);
}
for(u32 j = 0; j < sec->code.len; j++){
Operation* op = &sec->code.data[j];
const Instruction* instr = Instruction_getByOpcode(op->opcode);
printf(" %s", instr->name.data);
for(u32 k = 0; k < op->args.len; k++){
Argument* arg = &op->args.data[k];
printf(" %s(", ArgumentType_toString(arg->type).data);
switch(arg->type){
default:
fclose(f);
returnError("invalid argument type %i", arg->type);
case ArgumentType_Register:
const char* register_names[] = {"null", "ax", "bx", "cx", "dx"};
printf("%s", register_names[arg->value.register_code]);
break;
case ArgumentType_ConstValue:
printf(IFWIN("%lli", "%li"), arg->value.i);
break;
case ArgumentType_ConstDataPointer:
tmpstr = str_copy(arg->value.data_name);
printf("@%s", tmpstr.data);
free(tmpstr.data);
break;
case ArgumentType_ConstDataSize:
tmpstr = str_copy(arg->value.data_name);
printf("#%s", tmpstr.data);
free(tmpstr.data);
break;
case ArgumentType_VarDataName:
tmpstr = str_copy(arg->value.data_name);
printf("%s", tmpstr.data);
free(tmpstr.data);
break;
}
printf(")");
}
printf("\n");
}
}
}
if(!success){
fclose(f);
return false;
}
success = compileFile(cmp, f);
if(debug_log)
printf("==================================[compiling]==================================\n");
success = writeBinaryFile(cmp, f);
fclose(f);
if(success){
cmp->state = CompilerState_Success;

View File

@@ -2,8 +2,9 @@
#include "../std.h"
#include "../string/str.h"
#include "../collections/List.h"
#include "../collections/HashMap.h"
#include "Token.h"
#include "AST.h"
#include "Binary.h"
typedef enum CompilerState {
CompilerState_Initial,
@@ -14,16 +15,24 @@ typedef enum CompilerState {
CompilerState_Success
} CompilerState;
typedef Section* SectionPtr;
HashMap_declare(SectionPtr);
typedef struct Compiler {
/* general fields */
str code;
u32 column; // > 0 if code parsing started
u32 pos;
CompilerState state;
NULLABLE(char* error_message);
/* lexer fields */
List_Token tokens;
List_u32 line_lengths;
/* parser fields */
AST ast;
u32 tok_i;
/* compiler fields */
BinaryObject binary;
} Compiler;
void Compiler_init(Compiler* cmp);

View File

@@ -89,6 +89,7 @@ static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* d
}
free(_instr_name_zero_terminated.data);
ddf->element_size = _element_size_bits / 8;
ddf->data = List_u8_alloc(32);
Token tok = cmp->tokens.data[++cmp->tok_i];
if(tok.type != TokenType_Name){
@@ -165,6 +166,7 @@ static void parseOperation(Compiler* cmp, str instr_name, Operation* operPtr){
}
operPtr->opcode = instr->opcode;
operPtr->args = List_Argument_alloc(8);
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
str tok_str = str_null;
str processed_str = str_null;

View File

@@ -43,7 +43,7 @@ char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
char* buf = malloc(buffer_size);
int r = vsprintf_s(buf, buffer_size, format, argv);
int r = vsprintf(buf, format, argv);
if(r < 0){
free(buf);
return NULL;

View File

@@ -4,7 +4,7 @@
i32 MOV_impl(VM* vm){
u8 dst_register_i = 0;
readRegisterVar(dst_register_i);
u8 src_register_i = 0;
u8 src_register_i = 0;
readRegisterVar(src_register_i);
if(dst_register_i == src_register_i){
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);

View File

@@ -4,8 +4,9 @@
i32 PUSH_impl(VM* vm){
u8 dst_register_i = 0;
readRegisterVar(dst_register_i);
u8 value_size = 0;
readValueSizeVar(value_size);
/*u8 value_size = 0;
readValueSizeVar(value_size);*/
u8 value_size = 4;\
vm->registers[dst_register_i].u32v = 0;
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))

View File

@@ -1,5 +1,6 @@
#pragma once
#include "../instructions.h"
#include "../registers.h"
#define readVar(VAR) {\
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
@@ -8,7 +9,7 @@
}
#define validateRegisterIndex(VAR) {\
if(VAR > sizeof(vm->registers)){\
if(VAR> sizeof(vm->registers)){\
VM_setError(vm, "invalid register index (%x)", VAR);\
return -1;\
}\
@@ -16,9 +17,11 @@
#define readRegisterVar(VAR) {\
readVar(VAR);\
VAR -= 1;\
validateRegisterIndex(VAR);\
}
/*
#define validateValueSize(VAR) {\
if(VAR < 1 || VAR > 4){\
VM_setError(vm, "invalid value_size (%x)", VAR);\
@@ -30,3 +33,4 @@
readVar(VAR);\
validateValueSize(VAR);\
}
*/

View File

@@ -4,8 +4,9 @@
u8 dst_register_i = 0, src_register_i = 0;\
readRegisterVar(dst_register_i);\
readRegisterVar(src_register_i);\
u8 value_size = 0;\
readValueSizeVar(value_size);\
/*u8 value_size = 0;\
readValueSizeVar(value_size);*/\
u8 value_size = 4;\
\
switch(value_size){\
case 1: \

View File

@@ -51,5 +51,15 @@ const Instruction* Instruction_getByName(str name){
}
}
return HashMap_Instruction_tryGetPtr(instructions_map, name);
str name_upper = str_toUpper(name);
Instruction* iptr = HashMap_Instruction_tryGetPtr(instructions_map, name_upper);
free(name_upper.data);
return iptr;
}
void Instruction_freeSearchStructs(){
if(instructions_map != NULL){
HashMap_Instruction_free(instructions_map);
free(instructions_map);
}
}

View File

@@ -35,3 +35,4 @@ typedef struct Instruction {
/// @return ptr to struct or NULL
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
const Instruction* NULLABLE(Instruction_getByName)(str name);
void Instruction_freeSearchStructs();

View File

@@ -0,0 +1,13 @@
#include "registers.h"
RegisterCode RegisterCode_parse(str r){
if(str_equals(r, STR("ax")))
return RegisterCode_ax;
if(str_equals(r, STR("bx")))
return RegisterCode_bx;
if(str_equals(r, STR("cx")))
return RegisterCode_cx;
if(str_equals(r, STR("dx")))
return RegisterCode_dx;
return RegisterCode_Unset;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "../std.h"
#include "../string/str.h"
typedef enum RegisterCode {
RegisterCode_Unset,
RegisterCode_ax,
RegisterCode_bx,
RegisterCode_cx,
RegisterCode_dx
} RegisterCode;
RegisterCode RegisterCode_parse(str register_name);

View File

@@ -92,6 +92,8 @@ i32 main(const i32 argc, cstr* argv){
exit_code = bootFromImage(image_file);
}
// frees global variables to supress valgrind memory leak errors
Instruction_freeSearchStructs();
return exit_code;
}

View File

@@ -26,6 +26,12 @@ typedef u8 bool;
typedef const char* cstr;
#if defined(_WIN64) || defined(_WIN32)
#define IFWIN(YES, NO) YES
#else
#define IFWIN(YES, NO) NO
#endif
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))

View File

@@ -36,18 +36,18 @@ void StringBuilder_append_cstr(StringBuilder* b, char* s){
void StringBuilder_append_i64(StringBuilder* b, i64 n){
char buf[32];
sprintf_s(buf, sizeof(buf), "%llu", n);
sprintf(buf, IFWIN("%lli", "%li"), n);
StringBuilder_append_cstr(b, buf);
}
void StringBuilder_append_u64(StringBuilder* b, u64 n){
char buf[32];
sprintf_s(buf, sizeof(buf), "%llu", n);
sprintf(buf, IFWIN("%llu", "%lu"), n);
StringBuilder_append_cstr(b, buf);
}
void StringBuilder_append_f64(StringBuilder* b, f64 n){
char buf[32];
sprintf_s(buf, sizeof(buf), "%lf", n);
sprintf(buf, "%lf", n);
StringBuilder_append_cstr(b, buf);
}

View File

@@ -100,9 +100,26 @@ bool str_endsWith(str src, str fragment){
u32 str_hash32(str s){
u8* ubuf = (u8*)s.data;
u32 len = s.len;
u32 hash=0;
for (u32 i = 0; i < len; i++)
for (u32 i = 0; i < s.len; i++)
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
return hash;
}
str str_toUpper(str src){
str r = str_copy(src);
for (u32 i = 0; i < r.len; i++){
if(isAlphabeticalLower(r.data[i]))
r.data[i] = r.data[i] - 'a' + 'A';
}
return r;
}
str str_toLower(str src){
str r = str_copy(src);
for (u32 i = 0; i < r.len; i++){
if(isAlphabeticalUpper(r.data[i]))
r.data[i] = r.data[i] - 'A' + 'a';
}
return r;
}

View File

@@ -36,3 +36,6 @@ bool str_endsWith(str src, str fragment);
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
/// @return non-cryptografic hash of the string
u32 str_hash32(str s);
str str_toUpper(str src);
str str_toLower(str src);