-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] Add a new validate
parameter to the b64decode()
function
#3929
Open
msaelices
wants to merge
6
commits into
modular:nightly
Choose a base branch
from
msaelices:b64decode-validation
base: nightly
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
565284c
Add a new validate parameter to the b64decode() function
msaelices b32f2f5
Add changelog entry for the b64decode() new validate param
msaelices a70a32a
Merge branch 'nightly' into b64decode-validation
msaelices 34464d8
Merge branch 'nightly' into b64decode-validation
msaelices c1873d6
Improve error messages including the length and the problematic char
msaelices 7786317
Merge branch 'nightly' into b64decode-validation
msaelices 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 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 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 |
---|---|---|
|
@@ -112,17 +112,24 @@ fn b64encode(input_bytes: Span[Byte, _]) -> String: | |
|
||
|
||
@always_inline | ||
fn b64decode(str: StringSlice) -> String: | ||
fn b64decode[validate: Bool = False](str: StringSlice) raises -> String: | ||
"""Performs base64 decoding on the input string. | ||
|
||
Parameters: | ||
validate: If true, the function will validate the input string. | ||
|
||
Args: | ||
str: A base64 encoded string. | ||
|
||
Returns: | ||
The decoded string. | ||
""" | ||
var n = str.byte_length() | ||
debug_assert(n % 4 == 0, "Input length must be divisible by 4") | ||
|
||
@parameter | ||
if validate: | ||
if n % 4 != 0: | ||
raise Error("ValueError: Input length must be divisible by 4") | ||
|
||
var p = String._buffer_type(capacity=n + 1) | ||
|
||
|
@@ -133,10 +140,10 @@ fn b64decode(str: StringSlice) -> String: | |
var c = _ascii_to_value(str[i + 2]) | ||
var d = _ascii_to_value(str[i + 3]) | ||
|
||
debug_assert( | ||
a >= 0 and b >= 0 and c >= 0 and d >= 0, | ||
"Unexpected character encountered", | ||
) | ||
@parameter | ||
if validate: | ||
if a < 0 or b < 0 or c < 0 or d < 0: | ||
raise Error("ValueError: Unexpected character encountered") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion Include the unexpected character value in the error message. This may mean pulling the snippet out into a local function and call it for each of the values |
||
|
||
p.append((a << 2) | (b >> 4)) | ||
if str[i + 2] == "=": | ||
|
This file contains 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
Oops, something went wrong.
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.
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.
Suggestion Include the byte length value (
n
) in the error message.