简体   繁体   English

strlen 与零长度字符串不一致

[英]strlen inconsistent with zero length string

I'm creating a DataStage parallel routine, which is a C or C++ function that is called from within IBM (formerly Ascential) DataStage.我正在创建一个 DataStage 并行例程,它是一个从 IBM(以前称为 Ascential)DataStage 内部调用的 C 或 C++ 函数。 It is failing if one of the strings passed in is zero length.如果传入的字符串之一长度为零,则失败。 If I put this at the very first line of the function:如果我把它放在函数的第一行:

return strlen(str);

then it returns 0 for the calls that pass in empty values into str.然后它为将空值传入 str 的调用返回 0。 If I put this at the first line, however...如果我把它放在第一行,但是......

if (strlen(str)==0) {return 0;}

then it does not return and goes into an infinite loop然后它不会返回并进入无限循环

I'm baffled - it works fine in a test harness, but not in DataStage.我很困惑 - 它在测试工具中工作正常,但在 DataStage 中却没有。

Maybe there is something odd about the way DataStage passes empty strings to C routines?也许 DataStage 将空字符串传递给 C 例程的方式有些奇怪?

int pxStrFirstCharList(char *str, char *chars )
{
  if (strlen(str)==0) {return 0;}
  if (strlen(chars)==0) {return 0;}
  int i = 0;
  //Start search
  while (str[i]) //for the complete input string
  {
    if (strchr(chars, str[i]))
    {
      return i+1;
    }
    ++i;
  }
  return 0;
}

There is a builtin function for what you are doing, it's called strcspn .您正在执行的操作有一个内置函数,称为strcspn This function takes two strings, and searches the first one for the first occurance of any of the characters of the second string.此函数采用两个字符串,并搜索第一个字符串以查找第二个字符串中任何字符的第一次出现。

I suggest using that than RYO...我建议使用它而不是 RYO ...

http://www.cplusplus.com/reference/clibrary/cstring/strcspn/http://www.cplusplus.com/reference/clibrary/cstring/strcspn/

I guess it is the strlen's issue when the length of the string is 0. For example,当字符串的长度为 0 时,我猜这是 strlen 的问题。例如,

char s1[0];字符 s1[0]; char *s2="a";字符 *s2="a";

printf("%d %s\\n", sizeof(s1), s1);//0 @ printf("%d %s\\n", sizeof(s1), s1);//0 @

printf("%d %s\\n", strlen(s1), s1);//3 @ printf("%d %s\\n", strlen(s1), s1);//3 @

printf("%d %s\\n", sizeof(s2), s2);//8 a printf("%d %s\\n", sizeof(s2), s2);//8 a

printf("%d %s\\n", strlen(s2), s2);// 1 a printf("%d %s\\n", strlen(s2), s2);// 1 a

You will get a weird answer for using strlen and you can check its source code in detail( https://code.woboq.org/userspace/glibc/string/strlen.c.html ).你会得到一个关于使用 strlen 的奇怪答案,你可以详细检查它的源代码( https://code.woboq.org/userspace/glibc/string/strlen.c.html )。 In nutshell, you can use sizeof instead of strlen for char string or avoid 0 length case by using strlen.简而言之,您可以对字符字符串使用 sizeof 而不是 strlen,或者使用 strlen 避免长度为 0 的情况。

How about this?这个怎么样?

int pxStrFirstCharList(char *str, char *chars )
{
  if (str && chars && (0 != strlen(str)) && (0 != strlen(chars)))
  {
    int i = 0;
    //Start search
    while (str[i]) //for the complete input string
    {
      if (strchr(chars, str[i]))
      {
        return i+1;
      }
      ++i;
    }
  }
  return 0;
}

Also, I don't quite get the point of the while loop ... (and no, I don't mean that this could be written as for ).另外,我不太明白while循环的重点......(不,我并不是说这可以写成for )。 What I mean is that on one hand you are doing a search ( strstr ) that itself will be implemented as a loop and still you have some outer loop.我的意思是,一方面你正在做一个搜索( strstr ),它本身将作为一个循环来实现,但你仍然有一些外循环。 Could it be that you actually wanted to have chars in its place, ie:难道你真的想在它的位置有chars ,即:

int pxStrFirstCharList(char *str, char *chars )
{
  if (str && chars && (0 != strlen(str)) && (0 != strlen(chars)))
  {
    int i = 0;
    //Start search
    while (chars[i]) //for the complete input string
    {
      if (strchr(str, chars[i]))
      {
        return i+1;
      }
      ++i;
    }
  }
  return 0;
}

...? ……? That is, look for each of the characters within chars inside the string denoted by str ...也就是说,在由str表示的字符串中查找chars每个chars ...

If NULL is not explicitly part of the game, at least during development phase, it's always a good idea to add a precondition check on pointers received by a function:如果 NULL 不是游戏的明确组成部分,至少在开发阶段,对函数接收的指针添加前提条件检查总是一个好主意:

int pxStrFirstCharList(char *str, char *chars )
{
  if (!str)
    return -1;

  if (!chars)
    return -2;
....

(The negative values -1 and -2 than tell the caller that something went wrong) (负值 -1 和 -2 告诉调用者出现问题)

Or doing it in a more relaxed way, silently accepting NULL pointer strings as ""-string:或者以更轻松的方式进行,默默地接受 NULL 指针字符串作为 ""-string:

int pxStrFirstCharList(char *str, char *chars )
{
  if (!str)
    return 0;

  if (!chars)
    return 0;
...

If you are the only one using this API you could #ifndef BUILD_RELEASE these checks away for a release build if anything is tested stable.如果你是唯一一个使用这个 API 的人,你可以#ifndef BUILD_RELEASE这些检查是否有任何稳定的发布版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM