From 1b114e9820e15fe4a4a32f6d4a9eedba45e85d8c Mon Sep 17 00:00:00 2001 From: xolatile Date: Thu, 19 Oct 2023 17:10:50 -0400 Subject: [PATCH] Minor changes, still unfinished... --- xtandard.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++--------------- xtandard.h | 22 ++++++++++++--------- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/xtandard.c b/xtandard.c index c3c3592..1e8af63 100644 --- a/xtandard.c +++ b/xtandard.c @@ -521,7 +521,7 @@ int character_compare_array (char character, char * character_array) { } int string_length (char * string) { - int length = 0; + int length; if (string == NULL) { return (0); @@ -532,29 +532,37 @@ int string_length (char * string) { return (length); } -void string_reverse (char * string) { - int i = 0; - int length = 0; +char * string_reverse_limit (char * string, int limit) { + int i; fatal_failure (string == NULL, "string_reverse: String is null pointer."); - length = string_length (string); - - for (i = 0; string [i] != '\0'; ++i) { - string [i] = string [length - i]; + for (i = 0; i != limit / 2; ++i) { + char reverse_0 = string [limit - i]; + char reverse_1 = string [i]; + string [i] = reverse_0; + string [limit - i] = reverse_1; } + + return (string); } -void string_delete (char * string, int length) { +char * string_reverse (char * string) { + return (string_reverse_limit (string, string_length (string) - 1)); +} + +char * string_delete (char * string, int length) { int i; if ((string == NULL) || (length <= 0)) { - return; + return (string); } for (i = 0; i != length; ++i) { string [i] = '\0'; } + + return (string); } int string_compare (char * string_0, char * string_1) { @@ -572,7 +580,7 @@ int string_compare (char * string_0, char * string_1) { return (1); } -void string_copy (char * string_0, char * string_1) { +char * string_copy (char * string_0, char * string_1) { int i = 0; fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer."); @@ -581,9 +589,11 @@ void string_copy (char * string_0, char * string_1) { for (i = 0; i != string_length (string_1) + 1; ++i) { string_0 [i] = string_1 [i]; } + + return (string_0); } -void string_concatenate (char * string_0, char * string_1) { +char * string_concatenate (char * string_0, char * string_1) { fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer."); fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer."); @@ -596,6 +606,8 @@ void string_concatenate (char * string_0, char * string_1) { } * string_0 = '\0'; + + return (string_0); } int string_compare_limit (char * string_0, char * string_1, int limit) { @@ -613,22 +625,24 @@ int string_compare_limit (char * string_0, char * string_1, int limit) { return (1); } -void string_copy_limit (char * string_0, char * string_1, int limit) { +char * string_copy_limit (char * string_0, char * string_1, int limit) { int i = 0; fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer."); fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer."); if (limit <= 0) { - return; + return (string_0); } for (i = 0; i != limit; ++i) { string_0 [i] = string_1 [i]; } + + return (string_0); } -void string_concatenate_limit (char * string_0, char * string_1, int limit) { +char * string_concatenate_limit (char * string_0, char * string_1, int limit) { int i = 0; int length_0 = 0; int length_1 = 0; @@ -637,7 +651,7 @@ void string_concatenate_limit (char * string_0, char * string_1, int limit) { fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer."); if (limit <= 0) { - return; + return (string_0); } length_0 = string_length (string_0); @@ -646,6 +660,8 @@ void string_concatenate_limit (char * string_0, char * string_1, int limit) { for (i = 0; (i != length_1) && (i != limit); ++i) { string_0 [length_0 + i] = string_1 [i]; } + + return (string_0); } int string_split_space (char * string) { @@ -767,4 +783,23 @@ char * decode_byte (int byte) { return (decode); } +void number_to_string (int number, char * string) { + int i; + + if (number < 0) { + string [0] = '-'; + ++i; + number *= -1; + } + + for (i = (string [0] == '-'); number != 0; ++i) { + string [i] = (char) (number % 10) + '0'; + number /= 10; + } + + string [i] = '\0'; + + string_reverse (string); +} + #endif diff --git a/xtandard.h b/xtandard.h index 835a39f..c1f6768 100644 --- a/xtandard.h +++ b/xtandard.h @@ -104,17 +104,19 @@ extern int character_is_hexadecimal (char character); extern int character_compare_array (char character, char * character_array); extern int string_length (char * string); -extern void string_reverse (char * string); -extern void string_delete (char * string, int length); +extern char * string_reverse_limit (char * string, int limit); +extern char * string_reverse (char * string); -extern int string_compare (char * string_0, char * string_1); -extern void string_copy (char * string_0, char * string_1); -extern void string_concatenate (char * string_0, char * string_1); +extern char * string_delete (char * string, int length); -extern int string_compare_limit (char * string_0, char * string_1, int limit); -extern void string_copy_limit (char * string_0, char * string_1, int limit); -extern void string_concatenate_limit (char * string_0, char * string_1, int limit); +extern int string_compare (char * string_0, char * string_1); +extern char * string_copy (char * string_0, char * string_1); +extern char * string_concatenate (char * string_0, char * string_1); + +extern int string_compare_limit (char * string_0, char * string_1, int limit); +extern char * string_copy_limit (char * string_0, char * string_1, int limit); +extern char * string_concatenate_limit (char * string_0, char * string_1, int limit); extern int string_split_space (char * string); @@ -129,6 +131,8 @@ extern void terminal_cancel (void); extern void terminal_show_cursor (int show); extern int encode_byte (char * byte); -extern char * decode_byte (int byte); +extern char * decode_byte (int byte); + +extern void number_to_string (int number, char * string); #endif