From: Kevin Day Date: Mon, 9 Sep 2019 00:46:41 +0000 (-0500) Subject: Update: Simplifiy digit/decimal and hexidecimal functions X-Git-Tag: 0.5.0~438 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=ca2e631eb7976247971335c41e1cfb7efa582e2d;p=fll Update: Simplifiy digit/decimal and hexidecimal functions Use simpler logic for determining if character is a decimal or a hexidecimal. Rename digit to decimal. --- diff --git a/level_0/f_conversion/c/conversion.c b/level_0/f_conversion/c/conversion.c index cd098dc..281f2d9 100644 --- a/level_0/f_conversion/c/conversion.c +++ b/level_0/f_conversion/c/conversion.c @@ -4,64 +4,33 @@ extern "C" { #endif -#ifndef _di_f_is_digit_ - f_return_status f_is_digit(const char character) { +#ifndef _di_f_is_decimal_ + f_return_status f_is_decimal(const char character) { - // at this point, it seems that it would incur more overhead to use the libc isdigit here, so just use one less call and test it here - switch (character) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - break; - default: - return f_false; + if (character > 0x29 && character < 0x40) { + return f_true; } - return f_true; + return f_false; } -#endif // _di_f_is_digit_ +#endif // _di_f_is_decimal_ -#ifndef _di_f_is_hexdigit_ - f_return_status f_is_hexdigit(const char character) { +#ifndef _di_f_is_hexidecimal_ + f_return_status f_is_hexidecimal(const char character) { - switch (character) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - break; - default: - return f_false; + if (character > 0x29 && character < 0x40) { + return f_true; + } + else if (character > 0x40 && character < 0x47) { + return f_true; + } + else if (character > 0x60 && character < 0x67) { + return f_true; } - return f_true; + return f_false; } -#endif // _di_f_is_hexdigit_ +#endif // _di_f_is_hexidecimal_ #ifndef _di_f_character_to_digit_ f_return_status f_character_to_digit(const char character, f_u_long *digit) { @@ -70,16 +39,16 @@ extern "C" { #endif // _di_level_0_parameter_checking_ switch (character) { - case '0': *digit = 0; break; - case '1': *digit = 1; break; - case '2': *digit = 2; break; - case '3': *digit = 3; break; - case '4': *digit = 4; break; - case '5': *digit = 5; break; - case '6': *digit = 6; break; - case '7': *digit = 7; break; - case '8': *digit = 8; break; - case '9': *digit = 9; break; + case 0x30: *digit = 0; break; + case 0x31: *digit = 1; break; + case 0x32: *digit = 2; break; + case 0x33: *digit = 3; break; + case 0x34: *digit = 4; break; + case 0x35: *digit = 5; break; + case 0x36: *digit = 6; break; + case 0x37: *digit = 7; break; + case 0x38: *digit = 8; break; + case 0x39: *digit = 9; break; default: return f_no_data; } @@ -88,45 +57,45 @@ extern "C" { } #endif // _di_f_character_to_digit_ -#ifndef _di_f_character_to_hexdigit_ - f_return_status f_character_to_hexdigit(const char character, f_u_long *digit) { +#ifndef _di_f_character_to_hexdecimal_ + f_return_status f_character_to_hexdecimal(const char character, f_u_long *digit) { #ifndef _di_level_0_parameter_checking_ if (digit == 0) return f_status_set_error(f_invalid_parameter); #endif // _di_level_0_parameter_checking_ switch (character) { - case '0': *digit = 0; break; - case '1': *digit = 1; break; - case '2': *digit = 2; break; - case '3': *digit = 3; break; - case '4': *digit = 4; break; - case '5': *digit = 5; break; - case '6': *digit = 6; break; - case '7': *digit = 7; break; - case '8': *digit = 8; break; - case '9': *digit = 9; break; - case 'A': *digit = 10; break; - case 'B': *digit = 11; break; - case 'C': *digit = 12; break; - case 'D': *digit = 13; break; - case 'E': *digit = 14; break; - case 'F': *digit = 15; break; - case 'a': *digit = 10; break; - case 'b': *digit = 11; break; - case 'c': *digit = 12; break; - case 'd': *digit = 13; break; - case 'e': *digit = 14; break; - case 'f': *digit = 15; break; + case 0x30: *digit = 0; break; + case 0x31: *digit = 1; break; + case 0x32: *digit = 2; break; + case 0x33: *digit = 3; break; + case 0x34: *digit = 4; break; + case 0x35: *digit = 5; break; + case 0x36: *digit = 6; break; + case 0x37: *digit = 7; break; + case 0x38: *digit = 8; break; + case 0x39: *digit = 9; break; + case 0x41: *digit = 10; break; + case 0x42: *digit = 11; break; + case 0x43: *digit = 12; break; + case 0x44: *digit = 13; break; + case 0x45: *digit = 14; break; + case 0x46: *digit = 15; break; + case 0x61: *digit = 10; break; + case 0x62: *digit = 11; break; + case 0x63: *digit = 12; break; + case 0x64: *digit = 13; break; + case 0x65: *digit = 14; break; + case 0x66: *digit = 15; break; default: return f_no_data; } return f_none; } -#endif // _di_f_character_to_hexdigit_ +#endif // _di_f_character_to_hexdecimal_ -#ifndef _di_f_string_to_digit_ - f_return_status f_string_to_digit(const f_string string, f_u_long *digit, const f_string_location location) { +#ifndef _di_f_string_to_decimal_ + f_return_status f_string_to_decimal(const f_string string, f_u_long *digit, const f_string_location location) { #ifndef _di_level_0_parameter_checking_ if (digit == 0) return f_status_set_error(f_invalid_parameter); if (location.start < 0) return f_status_set_error(f_invalid_parameter); @@ -156,14 +125,14 @@ extern "C" { } ++current_location; - } + } // while return f_none; } -#endif // _di_f_string_to_digit_ +#endif // _di_f_string_to_decimal_ -#ifndef _di_f_string_to_hexdigit_ - f_return_status f_string_to_hexdigit(const f_string string, f_u_long *digit, const f_string_location location) { +#ifndef _di_f_string_to_hexidecimal_ + f_return_status f_string_to_hexidecimal(const f_string string, f_u_long *digit, const f_string_location location) { #ifndef _di_level_0_parameter_checking_ if (digit == 0) return f_status_set_error(f_invalid_parameter); if (location.start < 0) return f_status_set_error(f_invalid_parameter); @@ -176,7 +145,7 @@ extern "C" { f_u_long temp_digit = 0; while (current_location < location.stop) { - if (f_character_to_hexdigit(string[current_location], &temp_digit) == f_none) { + if (f_character_to_hexdecimal(string[current_location], &temp_digit) == f_none) { // when the scale exists, then we need to make the number larger, for this function the scale is base 16 if (scale > 0) { @@ -193,11 +162,11 @@ extern "C" { } ++current_location; - } + } // while return f_none; } -#endif // _di_f_string_to_hexdigit_ +#endif // _di_f_string_to_hexidecimal_ #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_conversion/c/conversion.h b/level_0/f_conversion/c/conversion.h index 334a94f..56fb087 100644 --- a/level_0/f_conversion/c/conversion.h +++ b/level_0/f_conversion/c/conversion.h @@ -34,19 +34,19 @@ extern "C" { #endif -#ifndef _di_f_is_digit_ +#ifndef _di_f_is_decimal_ /** - * convert a single character into the digit that it represents. + * convert a single character into the decimal value that it represents. */ - extern f_return_status f_is_digit(const char character); -#endif // _di_f_is_digit_ + extern f_return_status f_is_decimal(const char character); +#endif // _di_f_is_decimal_ -#ifndef _di_f_is_hexdigit_ +#ifndef _di_f_is_hexidecimal_ /** * convert a single character into the hexidecimal digit that it represents */ - extern f_return_status f_is_hexdigit(const char character); -#endif // _di_f_is_hexdigit_ + extern f_return_status f_is_hexidecimal(const char character); +#endif // _di_f_is_hexidecimal_ #ifndef _di_f_character_to_digit_ /** @@ -55,31 +55,31 @@ extern "C" { extern f_return_status f_character_to_digit(const char character, f_u_long *digit); #endif // _di_f_character_to_digit_ -#ifndef _di_f_character_to_hexdigit_ +#ifndef _di_f_character_to_hexdecimal_ /** *convert a single character into the hexidecimal digit that it represents. */ - extern f_return_status f_character_to_hexdigit(const char character, f_u_long *digit); -#endif // _di_f_character_to_hexdigit_ + extern f_return_status f_character_to_hexdecimal(const char character, f_u_long *digit); +#endif // _di_f_character_to_hexdecimal_ -#ifndef _di_f_string_to_digit_ +#ifndef _di_f_string_to_decimal_ /** * works like atoi, except there is a start/stop range. * convert a series of positive numbers into a string, stopping at one of the following: EOS, max_length, or a non-digit. * will not process signed statuses (+/-). */ - extern f_return_status f_string_to_digit(const f_string string, f_u_long *digit, const f_string_location location); -#endif // _di_f_string_to_digit_ + extern f_return_status f_string_to_decimal(const f_string string, f_u_long *digit, const f_string_location location); +#endif // _di_f_string_to_decimal_ -#ifndef _di_f_string_to_hexdigit_ +#ifndef _di_f_string_to_hexidecimal_ /** * works like atoi, except there is a start/stop range and that this is for hexidecimal digits. * convert a series of positive numbers into a string, stopping at one of the following: EOS, max_length, or a non-hexdigit. * will not process signed statuses (+/-). */ - extern f_return_status f_string_to_hexdigit(const f_string string, f_u_long *digit, const f_string_location location); -#endif // _di_f_string_to_hexdigit_ + extern f_return_status f_string_to_hexidecimal(const f_string string, f_u_long *digit, const f_string_location location); +#endif // _di_f_string_to_hexidecimal_ #ifdef __cplusplus } // extern "C" diff --git a/level_1/fl_fss/c/fss.c b/level_1/fl_fss/c/fss.c index abd0026..035192a 100644 --- a/level_1/fl_fss/c/fss.c +++ b/level_1/fl_fss/c/fss.c @@ -91,16 +91,16 @@ extern "C" { if (buffer.string[i] == f_fss_type_header_part5) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; f_string_location location = f_string_location_initialize; @@ -109,7 +109,7 @@ extern "C" { location.stop = i; // 1: A possibly valid header type was found, now convert it into its proper format and save the header type - f_string_to_hexdigit(buffer.string, &header->type, location); + f_string_to_hexidecimal(buffer.string, &header->type, location); // 2: At this point, we can still know the proper format for the file and still have a invalid header, handle accordingly if (buffer.string[i] == f_fss_type_header_close) { @@ -147,16 +147,16 @@ extern "C" { if (buffer.string[i] == f_fss_type_header_part5) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; - if (f_is_hexdigit(buffer.string[i]) == f_true) { + if (f_is_hexidecimal(buffer.string[i]) == f_true) { i++; f_string_location location = f_string_location_initialize; @@ -164,7 +164,7 @@ extern "C" { location.start = i - 4; location.stop = i; - f_string_to_hexdigit(buffer.string, &header->type, location); + f_string_to_hexidecimal(buffer.string, &header->type, location); header->length = i + 1; diff --git a/level_2/fll_fss/c/fss_status.c b/level_2/fll_fss/c/fss_status.c index fe1313e..5e37e17 100644 --- a/level_2/fll_fss/c/fss_status.c +++ b/level_2/fll_fss/c/fss_status.c @@ -18,7 +18,7 @@ extern "C" { } // numbers are not valid status code strings. - if ((status = f_is_digit(string[0])) == f_true) { + if ((status = f_is_decimal(string[0])) == f_true) { return f_invalid_data; } diff --git a/level_2/fll_status/c/status.c b/level_2/fll_status/c/status.c index c8e5df7..b7e64ec 100644 --- a/level_2/fll_status/c/status.c +++ b/level_2/fll_status/c/status.c @@ -18,7 +18,7 @@ extern "C" { } // numbers are not valid status code strings. - if ((status = f_is_digit(string[0])) == f_true) { + if ((status = f_is_decimal(string[0])) == f_true) { return f_invalid_data; } diff --git a/level_3/fss_status_code/c/fss_status_code.c b/level_3/fss_status_code/c/fss_status_code.c index 43e7490..6b13256 100644 --- a/level_3/fss_status_code/c/fss_status_code.c +++ b/level_3/fss_status_code/c/fss_status_code.c @@ -62,7 +62,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -101,7 +101,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -140,7 +140,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -178,7 +178,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // numbers are not valid status code strings. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_true) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_true) { status = f_false; continue; } @@ -214,7 +214,7 @@ extern "C" { if (data->remaining.used > 0) { for (; counter < data->remaining.used; counter++) { // only numbers are valid status code. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } diff --git a/level_3/status_code/c/status_code.c b/level_3/status_code/c/status_code.c index 8e8fc70..872aaad 100644 --- a/level_3/status_code/c/status_code.c +++ b/level_3/status_code/c/status_code.c @@ -62,7 +62,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -101,7 +101,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -140,7 +140,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // only numbers are valid status codes. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; } @@ -178,7 +178,7 @@ extern "C" { for (; counter < data->remaining.used; counter++) { // numbers are not valid status code strings. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_true) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_true) { status = f_false; continue; } @@ -213,7 +213,7 @@ extern "C" { if (data->remaining.used > 0) { for (; counter < data->remaining.used; counter++) { // only numbers are valid status code. - if (f_is_digit(arguments.argv[data->remaining.array[counter]][0]) == f_false) { + if (f_is_decimal(arguments.argv[data->remaining.array[counter]][0]) == f_false) { status = f_false; continue; }