From: Kevin Day Date: Fri, 14 Jul 2023 03:07:57 +0000 (-0500) Subject: Update: Conversion should return F_number_positive and F_number_negative without... X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=8ea430f4e5186e6f6cea5c0073a4f0db8afc633d;p=fll Update: Conversion should return F_number_positive and F_number_negative without error bit and do some clean up. Do not treat F_number_positive and F_number_negative as an error. The "unsigned" concept should be internal to the function and not whether or not the user input has a sign. If F_number_negative is returned, the value can still be of type f_number_unsigned_t. This is because the return status designates the effective sign. The caller can then decide whether or not this is an error. --- diff --git a/level_0/f_conversion/c/conversion.c b/level_0/f_conversion/c/conversion.c index 4623a68..0eeedeb 100644 --- a/level_0/f_conversion/c/conversion.c +++ b/level_0/f_conversion/c/conversion.c @@ -8,39 +8,23 @@ extern "C" { #ifndef _di_f_conversion_character_is_binary_ f_status_t f_conversion_character_is_binary(const f_char_t character) { - if (character == 0x30 || character == 0x31) { - return F_true; - } - - return F_false; + return (character == 0x30 || character == 0x31) ? F_true : F_false; } #endif // _di_f_conversion_character_is_binary_ #ifndef _di_f_conversion_character_is_decimal_ f_status_t f_conversion_character_is_decimal(const f_char_t character) { - if (character > 0x2f && character < 0x3a) { - return F_true; - } - - return F_false; + return (character > 0x2f && character < 0x3a) ? F_true : F_false; } #endif // _di_f_conversion_character_is_decimal_ #ifndef _di_f_conversion_character_is_duodecimal_ f_status_t f_conversion_character_is_duodecimal(const f_char_t character) { - if (character > 0x2f && character < 0x3a) { - return F_true; - } - - if (character == 0x41 || character == 0x42) { - return F_true; - } - - if (character == 0x61 || character == 0x62) { - return F_true; - } + if (character > 0x2f && character < 0x3a) return F_true; + if (character == 0x41 || character == 0x42) return F_true; + if (character == 0x61 || character == 0x62) return F_true; return F_false; } @@ -49,17 +33,9 @@ extern "C" { #ifndef _di_f_conversion_character_is_hexidecimal_ f_status_t f_conversion_character_is_hexidecimal(const f_char_t character) { - if (character > 0x2f && character < 0x3a) { - return F_true; - } - - if (character > 0x40 && character < 0x47) { - return F_true; - } - - if (character > 0x60 && character < 0x67) { - return F_true; - } + if (character > 0x2f && character < 0x3a) return F_true; + if (character > 0x40 && character < 0x47) return F_true; + if (character > 0x60 && character < 0x67) return F_true; return F_false; } @@ -68,11 +44,7 @@ extern "C" { #ifndef _di_f_conversion_character_is_octal_ f_status_t f_conversion_character_is_octal(const f_char_t character) { - if (character > 0x2f && character < 0x38) { - return F_true; - } - - return F_false; + return (character > 0x2f && character < 0x38) ? F_true : F_false; } #endif // _di_f_conversion_character_is_octal_ @@ -160,9 +132,7 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (character < 0x30 || character > 0x37) { - return F_status_set_error(F_number); - } + if (character < 0x30 || character > 0x37) return F_status_set_error(F_number); *number = 0xf & character; diff --git a/level_1/fl_conversion/c/conversion.c b/level_1/fl_conversion/c/conversion.c index 391e08a..ed133e6 100644 --- a/level_1/fl_conversion/c/conversion.c +++ b/level_1/fl_conversion/c/conversion.c @@ -11,14 +11,13 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used || range.start > range.stop) { - return F_data_not; - } + if (!buffer.used || range.start > range.stop) return F_data_not; if (data.base == 10 || data.base == 16 || data.base == 12 || data.base == 8) { return private_fl_conversion_dynamic_to_base_signed(data, buffer.string + range.start, (range.stop - range.start) + 1, number); } - else if (data.base == 2) { + + if (data.base == 2) { return private_fl_conversion_dynamic_to_binary_signed(data.flag, buffer.string + range.start, (range.stop - range.start) + 1, number); } @@ -32,14 +31,13 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used || range.start > range.stop) { - return F_data_not; - } + if (!buffer.used || range.start > range.stop) return F_data_not; if (data.base == 10 || data.base == 16 || data.base == 12 || data.base == 8) { return private_fl_conversion_dynamic_to_base_unsigned(data, buffer.string + range.start, (range.stop - range.start) + 1, number); } - else if (data.base == 2) { + + if (data.base == 2) { return private_fl_conversion_dynamic_to_binary_unsigned(data.flag, buffer.string + range.start, (range.stop - range.start) + 1, number); } @@ -53,9 +51,7 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used || range.start > range.stop) { - return F_data_not; - } + if (!buffer.used || range.start > range.stop) return F_data_not; return private_fl_conversion_dynamic_to_signed_detect(data.flag, buffer.string + range.start, (range.stop - range.start) + 1, number); } @@ -67,9 +63,7 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used || range.start > range.stop) { - return F_data_not; - } + if (!buffer.used || range.start > range.stop) return F_data_not; return private_fl_conversion_dynamic_to_unsigned_detect(data.flag, buffer.string + range.start, (range.stop - range.start) + 1, number); } @@ -81,14 +75,13 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used) { - return F_data_not; - } + if (!buffer.used) return F_data_not; if (data.base == 10 || data.base == 16 || data.base == 12 || data.base == 8) { return private_fl_conversion_dynamic_to_base_signed(data, buffer.string, buffer.used, number); } - else if (data.base == 2) { + + if (data.base == 2) { return private_fl_conversion_dynamic_to_binary_signed(data.flag, buffer.string, buffer.used, number); } @@ -102,14 +95,13 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used) { - return F_data_not; - } + if (!buffer.used) return F_data_not; if (data.base == 10 || data.base == 16 || data.base == 12 || data.base == 8) { return private_fl_conversion_dynamic_to_base_unsigned(data, buffer.string, buffer.used, number); } - else if (data.base == 2) { + + if (data.base == 2) { return private_fl_conversion_dynamic_to_binary_unsigned(data.flag, buffer.string, buffer.used, number); } @@ -123,9 +115,7 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used) { - return F_data_not; - } + if (!buffer.used) return F_data_not; return private_fl_conversion_dynamic_to_signed_detect(data.flag, buffer.string, buffer.used, number); } @@ -137,9 +127,7 @@ extern "C" { if (!number) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ - if (!buffer.used) { - return F_data_not; - } + if (!buffer.used) return F_data_not; return private_fl_conversion_dynamic_to_unsigned_detect(data.flag, buffer.string, buffer.used, number); } diff --git a/level_1/fl_conversion/c/conversion.h b/level_1/fl_conversion/c/conversion.h index 0c3add7..3d96b40 100644 --- a/level_1/fl_conversion/c/conversion.h +++ b/level_1/fl_conversion/c/conversion.h @@ -82,6 +82,7 @@ extern "C" { * * @param data * Conversion data for specifying things such as treating this as a negative number. + * This auto-detects the base and negative, ignoring the base number and negative flag. * @param buffer * The string to convert. * @param range @@ -173,13 +174,13 @@ extern "C" { * @return * F_none on success. * F_data_not if string starts with a null (length is 0). + * F_number_negative on negative value (has a -, such as '-1'). + * F_number_positive on positive value (has a +, such as '+1'). * * F_base_not (with error bit) if no supported or valid base unit is provided. * F_complete_not_utf (with error bit) if an incomplete UTF-8 fragment is found. * F_number (with error bit) if parameter is not a number. * F_number_decimal (with error bit) if number has a decimal digit. - * F_number_negative (with error bit) on negative value. - * F_number_positive (with error bit) on positive value (has a +, such as '+1', when only '1' is valid here). * F_number_overflow (with error bit) on integer overflow. * F_parameter (with error bit) if a parameter is invalid. * @@ -326,13 +327,13 @@ extern "C" { * @return * F_none on success. * F_data_not if string starts with a null (length is 0). + * F_number_negative on negative value (has a -, such as '-1'). + * F_number_positive on positive value (has a +, such as '+1'). * * F_base_not (with error bit) if no supported or valid base unit is provided. * F_complete_not_utf (with error bit) if an incomplete UTF-8 fragment is found. * F_number (with error bit) if parameter is not a number. * F_number_decimal (with error bit) if number has a decimal digit. - * F_number_negative (with error bit) on negative value. - * F_number_positive (with error bit) on positive value (has a +, such as '+1', when only '1' is valid here). * F_number_overflow (with error bit) on integer overflow. * F_parameter (with error bit) if a parameter is invalid. * diff --git a/level_1/fl_conversion/c/private-conversion.c b/level_1/fl_conversion/c/private-conversion.c index 500fd0e..2037432 100644 --- a/level_1/fl_conversion/c/private-conversion.c +++ b/level_1/fl_conversion/c/private-conversion.c @@ -14,18 +14,14 @@ extern "C" { for (f_number_unsigned_t i = 0; i < length; ++i) { - if (string[i] == f_string_ascii_period_s.string[0]) { - return F_status_set_error(F_number_decimal); - } + if (string[i] == f_string_ascii_period_s.string[0]) return F_status_set_error(F_number_decimal); if (f_conversion_character_to_binary(string[i], &digit) == F_none) { if (digits) { ++digits; if (flag & fl_conversion_data_flag_negative_e) { - if (digits > F_conversion_digits_binary_signed_d) { - return F_status_set_error(F_number_underflow); - } + if (digits > F_conversion_digits_binary_signed_d) return F_status_set_error(F_number_underflow); if (flag & fl_conversion_data_flag_endian_big_e) { converted >>= 1; @@ -44,9 +40,7 @@ extern "C" { converted -= digit; } else { - if (digits > F_conversion_digits_binary_signed_d) { - return F_status_set_error(F_number_overflow); - } + if (digits > F_conversion_digits_binary_signed_d) return F_status_set_error(F_number_overflow); if (flag & fl_conversion_data_flag_endian_big_e) { converted >>= 1; @@ -96,17 +90,13 @@ extern "C" { for (f_number_unsigned_t i = 0; i < length; ++i) { - if (string[i] == f_string_ascii_period_s.string[0]) { - return F_status_set_error(F_number_decimal); - } + if (string[i] == f_string_ascii_period_s.string[0]) return F_status_set_error(F_number_decimal); if (f_conversion_character_to_binary(string[i], &digit) == F_none) { if (digits) { ++digits; - if (digits > F_conversion_digits_binary_unsigned_d) { - return F_status_set_error(F_number_overflow); - } + if (digits > F_conversion_digits_binary_unsigned_d) return F_status_set_error(F_number_overflow); if (flag & fl_conversion_data_flag_endian_big_e) { converted >>= 1; @@ -164,9 +154,7 @@ extern "C" { for (f_number_unsigned_t i = 0; i < length; ++i) { - if (string[i] == f_string_ascii_period_s.string[0]) { - return F_status_set_error(F_number_decimal); - } + if (string[i] == f_string_ascii_period_s.string[0]) return F_status_set_error(F_number_decimal); if (character_to_digit(string[i], &digit) == F_none) { if (digits) { @@ -260,9 +248,7 @@ extern "C" { for (f_number_unsigned_t i = 0; i < length; ++i) { - if (string[i] == f_string_ascii_period_s.string[0]) { - return F_status_set_error(F_number_decimal); - } + if (string[i] == f_string_ascii_period_s.string[0]) return F_status_set_error(F_number_decimal); if (character_to_digit(string[i], &digit) == F_none) { if (digits) { @@ -442,13 +428,7 @@ extern "C" { return F_status_set_error(F_number); } // for - if (!mode) { - return F_status_set_error(F_number); - } - - if (offset >= length) { - return F_status_set_error(F_number); - } + if (!mode || offset >= length) return F_status_set_error(F_number); fl_conversion_data_t data = macro_fl_conversion_data_t_initialize_1(mode, flag); @@ -568,13 +548,21 @@ extern "C" { } if (string[i] == f_string_ascii_plus_s.string[0]) { - ++offset; - sign_found = 1; + if (!sign_found) { + ++offset; + sign_found = 1; + + continue; + } } if (string[i] == f_string_ascii_minus_s.string[0]) { - ++offset; - sign_found = -1; + if (!sign_found) { + ++offset; + sign_found = -1; + + continue; + } } if (f_conversion_character_is_decimal(string[i]) == F_true) { @@ -586,13 +574,7 @@ extern "C" { return F_status_set_error(F_number); } // for - if (!mode) { - return F_status_set_error(F_number); - } - - if (offset >= length) { - return F_status_set_error(F_number); - } + if (!mode || offset >= length) return F_status_set_error(F_number); fl_conversion_data_t data = macro_fl_conversion_data_t_initialize_1(mode, flag); @@ -612,13 +594,7 @@ extern "C" { // The +/- signs are not allowed. if (sign_found) { - if (status == F_none) { - if (sign_found == -1) { - return F_status_set_error(F_number_negative); - } - - return F_status_set_error(F_number_positive); - } + if (status == F_none) return (sign_found == -1) ? F_number_negative : F_number_positive; return F_status_set_error(F_number); } diff --git a/level_1/fl_conversion/c/private-conversion.h b/level_1/fl_conversion/c/private-conversion.h index 30cff93..93e6050 100644 --- a/level_1/fl_conversion/c/private-conversion.h +++ b/level_1/fl_conversion/c/private-conversion.h @@ -198,12 +198,12 @@ extern "C" { * @return * F_none on success. * F_data_not if string starts with a null (length is 0). + * F_number_negative on negative value (has a -, such as '-1'). + * F_number_positive on positive value (has a +, such as '+1'). * * F_complete_not_utf (with error bit) if an incomplete UTF-8 fragment is found. * F_number (with error bit) if parameter is not a number. * F_number_decimal (with error bit) if number has a decimal digit. - * F_number_negative (with error bit) on negative value. - * F_number_positive (with error bit) on positive value (has a +, such as '+1', when only '1' is valid here). * F_number_overflow (with error bit) on integer overflow. * F_parameter (with error bit) if a parameter is invalid. * diff --git a/level_3/byte_dump/c/byte_dump.c b/level_3/byte_dump/c/byte_dump.c index 28ec16b..39844da 100644 --- a/level_3/byte_dump/c/byte_dump.c +++ b/level_3/byte_dump/c/byte_dump.c @@ -189,6 +189,11 @@ extern "C" { status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, data.argv[index], &number); + // Negative numbers are not supported. + if (status == F_number_negative) { + status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(status) || number < 1 || number >= 0xfb) { f_file_stream_lock(main->error.to); @@ -228,6 +233,11 @@ extern "C" { status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, data.argv[index], &number); + // Negative numbers are not supported. + if (status == F_number_negative) { + status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(status) || number > F_number_t_size_unsigned_d) { f_file_stream_lock(main->error.to); @@ -267,6 +277,11 @@ extern "C" { status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, data.argv[index], &number); + // Negative numbers are not supported. + if (status == F_number_negative) { + status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(status) || number < 0 || number > F_number_t_size_unsigned_d) { f_file_stream_lock(main->error.to); diff --git a/level_3/fake/c/main/make.c b/level_3/fake/c/main/make.c index 2056840..48dd2d6 100644 --- a/level_3/fake/c/main/make.c +++ b/level_3/fake/c/main/make.c @@ -60,6 +60,11 @@ extern "C" { main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, buffer, &number); + // Negative numbers are not supported. + if (main->setting.state.status == F_number_negative) { + main->setting.state.status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(main->setting.state.status)) { // When the buffer is not a number, then check to see if this is a group or owner name. diff --git a/level_3/fss_identify/c/main/common.c b/level_3/fss_identify/c/main/common.c index aa8a519..543c936 100644 --- a/level_3/fss_identify/c/main/common.c +++ b/level_3/fss_identify/c/main/common.c @@ -112,6 +112,11 @@ extern "C" { main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, main->program.parameters.arguments.array[index], &main->setting.line); + // Negative numbers are not supported. + if (main->setting.state.status == F_number_negative) { + main->setting.state.status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(main->setting.state.status)) { if ((main->setting.flag & fss_identify_main_flag_print_first_e) && main->program.message.verbosity > f_console_verbosity_error_e) { fll_print_dynamic_raw(f_string_eol_s, main->program.message.to); diff --git a/level_3/iki_read/c/main/common.c b/level_3/iki_read/c/main/common.c index 29cbade..e3c5f31 100644 --- a/level_3/iki_read/c/main/common.c +++ b/level_3/iki_read/c/main/common.c @@ -135,6 +135,11 @@ extern "C" { main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, main->program.parameters.arguments.array[index], &main->setting.at); + // Negative numbers are not supported. + if (main->setting.state.status == F_number_negative) { + main->setting.state.status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(main->setting.state.status)) { main->setting.state.status = F_status_set_error(F_parameter); @@ -168,6 +173,11 @@ extern "C" { main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, main->program.parameters.arguments.array[index], &main->setting.line); + // Negative numbers are not supported. + if (main->setting.state.status == F_number_negative) { + main->setting.state.status = F_status_set_error(F_number_negative); + } + if (F_status_is_error(main->setting.state.status)) { main->setting.state.status = F_status_set_error(F_parameter); diff --git a/level_3/utf8/c/main/codepoint.c b/level_3/utf8/c/main/codepoint.c index 1b8d2ef..9a0c3c0 100644 --- a/level_3/utf8/c/main/codepoint.c +++ b/level_3/utf8/c/main/codepoint.c @@ -126,6 +126,11 @@ extern "C" { main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, main->setting.text, &number); raw = (f_utf_char_t) number; + + // Negative numbers are not supported. + if (main->setting.state.status == F_number_negative) { + main->setting.state.status = F_status_set_error(F_number_negative); + } } if (F_status_is_error(main->setting.state.status)) {