-
Notifications
You must be signed in to change notification settings - Fork 0
Leverage dart:typed_data for validating the values of certain numeric data types #71
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ecf64ec
Update XsdShortCodec to use typed_data
mark-dropbear d72c069
Update XsdByteCodec to use typed_data
mark-dropbear d7a9e3f
Update XsdIntCodec to use typed_data
mark-dropbear 76600f0
Update XsdUnsignedByteCodec to use typed_data
mark-dropbear 411d305
Rename Unsigned Short Codec
mark-dropbear d3d2c89
Update XsdUnsignedShort to use typed_data
mark-dropbear cac367f
Run dart format
mark-dropbear f6dabba
Add some more explicit checks for blank inputs
mark-dropbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,35 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import '../../helpers/whitespace.dart'; | ||
|
|
||
| class XsdShortDecoder extends Converter<String, int> { | ||
| const XsdShortDecoder(); | ||
|
|
||
| static const int _minValue = -32768; | ||
| static const int _maxValue = 32767; | ||
|
|
||
| @override | ||
| int convert(String input) { | ||
| final String collapsedInput = processWhiteSpace(input, Whitespace.collapse); | ||
| final str = processWhiteSpace(input, Whitespace.collapse); | ||
|
|
||
| if (str.isEmpty) { | ||
| throw const FormatException('The input string cannot be empty.'); | ||
| } | ||
|
|
||
| final value = int.tryParse(str); | ||
|
|
||
| final int? value = int.tryParse(collapsedInput); | ||
| if (value == null) { | ||
| throw FormatException( | ||
| "Invalid XSD short lexical format: '$input' (collapsed to '$collapsedInput')", | ||
| ); | ||
| throw FormatException('The input "$str" is not a valid integer.'); | ||
| } | ||
mark-dropbear marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Check bounds for xsd:short | ||
| if (value < _minValue || value > _maxValue) { | ||
| // Use Int16List to validate that the value fits within 16 bits (wraps on overflow). | ||
| final list = Int16List(1); | ||
| list[0] = value; | ||
|
|
||
| if (list[0] != value) { | ||
| throw FormatException( | ||
| "Value '$collapsedInput' is out of range for xsd:short. Must be between $_minValue and $_maxValue.", | ||
| 'The value "$value" is out of range for xsd:short.', | ||
| ); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| /// A [Converter] that converts an [int] to an XSD `short` string. | ||
| class XsdShortEncoder extends Converter<int, String> { | ||
| const XsdShortEncoder(); | ||
|
|
||
| static const int _minValue = -32768; | ||
| static const int _maxValue = 32767; | ||
|
|
||
| @override | ||
| String convert(int input) { | ||
| if (input < _minValue || input > _maxValue) { | ||
| // Use Int16List to validate that the value fits within 16 bits (wraps on overflow). | ||
| final list = Int16List(1); | ||
| list[0] = input; | ||
| if (list[0] != input) { | ||
| throw FormatException( | ||
| "Value '$input' is out of range for xsd:short. Must be between $_minValue and $_maxValue.", | ||
| 'The value "$input" is out of range for xsd:short.', | ||
| ); | ||
| } | ||
|
|
||
| return input.toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.