-2

I am currently developing a function that converts a list of values to integers. In the case of float types, we have the convenient float8in_internal_null() function at toFloatList(), which returns the appropriate float value if the conversion is successful, but returns NULL if the conversion of a list element fails.

However, when working with integers and using the atoi() function, I encountered a limitation. Unlike float8in_internal_null(), atoi() does not return NULL when it fails to convert an element of the list to an integer. Instead, it returns 0.

Is there an alternative function within some library in AGE, that can help me achieve the desired behavior of returning NULL when a conversion to an integer is not possible? I would appreciate any guidance on this matter.

Marcos Silva
  • 115
  • 5
  • 2
    In which language are you developing this function? Please select _one_. – Ted Lyngmo Jul 11 '23 at 04:19
  • 2
    C or C++? In C a function can only return one type so I assume this can't be C. As far as I recall the same goes for C++ but I haven't done C++ for a while so perhaps I don't remember that correctly. Anyway, in C I would take a look at `strtoul`. With a few extra checks it seems you can (nearly) reach your goal. – Support Ukraine Jul 11 '23 at 04:43
  • `NULL` is not a valid value for `float` in C, and `nullptr` is not a valid value for `float` in C++ either. I.e. the premise of the question is already flawed. However, most C implementations do support `NaN` (not-a-number), which is somewhat similar. On the flip side, hardly any C implementation supports `NaN` for `int` types. – MSalters Jul 11 '23 at 08:37
  • Also, pick a language. In C++ a failure to convert would cause a `std::runtime_error` exception, possibly a `std::range_error`. See also `std::stoi`. – MSalters Jul 11 '23 at 08:41

5 Answers5

0

You can use check if the string you need to convert is zero, if it's not zero and atoi() returns 0, then the string cannot be converted to an integer. Therefore, you can return or pass NULL.

int result = atoi(string);
if (strcmp(string, "0") != 0 && result == 0)
{
    return NULL;
}

Additionally, if you want you can use the function age_tointeger from AGE, calling this function inside your C function using DirectFunctionCall.

EDIT: We should then utilize strtoi64(string, &errorOK, 10) from Postgres to convert a regular integer string, this function handles errors like invalid syntax and out of range integer.

Wendel
  • 763
  • 1
  • 12
  • You had better recommend `strtol`, which has ways to report an error. – Laurenz Albe Jul 11 '23 at 06:51
  • Other strings for which `atoi` correctly returns zero are `+0`, `-0`, `00`, and `0foo` (because `atoi` is specified to convert the initial portion of the string), so the test in this routine is inadequate. Additionally, while OP may have observed `atoi` returning zero on error, this is not necessarily always so, as the C standard does not specify it, and so it should not be relied upon. When there is an overflow, the routine is likely to return some non-zero value. – Eric Postpischil Jul 11 '23 at 11:25
0

In C or C++, there is no special value like null or None like other languages. NULL in C-C++ is just a macro that defines it as 0. So, unlike other languages, C-C++ functions return 0. You need to add an additional check to verify if the case is an error or if 0 is the right answer.

#include <iostream>
#include <string>

int main() {
    int i = atoi(num);
    if (num!="0" && i == 0)
    {
        cout<<"Error"<<endl;
    }
}

Instead, if you can use C++ libraries, you can also use std::stoi. It raises an exception in such cases, which can be handled.

abhishek2046
  • 312
  • 1
  • 11
0

It's impossible to return NULL as a string and also return as an int in the same function. NULL is a macro which is equivalent to 0 if you return NULL as an int you get 0 which defeats the purpose of your intended functionality.

Peter
  • 43
  • 4
0

Unfortunately, there is no built in function for integers like float8in_internal_null() that will return NULL in case of conversion failure. You have to implement your own function to achieve this result.

adil shahid
  • 125
  • 4
0

There is no built-in function ( Suggested name: int4in_internal_null() ) for integers.

You need to write custom logic to tackle this problem.

OR

Use the COALESCE() function from SQL.

OR

Create a new issue on this GitHub for feature suggestion.