about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDaniel Santos <dacs.git@brilhante.top>2022-03-18 20:10:17 +0000
committerDaniel Santos <dacs.git@brilhante.top>2022-03-18 20:39:40 +0000
commit32fbc9c08c95658fab3b31c71583642847b552aa (patch)
treee1fc01baed9adad1bb288aa30971258f58d08ec5
parenta8f50b625ce5f0505e2f6051c706de33efaef4de (diff)
downloadnumericx-c-32fbc9c08c95658fab3b31c71583642847b552aa.tar.gz
bugfix: handle number void with any number of cases
 * add is_number_void()
 * numericx_translate() was only handling a void number with one digit
   as argument, for example a 0.
   When encontering a void number with more then one digit, the function
   was entering on a infinite cycle.

   Now, with the function is_number_void(), numericx_translate() can
   handle correctly void number with any number of cases. Either with a
   from numerical system that is infinite (0 == 00 == 000 ...)  or if it
   is not (0 is void, 00 is concrete, 000 is concrete, ...)

Signed-off-by: Daniel Santos <dacs.git@brilhante.top>
-rw-r--r--numericx.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/numericx.c b/numericx.c
index ccf6f4f..41831e9 100644
--- a/numericx.c
+++ b/numericx.c
@@ -304,6 +304,39 @@ numeral_to_string(numeral_ptr* numeral, bool result_with_units_on_the_end)
 }
 
 /**
+ * @brief Check if number is void.
+ *
+ * Check if number is void by testing each digit to see if all of
+ * number's numerals correspond to void_numeral.
+ *
+ * @param number - Number string to be checked.
+ * @param void_numeral - Numeral that represents void.
+ *
+ * @result true if number is void.
+ * @result false if number is countable.
+ */
+static bool
+is_number_void(char* number, char* void_numeral, bool is_infinite)
+{
+	if( !is_infinite )
+	{
+		return ( (strlen(number) == 1) && (*number == *void_numeral) );
+	}
+	else
+	{
+		while( !(*number == '\0') )
+		{
+			if( *number != *void_numeral )
+				return false;
+
+			++number;
+		}
+
+		return true;
+	}
+}
+
+/**
  * @brief Translate string to a different numerical system.
  *
  * After definition of the 'from' numerical system proprieties and the
@@ -369,7 +402,7 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe
 	{
 		if( from_first_number_void )
 		{
-			if( strlen(number) == 1 && *number == *from_first )
+			if( is_number_void(number, from_first, from_infinite_base) )
 			{
 				free_numeral(counting);
 				free_numeral(result);