Created
March 2, 2023 09:46
-
-
Save angusbarnes/c8c6020181c25738fab16dd5482467d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #define INTERNAL_FUNC static // Sementic distinction for functions designed for internal use only | |
| /* | |
| * These are functions specific to use on the linux platfrom. | |
| * Unix and POSIX platforms define terminal color modes which can | |
| * be set using escapre sequences on the standard output stream. | |
| * These are not available on windows by default. Modern windows | |
| * builds do support this ANSI color codes via terminal emulation | |
| * however there is not way to detect this currently. As a result, | |
| * windows users who wish to use these color modes must manually enable | |
| * them using WINDOWS_MANUAL_COLOR_ENABLE 1 | |
| */ | |
| #if __linux__ | |
| #define __TERMINAL_COLOR_MODE_ENABLED 1 | |
| #endif | |
| #if WINDOWS_MANUAL_COLOR_ENABLE | |
| #define __TERMINAL_COLOR_MODE_ENABLED 1 | |
| #endif | |
| #if __TERMINAL_COLOR_MODE_ENABLED | |
| #define CM_FOREGROUND_RED "\e[0;31m" | |
| #define CM_FOREGROUND_GREEN "\e[0;32m" | |
| #define CM_FOREGROUND_RESET "\e[0m" | |
| #define CM_BACKGROUND_WHITE "\e[47m" | |
| // Below are are number of functions which are simply sementically named | |
| // versions of eachother. Functionally all that is required is for the user | |
| // to place one of the color codes on the standard out, however using naming | |
| // such as this allows for easier reading of the code | |
| void cm_foreground_set(char* color_code) { | |
| puts(color_code); | |
| } | |
| void cm_background_set(char* color_code) { | |
| puts(color_code); | |
| } | |
| void cm_foreground_reset() { | |
| puts(CM_FOREGROUND_RESET); | |
| } | |
| #endif | |
| #ifdef _DEBUG | |
| #endif | |
| /* | |
| * This section defined custom implementations of standard library | |
| * that have more limited usecases but are intended to 'fix' some | |
| * of the default functionality of these functions that I have a | |
| * problem with. They are prefixed with a_ for Astro to avoid naming | |
| * conflicts, however the provided API's remain similar, with the | |
| * exception of some functions which return stricter and more sensible | |
| * values | |
| */ | |
| INTERNAL_FUNC int __a_strlen(char* str) { | |
| char c = str[0]; | |
| int length = 0; | |
| while (c != '\0') { | |
| length++; | |
| c = str[length]; | |
| } | |
| return length; | |
| } | |
| // TODO: Rewrite this as it is nearly twice as slow as the standard strcmp | |
| // loop through string using while and checking index incrementely to improve performance | |
| // rather than precalculating both lengths | |
| int a_strcmp(char* str1, char* str2) { | |
| int length1 = __a_strlen(str1); | |
| int length2 = __a_strlen(str2); | |
| if (length1 != length2) { | |
| return 0; | |
| } | |
| for (int i = 0; i < length1; i++) { | |
| if (str1[i] != str2[i]) | |
| return 0; | |
| } | |
| return 1; | |
| } | |
| /* | |
| This version is roughly as fast as strcmp for non constant usecases. | |
| For some specific cases this method may be faster. | |
| */ | |
| int a_strcmp_fast(char* str1, char* str2) { | |
| int currChar = 0; | |
| while(str1[currChar] != '\0') { | |
| // At this point string 1 has a char remaining | |
| if (str1[currChar] != str2[currChar]) | |
| return 0; // There was a mismatch in the characters | |
| if (str2[currChar] == '\0') | |
| return 0; // String 2 finished before the end of string 1 | |
| currChar++; | |
| } | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment