-
Notifications
You must be signed in to change notification settings - Fork 16
add parse functions #133
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
add parse functions #133
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
20b9624
add parse functions
ReneWerner87 6645d8a
Merge remote-tracking branch 'origin/master' into add_parse_functions
ReneWerner87 666ffe3
add parse functions
ReneWerner87 80889bb
increase performance
ReneWerner87 ed20571
increase performance
ReneWerner87 c261725
increase performance
ReneWerner87 676629a
increase performance
ReneWerner87 9ae1b92
fix linting and add more test coverage
ReneWerner87 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "math" | ||
| ) | ||
|
|
||
| type Signed interface { | ||
| ~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
| } | ||
| type Unsigned interface { | ||
| ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ||
| } | ||
|
|
||
| // ParseUint parses a decimal ASCII string or byte slice into a uint64. | ||
| // It returns the parsed value and true on success. | ||
| // If the input contains non-digit characters, it returns 0 and false. | ||
| func ParseUint[S byteSeq](s S) (uint64, bool) { | ||
| return parseUnsigned[S, uint64](s, uint64(math.MaxUint64)) | ||
| } | ||
|
|
||
| // ParseInt parses a decimal ASCII string or byte slice into an int64. | ||
| // Returns the parsed value and true on success, else 0 and false. | ||
| func ParseInt[S byteSeq](s S) (int64, bool) { | ||
| return parseSigned[S, int64](s, math.MinInt64, math.MaxInt64) | ||
| } | ||
|
|
||
| // ParseInt32 parses a decimal ASCII string or byte slice into an int32. | ||
| func ParseInt32[S byteSeq](s S) (int32, bool) { | ||
| return parseSigned[S, int32](s, math.MinInt32, math.MaxInt32) | ||
| } | ||
|
|
||
| // ParseInt8 parses a decimal ASCII string or byte slice into an int8. | ||
| func ParseInt8[S byteSeq](s S) (int8, bool) { | ||
| return parseSigned[S, int8](s, math.MinInt8, math.MaxInt8) | ||
| } | ||
|
|
||
| // ParseUint32 parses a decimal ASCII string or byte slice into a uint32. | ||
| func ParseUint32[S byteSeq](s S) (uint32, bool) { | ||
| return parseUnsigned[S, uint32](s, uint32(math.MaxUint32)) | ||
| } | ||
|
|
||
| // ParseUint8 parses a decimal ASCII string or byte slice into a uint8. | ||
| func ParseUint8[S byteSeq](s S) (uint8, bool) { | ||
| return parseUnsigned[S, uint8](s, uint8(math.MaxUint8)) | ||
| } | ||
|
|
||
| // parseDigits parses a sequence of digits and returns the uint64 value and success. | ||
| // Returns (0, false) if any non-digit is encountered or overflow happens. | ||
| func parseDigits[S byteSeq](s S, i int) (uint64, bool) { | ||
| var n uint64 | ||
| for ; i < len(s); i++ { | ||
| c := s[i] - '0' | ||
| if c > 9 { | ||
| return 0, false | ||
| } | ||
| nn := n*10 + uint64(c) | ||
| if nn < n { | ||
| return 0, false | ||
| } | ||
| n = nn | ||
| } | ||
| return n, true | ||
| } | ||
|
|
||
| // parseSigned parses a decimal ASCII string or byte slice into a signed integer type T. | ||
| // It supports optional '+' or '-' prefix, checks for overflow and underflow, and returns (0, false) on error. | ||
| func parseSigned[S byteSeq, T Signed](s S, minRange, maxRange T) (T, bool) { | ||
| if len(s) == 0 { | ||
| return 0, false | ||
| } | ||
|
|
||
| neg := false | ||
| i := 0 | ||
| switch s[0] { | ||
| case '-': | ||
| neg = true | ||
| i++ | ||
| case '+': | ||
| i++ | ||
| } | ||
| if i == len(s) { | ||
| return 0, false | ||
| } | ||
|
|
||
| // Parse digits | ||
| n, ok := parseDigits(s, i) | ||
| if !ok { | ||
| return 0, false | ||
| } | ||
|
|
||
| if !neg { | ||
| // Check for overflow | ||
| if n > uint64(int64(maxRange)) { | ||
| return 0, false | ||
| } | ||
| return T(n), true | ||
| } | ||
|
|
||
| // Check for underflow | ||
| minAbs := uint64(-int64(minRange)) | ||
| if n > minAbs { | ||
| return 0, false | ||
| } | ||
|
|
||
| return T(-int64(n)), true | ||
| } | ||
|
|
||
| // parseUnsigned parses a decimal ASCII string or byte slice into an unsigned integer type T. | ||
| // It does not support sign prefixes, checks for overflow, and returns (0, false) on error. | ||
| func parseUnsigned[S byteSeq, T Unsigned](s S, maxRange T) (T, bool) { | ||
| if len(s) == 0 { | ||
| return 0, false | ||
| } | ||
|
|
||
| i := 0 | ||
|
|
||
| // Parse digits | ||
| n, ok := parseDigits(s, i) | ||
| // Check for overflow | ||
| if !ok || n > uint64(maxRange) { | ||
| return 0, false | ||
| } | ||
| return T(n), true | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.