How to find invalid character set in string/char array

I am having a string/char array in which I want to test if any invalid character set(which is out of 0-127 standard character) exist.So what are the standard library functions available for this?

Thanks
Use strpbrk().
This requires second argument as character to match.This character I can't provide, as my purpose is to just check whether given string contains any invalid character set.

Any suggestions would be appreciated.
http://www.cplusplus.com/reference/algorithm/find_if/

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <functional>

bool isASCII( int c )
  {
  return (c >= 0) && (c < 128);
  }

bool isASCII( const std::string& s )
  {
  return std::find_if( s.begin(), s.end(), std::not1( isASCII ) ) != s.end();
  }
closed account (z05DSL3A)
Should isASCII() not be derived from unary_function, when used with std::not1()?

1
2
3
4
5
6
7
struct isASCII : unary_function<int,bool> 
{
    bool operator() (const int& c) const 
    {
        return (c >= 0) && (c < 128);
    }
};
Last edited on
Oh, god.. You guys, this is a job for a for loop, not std::algorithm!
1
2
3
4
bool isAllASCII(const char *s){
	for(;*s!=0;s++)if(*s&0x80)return 0;
	return 1;
}
Last edited on
closed account (z05DSL3A)
rocketboy9000,
forstudy3 wrote:
So what are the standard library functions available for this?

Right, but you're also making up your own, slower, function. There is no standard library function specialized for finding ascii characters. In UNIX there is isascii() from ctype, but he didn't specify his platform so I assumed windows, the most common one.
EDIT: Actually, I'm sorry your code just doesn't compile. I'm attempting to fix it and test it.
Last edited on
Ok, got it to compile. It's 6 times slower to run, and compiles in the same time as my entire tetris implementation.
I just built rocketboy9000's "C" version and Duoas' STL version. They run in a loop 50M times, both take 1m 5s on my machine.

The C version is not faster than the C++ on Visual Studio 2008 with full optimisation (/Ox) on Windows. And both programs are 8704 bytes.
Oh, right, optimization. forgot about that. My programs are usually fast without optimization, so I often forget about it. What about compile time?
Last edited on
closed account (z05DSL3A)
Is it better to use a function object or a function pointer in std::algorithms

1
2
3
4
5
6
7
8
9
10
11
12
struct is_non_ASCII 
{
    inline bool operator() (const int& c) const 
    {
        return (c < 0) || (c >= 128);
    }
};

bool is_All_ASCII( const std::string& s )
{
    return std::find_if( s.begin(), s.end(), is_non_ASCII() ) == s.end();
}


1
2
3
4
5
6
7
8
9
bool is_non_ASCII_2(const int & c)
{
    return  ((c < 0) || (c >= 128));
}

bool is_All_ASCII_2( const std::string& s )
{
    return std::find_if( s.begin(), s.end(), is_non_ASCII_2 ) == s.end();
}
NB: Head parser, I think the code is correct ;0)

I have a feeling that function objects are better but I can't remember where I got this from. It is to do with the compiler being ably to optimise them better.
Last edited on
I was going to use a functor, but I didn't want to overload the OP with stuff...
My tests indicate that functors are faster, on par with mine, whereas function pointers are a little slower.
I was going to use a functor, but I didn't want to overload the OP with stuff...

yah, it is very inconveninet to write a small functor
obviously, we need lambda
Last edited on
The C++ version builds in 12 seconds. The C version isn't much faster. Most of the time is spent running resource compiler (which does nothing as there's no input) and running the linker with its manifest nonsense.

What kills C++ compile times is processing those massive include files. But on a single file, the overhead is in the tools.
Topic archived. No new replies allowed.