-
Notifications
You must be signed in to change notification settings - Fork 182
Handle StringValue unint parsing when input is invalid (usually becau… #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle StringValue unint parsing when input is invalid (usually becau… #199
Conversation
…nt 'casting' but not useful in C#
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide details of a scenario that benefits from this? It'd be good to include such information in a comment too.
{ | ||
return uint.Parse(ToString()); | ||
} | ||
catch(Exception) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we capture a more specific kind of exception here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could have it capture the three exceptions that uint.Parse can throw - ArgumentNullException, FormatException, OverflowException
foreach (var b in Bytes) | ||
{ | ||
val = val << 8; | ||
val += b; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not this approach makes sense if there are more than four bytes (or perhaps also when fewer).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was copied from ToInt32, which was copied from Java. I could try to run more tests here but this code has been in both libraries for a while.
@@ -87,7 +87,23 @@ int IConvertible.ToInt32(IFormatProvider provider) | |||
|
|||
ushort IConvertible.ToUInt16(IFormatProvider provider) => ushort.Parse(ToString()); | |||
|
|||
uint IConvertible.ToUInt32(IFormatProvider provider) => uint.Parse(ToString()); | |||
uint IConvertible.ToUInt32(IFormatProvider provider) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only uint
? Why not all the other supported integral types as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't come across an issue that involved the other types so decided to leave them be for now.
This is a rare exception, but this image from the images repo exhibits the problem: jpg/Issue 122.jpg IPTC tag "File Format" (0114) was blank in .NET. The reason is the bytes in this StringValue are set to decode using UTF-8 and the result string has encoding characters in it: \0\u0003 Java catches the parsing exception but implements something similar to this PR to overcome it manually; .NET simply swallows the exception. This is a very rare situation in the entire images repo. I figured since a work-around has been in the Java library for so long and this is effectively a port, it was reasonable. But, I didn't exhaustively test whether the Java port is 100% correct for .NET. |
…se of encoding)