-
Notifications
You must be signed in to change notification settings - Fork 1.4k
refactor: replace Split in loops with more efficient SplitSeq #1586
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,8 +27,8 @@ func RunSubfinderAndGetResults(debug bool, domain string, extra ...string) ([]st | |||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
var parts []string | ||||||||||||||||||||||||
items := strings.Split(string(data), "\n") | ||||||||||||||||||||||||
for _, i := range items { | ||||||||||||||||||||||||
items := strings.SplitSeq(string(data), "\n") | ||||||||||||||||||||||||
for i := range items { | ||||||||||||||||||||||||
if i != "" { | ||||||||||||||||||||||||
parts = append(parts, i) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+30
to
34
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. Critical issue: Incorrect loop variable usage The refactoring from
Here's how to fix it: items := strings.SplitSeq(string(data), "\n")
- for i := range items {
- if i != "" {
- parts = append(parts, i)
+ for i := range items {
+ if items[i] != "" {
+ parts = append(parts, items[i])
}
} Alternatively, you could use the original value-based loop approach: items := strings.SplitSeq(string(data), "\n")
- for i := range items {
- if i != "" {
+ for _, item := range items {
+ if item != "" {
- parts = append(parts, i)
+ parts = append(parts, item)
}
} 📝 Committable suggestion
Suggested change
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. the function description: "SplitSeq returns an iterator over all substrings of s separated by sep. The iterator yields the same strings that would be returned by [Split](s, sep), but without constructing the slice. It returns a single-use iterator." 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. 🧩 Analysis chainLet me check the Go documentation and explore how @dogancanbakir Thank you for sharing the function description. Based on this, I see the issue with the current implementation. Since Here's the correct way to use var parts []string
iter := strings.SplitSeq(string(data), "\n")
for iter.Next() {
item := iter.Value()
if item != "" {
parts = append(parts, item)
}
} The current PR implementation is incorrect because it's treating the iterator as a slice. With 🏁 Script executed:
Length of output: 3169 @dogancanbakir Thanks for providing that function description. Looking at the Go documentation, The current implementation is incorrect because it's trying to range over an iterator and directly use the index variable as a string. Here's the correct way to use var parts []string
iter := strings.SplitSeq(string(data), "\n")
for iter.Next() {
item := iter.Value()
if item != "" {
parts = append(parts, item)
}
} Since ✏️ Learnings added
|
||||||||||||||||||||||||
|
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.
Can we update other instances as well?