具有类似于 float8in_internal_null() 的整数类型函数吗?

huangapple go评论121阅读模式
英文:

Have a function similar to float8in_internal_null( ) for Integer types?

问题

在处理整数并使用 atoi() 函数时,我遇到了一个限制。与 float8in_internal_null() 不同,atoi() 在无法将列表元素转换为整数时不会返回 NULL。相反,它会返回 0。

AGE 中是否有某个库中的替代函数,可以帮助我实现所需的行为,即在无法将值转换为整数时返回 NULL?我会感激对此事的任何指导。

英文:

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.

答案1

得分: 0

你可以检查需要转换的字符串是否为零,如果不是零且atoi()返回0,则无法将字符串转换为整数。因此,您可以返回或传递NULL。

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

此外,如果您希望,可以使用AGE中的age_tointeger函数,在C函数中使用DirectFunctionCall调用此函数。

编辑:
然后,我们应该使用Postgres的strtoi64(string, &errorOK, 10)将普通整数字符串转换为整数,此函数处理诸如无效语法和超出范围的整数等错误。

英文:

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.

答案2

得分: 0

在C或C++中,与其他编程语言不同,没有像null或None这样的特殊值。在C-C++中,NULL只是一个将其定义为0的宏。因此,与其他语言不同,C-C++函数返回0。您需要添加额外的检查来验证是否是错误情况或0是否是正确的答案。

#include <iostream>
#include <string>

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

相反,如果您可以使用C++库,还可以使用std::stoi。它会在这种情况下引发异常,可以进行处理。

英文:

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 &lt;iostream&gt;
#include &lt;string&gt;

int main() {
    int i = atoi(num);
    if (num!=&quot;0&quot; &amp;&amp; i == 0)
    {
        cout&lt;&lt;&quot;Error&quot;&lt;&lt;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.

答案3

得分: 0

无法在同一个函数中将 NULL 同时作为字符串和整数返回。NULL 是一个宏,等同于 0,如果你将 NULL 作为整数返回,你会得到 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.

答案4

得分: 0

抱歉,没有像float8in_internal_null()这样的整数内置函数,它会在转换失败时返回NULL。您必须自己实现函数来实现这个结果。

英文:

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.

答案5

得分: 0

没有内置函数(建议的名称:int4in_internal_null())用于整数。

您需要编写自定义逻辑来解决这个问题。

或者

使用SQL中的**COALESCE()**函数。

或者

在此GitHub上创建一个新的问题,建议增加此功能。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月11日 12:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定