0
Follow
2
View

Inconsistent output from gcount()

dunkyyan 注册会员
2023-01-24 23:53

The basic problem you have is with the operator<< functions. You've tried two of them:

  • operator<<(ostream &, const char *) which will take characters from the pointer up to (and not including) the next NUL. As you've noted, that may be a problem if the pointer comes from a string_view without a terminating NUL.
  • operator<<(ostream &, const string_view &) which will take all the characters from the string_view including any NULs that may be present.

It seems that what you want to do is take characters from the string_view up to (and not including) the first NUL or the end of the string_view, whichever comes first. You can do that with find and constructing a substr up to the NUL or end:

ss << inputStr.substr(0, inputStr.find('\0'));