Digits to numeric value?
-
This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.
unsigned Bogus(size n_digits)
{
string_type digits;
// Gather digits.
while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
{
digits += input_.pop();
--n_digits;
}// Convert digits string to numeric value. unsigned n = 0; if( !digits.empty()g ) { std::basic\_istringstream iss(digits); iss >> n; } // return result return( n );
}
Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.
unsigned Bogus(size n_digits)
{
string_type digits;
// Gather digits.
while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
{
digits += input_.pop();
--n_digits;
}// Convert digits string to numeric value. unsigned n = 0; if( !digits.empty()g ) { std::basic\_istringstream iss(digits); iss >> n; } // return result return( n );
}
Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
You should post the answer, this isn't a quiz forum.
-
You should post the answer, this isn't a quiz forum.
Then someone will be saying that I should have waited to post the answer, so they have a chance to see it. Normally if it is related to the standard I see it before reading the answer, but this one is completely hidden. This is a subtle bug related to the STL and I expect no one to know what it is. Of course whether it is a bug or not is still up for grabs. Hint: It is not a coding error. P.S. If you do not want to learn to fish, then go starve.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.
unsigned Bogus(size n_digits)
{
string_type digits;
// Gather digits.
while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
{
digits += input_.pop();
--n_digits;
}// Convert digits string to numeric value. unsigned n = 0; if( !digits.empty()g ) { std::basic\_istringstream iss(digits); iss >> n; } // return result return( n );
}
Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
Given the chance that someone is able to and enters a fullwidth digit U+FF10 to U+FF19 (don't ask me why one would do that), std::isdigit(...) returns true - at least the VS2003 implementation does. However the conversion does not work for fullwidth digits. Well, I didn't even know that the STL is Unicode-aware in some ways ...
-
Then someone will be saying that I should have waited to post the answer, so they have a chance to see it. Normally if it is related to the standard I see it before reading the answer, but this one is completely hidden. This is a subtle bug related to the STL and I expect no one to know what it is. Of course whether it is a bug or not is still up for grabs. Hint: It is not a coding error. P.S. If you do not want to learn to fish, then go starve.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
You can post it as a reply to the original post so readers have a choice.
-
Given the chance that someone is able to and enters a fullwidth digit U+FF10 to U+FF19 (don't ask me why one would do that), std::isdigit(...) returns true - at least the VS2003 implementation does. However the conversion does not work for fullwidth digits. Well, I didn't even know that the STL is Unicode-aware in some ways ...
You just won the gold medal! :cool::cool::cool::cool::cool: That does not only apply to fullwidth (Chinese) digits, it applies to all digits from various languages and eras. If you want to know the starting digit (digit) 0 for various countries that MS knows about, just step into “_wtoi” to see how they solve the problem. I just opted to only accept ASCII digits, because that satisfied my requirements. Check out other countries, how a squiggly line is equivalent to a digit is beyond me but it is.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
You can post it as a reply to the original post so readers have a choice.
Good point! But "Doc Lobster" has already posted what is wrong, and he did not need to know the cheat codes. Too bad, I was hopping to surprise everyone. :laugh:
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
You just won the gold medal! :cool::cool::cool::cool::cool: That does not only apply to fullwidth (Chinese) digits, it applies to all digits from various languages and eras. If you want to know the starting digit (digit) 0 for various countries that MS knows about, just step into “_wtoi” to see how they solve the problem. I just opted to only accept ASCII digits, because that satisfied my requirements. Check out other countries, how a squiggly line is equivalent to a digit is beyond me but it is.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
Thanks! :) I've also tried other unicode numerics, as circled numbers, super- and subscript numbers, but isdigit(...) returned false for them. It seems that the Nd (Numeric-decimal) type from the UCD is checked.