开发者

How Can I Check if a string represents an positive, non-zero int?

I don't need to know WHAT the int is, I just need to know if it is a positive, non-zero integer representation in Decimal representation, with no leading 0's.

It's going to be called for a large number of records, so I'd like it to be as inexpensive a check as possible.

The e开发者_如何学Cxpected behaviour is that the system should never be passed something that doesn't validate (because it's normally passed ints which it converts to strings for storage) so this is just a final safety check to make sure nothing weird's happened.


Whilst you could actually go through the process of actually converting it to an int, my assumption is that what you really want to know is whether all the characters in it are digits?

Unfortunately even then there is no option but do linearly run through the string, although this should be faster than converting to an integer first.

Using STL you can use std::find and ::isdigit and std::not1

template<typename FwdIter>
bool all_digits( FwdIter start, FwdIter end )
{
   return std::find( start, end, std::not1(::isdigit) ) == end;
}

Of course you could just write a loop

template<typename FwdIter>
bool all_digits( FwdIter start, FwdIter end )
{
   for( ; start != end; ++start )
   {
     if( !::isdigit( *start ) )
        return false;
   }
   return true;
}

That will not totally tell you if an input string represents a positive number as they might all be zeros, and the string might be empty. We can cover that in the loop version easily.

template<typename FwdIter>
bool is_positive_int( FwdIter start, FwdIter end )
{
   bool foundNonZero;
   for( ; start != end; ++start )
   {
     if( !::isdigit( *start ) )
        return false;
     if( *start > '0' ) // it must be a digit
        foundNonZero = true;
   }
   return foundNonZero;
}

Assumptions:

  • You may have leading zeros (but there must be at least one non-zero in there) so 0234 is a valid positive number
  • No whitespace allowed


  • first character must be one of 1 through 9
  • all other characters must be one of 0 through 9

Check the first, loop over the rest, easy-peasy.


it is indeed quite straightforward, but it depends on the requirements/allowed input:

  • what representation/basis are allowed: binary, octal, hexadecimal? (the main difference is made by hex, your digits set is different)
  • are leading and trailing whitespace allowed (your input is a string, you should not disregard this unless you are promised it will not happen)
  • are leading zeros allowed?
  • is leading + allowed?

based on the above, you may need to:

  • strip leading and trailing whitespace
  • scan the string checking that it contains only the allowed digits and a +
  • verify that it contains at least one non-zero digit

the question is also do you need to validate the number, e.g. the input 123+456

you can write a simple state machine that would do all of the above, if needed.

EDIT

if I understand your requirements, one subsystem converts positive ints to strings which are eventually get to another subsystem that wants to validate that nothing happened to the input. if you know your int-to-string conversion, the task is much easier: assuming no whitespace, no leading zeros, no leading +, than your task is indeed "easy-peasy" as in the first answer :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜