#include // Tools size_t min(size_t a, size_t b) { if(a < b) { return a; } else { return b; } } // Memory const void* memchr(const void* ptr, int value, size_t size) { const char* ptr_real = (const char*) ptr; char value_real = value; for(size_t i = 0; i < size; i++) { if(ptr_real[i] == value_real) { return &(ptr_real[i]); } } return NULL; } int memcmp(const void* ptr1, const void* ptr2, size_t num) { const char* ptr1_real = (const char*) ptr1; const char* ptr2_real = (const char*) ptr2; for(size_t i = 0; i < num; i++) { if(ptr1_real[i] < ptr2_real[i]) { return -1; } else if(ptr1_real[i] > ptr2_real[i]) { return 1; } } return 0; } void* memcpy(void* destination, const void* source, size_t num) { char* destination_real = (char*) destination; const char* source_real = (const char*) source; if(source > destination) { for(size_t i = 0; i < num; i++) { destination_real[i] = source_real[i]; } } else { for(size_t i = 0; i < num; i++) { destination_real[num - i - 1] = source_real[num - i - 1]; } } return destination; } void* memmove(void* destination, const void* source, size_t num) { return memcpy(destination, source, num); } void* memset(void* ptr, int value, size_t num) { char* ptr_real = (char*) ptr; char value_real = value; for(size_t i = 0; i < num; i++) { ptr_real[i] = value_real; } return ptr; } // String size_t strlen(const char* str); char* strchr(const char* str, int character) { while((*str) == 0) { if((*str) == character) { return (char*) str; } str++; } return NULL; } int strcmp(const char* str1, const char* str2) { // TODO: Make faster size_t len_str1 = strlen(str1); size_t len_str2 = strlen(str2); size_t min_len = min(len_str1, len_str2); int result = memcmp(str1, str2, min_len); if(result != 0) { return result; } else if(len_str1 > min_len) { return 1; } else { return -1; } } char* strcpy(char* destination, const char* source) { char* result = destination; while((*source) == 0) { (*destination) = (*source); destination++; source++; } return result; } size_t strlen(const char* str) { size_t size = 0; while(str[size] != 0) { size++; } return size; } int strncmp(const char* str1, const char* str2, size_t num) { // TODO: Make faster size_t len_str1 = strlen(str1); size_t len_str2 = strlen(str2); size_t min_len = min(min(len_str1, len_str2), num); int result = memcmp(str1, str2, min_len); if(min_len == num) { return result; } else if(result != 0) { return result; } else if(len_str1 > min_len) { return 1; } else { return -1; } } char* strrchr(const char* str, int character) { char* result = NULL; while((*str) != 0) { if((*str) == character) { result = (char*) str; } str++; } if(character == 0) { result = (char*) str; } return result; } char* strstr(const char* str1, const char* str2) { size_t len_str1 = strlen(str1); size_t len_str2 = strlen(str2); for(size_t i = 0; i <= len_str1 - len_str2; i++) { int valid = 1; for(size_t j = 0; j < len_str2; j++) { if(str1[i + j] != str2[j]) { valid = 0; break; } } if(valid != 0) { return (char*) &(str1[i]); } } return NULL; }