Re: Weird warning about data type range
In article <ad7ec9cc-6e85-4d12-8ebf-6072d20e2617
@z4g2000prh.googlegroups.com>, digital_puer@hotmail.com says...
Thanks for everyone's help.
What I am really trying to do is to test if
characters in a std::string are alphanumeric
characters in the Latin-1 encoding. I have
the following:
#define IS_ALPHANUMERIC(x) ( \
((x) >= 48 && (x) <= 57 ) || \
((x) >= 65 && (x) <= 90 ) || \
((x) >= 97 && (x) <= 122) || \
((x) >= 192 && (x) <= 214) || \
((x) >= 216 && (x) <= 246) || \
((x) >= 248 && (x) <= 255) )
string s = getLatin1Text();
int len = s.size();
for (int i = 0; i < len; i++)
{
if (! IS_ALPHANUMERIC(s.at(i)))
{
...
}
}
If I had to do this, I think I'd use something like this:
#include <climits>
#include <vector>
#include <algorithm>
struct alphanumeric_table {
std::vector<bool> table;
public:
#define elements(r) (sizeof(r)/sizeof(r[0]))
alphanumeric_table() : table(UCHAR_MAX+2, false) {
static const int ranges[] = {
48, 57,
65, 90,
97, 122,
192, 214,
216, 246,
248, 255
};
for (int i=0; i<elements(ranges); i+=2)
std::fill(table.begin()+ranges[i]+1,
table.begin()+ranges[i+1]+2,
true);
}
bool operator[](int n) { return table[unsigned char(n+1)]; }
} alpha_table;
inline bool is_alphanumeric(int n) {
return alpha_table[n];
}
This assumes that EOF is -1. Technically this isn't required (any
negative value is allowed) but it's extremely common -- to the point
that I'm not sure I've ever seen or heard of it actually having any
other value. In any case, the reason for the "+1"in most places is to
get a range from 0 through the maximum, so we can use it directly as
an index into the vector.
--
Later,
Jerry.