-
Notifications
You must be signed in to change notification settings - Fork 916
GODRIVER-3605 Refactor StringN #2128
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
Open
qingyang-hu
wants to merge
3
commits into
mongodb:master
Choose a base branch
from
qingyang-hu:godriver3605
base: master
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.
+533
−575
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ package bsoncore | |
import ( | ||
"fmt" | ||
"io" | ||
"math" | ||
"strconv" | ||
"strings" | ||
) | ||
|
@@ -83,55 +82,73 @@ func (a Array) DebugString() string { | |
// String outputs an ExtendedJSON version of Array. If the Array is not valid, this method | ||
// returns an empty string. | ||
func (a Array) String() string { | ||
return a.StringN(math.MaxInt) | ||
str, _ := a.stringN(0) | ||
return str | ||
} | ||
|
||
// StringN stringifies an array upto N bytes | ||
func (a Array) StringN(n int) string { | ||
if lens, _, _ := ReadLength(a); lens < 5 || n <= 0 { | ||
return "" | ||
func (a Array) StringN(n int) (string, bool) { | ||
if n <= 0 { | ||
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. Consider treating n<=0 as "no limit", see comment on document.StringN(). |
||
if l, _, ok := ReadLength(a); !ok || l < 5 { | ||
return "", false | ||
} | ||
return "", true | ||
} | ||
return a.stringN(n) | ||
} | ||
|
||
// stringN stringify an array. If N is larger than 0, it will truncate the string to N bytes. | ||
func (a Array) stringN(n int) (string, bool) { | ||
length, rem, ok := ReadLength(a) | ||
if !ok || length < 5 { | ||
return "", false | ||
} | ||
length -= (4 /* length bytes */ + 1 /* final null byte */) | ||
|
||
var buf strings.Builder | ||
buf.WriteByte('[') | ||
|
||
length, rem, _ := ReadLength(a) // We know we have enough bytes to read the length | ||
length -= 4 | ||
|
||
var truncated bool | ||
var elem Element | ||
var ok bool | ||
|
||
if n > 0 { | ||
for length > 1 { | ||
elem, rem, ok = ReadElement(rem) | ||
|
||
length -= int32(len(elem)) | ||
if !ok { | ||
return "" | ||
} | ||
|
||
str := elem.Value().StringN(n - buf.Len()) | ||
|
||
buf.WriteString(str) | ||
|
||
if buf.Len() == n { | ||
return buf.String() | ||
var str string | ||
first := true | ||
for length > 0 && !truncated { | ||
l := 0 | ||
if n > 0 { | ||
if buf.Len() >= n { | ||
truncated = true | ||
break | ||
} | ||
|
||
if length > 1 { | ||
buf.WriteByte(',') | ||
l = n - buf.Len() | ||
} | ||
if !first { | ||
buf.WriteByte(',') | ||
if l > 0 { | ||
l-- | ||
if l == 0 { | ||
truncated = true | ||
break | ||
} | ||
} | ||
} | ||
if length != 1 { // Missing final null byte or inaccurate length | ||
return "" | ||
|
||
elem, rem, ok = ReadElement(rem) | ||
length -= int32(len(elem)) | ||
if !ok || length < 0 { | ||
return "", false | ||
} | ||
|
||
str, truncated = elem.Value().stringN(l) | ||
buf.WriteString(str) | ||
|
||
first = false | ||
} | ||
|
||
if buf.Len()+1 <= n { | ||
if n <= 0 || (buf.Len() < n && !truncated) { | ||
buf.WriteByte(']') | ||
} | ||
|
||
return buf.String() | ||
return buf.String(), truncated | ||
} | ||
|
||
// Values returns this array as a slice of values. The returned slice will contain valid values. | ||
|
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.
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 line causes GODRIVER-3561