-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add AppendInt function with positive/negative number convertion to string #90
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e60c5a7
Add AppendInt function with positive/negative number convertion to st…
ReneWerner87 87790b8
Add AppendInt function with positive/negative number convertion to st…
ReneWerner87 9c0bea3
Add AppendInt function with positive/negative number convertion to st…
ReneWerner87 ac59843
Add AppendInt function with positive/negative number convertion to st…
ReneWerner87 14f52bc
Add AppendInt function with positive/negative number convertion to st…
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 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 |
---|---|---|
|
@@ -151,3 +151,33 @@ func ToString(arg any, timeFormat ...string) string { | |
return fmt.Sprint(arg) | ||
} | ||
} | ||
|
||
// AppendInt appends the string representation of the int n to dst and returns the extended buffer. | ||
func AppendInt(dst []byte, n int) []byte { | ||
isNegative := n < 0 | ||
if isNegative { | ||
// Convert the number to positive | ||
n = -n | ||
} | ||
Comment on lines
+156
to
+161
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. Correctness: Handle integer overflow for negative values. When converting a negative integer to positive, be cautious of integer overflow. The edge case for if isNegative {
if n == math.MinInt {
// Handle the edge case for the smallest possible integer
dst = append(dst, "-9223372036854775808"...)
return dst
}
n = -n
} |
||
|
||
var b [20]byte | ||
buf := b[:] | ||
i := len(buf) | ||
var q int | ||
for n >= 10 { | ||
i-- | ||
q = n / 10 | ||
buf[i] = '0' + byte(n-q*10) | ||
n = q | ||
} | ||
i-- | ||
buf[i] = '0' + byte(n) | ||
|
||
if isNegative { | ||
// add '-' in front of the number | ||
dst = append(dst, '-') | ||
} | ||
dst = append(dst, buf[i:]...) | ||
|
||
return dst | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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.
I think we need a separate function for
AppendUint
that usesuint
as param, else we won't be able to handle values bigger than2,147,483,647
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.
The
math
module in Golang defines these: