Skip to content

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

findnature
Copy link

@findnature findnature commented May 2, 2025

Optimize code using a more modern writing style. Official support from Go, for more details visit https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of output line splitting to enhance reliability when processing multi-line data.

Copy link

coderabbitai bot commented May 2, 2025

Walkthrough

The code modifies the method for splitting a string into lines by replacing the use of strings.Split with strings.SplitSeq. The iteration over the resulting slice is also changed from value-based to index-based. There is an update in the loop's condition, now incorrectly checking if the index is not an empty string, instead of checking the line content. No changes are made to exported or public entity declarations.

Changes

File(s) Change Summary
v2/pkg/testutils/integration.go Replaced strings.Split with strings.SplitSeq for line splitting; changed loop from value-based to index-based iteration; updated conditional check inside the loop.

Poem

A hop and a split, my code takes a leap,
From Split to SplitSeq, the lines now run deep.
Indices march where values once lay,
But beware, dear coder, of checks gone astray!
In the garden of logic, let clarity stay.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 678fbc8 and 1227677.

📒 Files selected for processing (1)
  • v2/pkg/testutils/integration.go (1 hunks)
🔇 Additional comments (1)
v2/pkg/testutils/integration.go (1)

30-30:

✅ Verification successful

Verify SplitSeq is available in your Go version

The PR mentions replacing Split with SplitSeq for efficiency, but strings.SplitSeq is not a standard function in the Go standard library as of my knowledge. Please ensure this function exists in your environment or that you're using a custom package that provides it.


🏁 Script executed:

#!/bin/bash
# Check if SplitSeq exists in the Go strings package
go doc strings.SplitSeq
echo "Exit code: $?"

# Check Go version
go version

# Look for any imports that might provide SplitSeq
grep -r "SplitSeq" --include="*.go" .

Length of output: 540


No action needed: strings.SplitSeq is available in Go 1.24

  • Verified with go version go1.24.1 linux/amd64.
  • go doc strings.SplitSeq shows it returning iter.Seq[string] in the standard library.

Comment on lines +30 to 34
items := strings.SplitSeq(string(data), "\n")
for i := range items {
if i != "" {
parts = append(parts, i)
}
Copy link

@coderabbitai coderabbitai bot May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical issue: Incorrect loop variable usage

The refactoring from strings.Split to strings.SplitSeq is a good optimization, but there are two critical issues in the implementation:

  1. When using for i := range items, the variable i represents the index, not the value at that index.
  2. The condition if i != "" is incorrectly comparing an integer index with an empty string, which will cause a compilation error.
  3. Line 33 attempts to append an integer index to a string slice, which will also fail.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
items := strings.SplitSeq(string(data), "\n")
for i := range items {
if i != "" {
parts = append(parts, i)
}
items := strings.SplitSeq(string(data), "\n")
for _, item := range items {
if item != "" {
parts = append(parts, item)
}
}

Copy link
Member

Choose a reason for hiding this comment

The 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."

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

Let me check the Go documentation and explore how SplitSeq should be properly used, as it seems to be different from Split.

@dogancanbakir Thank you for sharing the function description. Based on this, I see the issue with the current implementation. Since SplitSeq returns an iterator rather than a slice, we need to use it differently.

Here's the correct way to use SplitSeq:

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 SplitSeq, you need to use the Next() and Value() methods to properly iterate through the results rather than using a range loop.


🏁 Script executed:

#!/bin/bash
# Check Go documentation for SplitSeq
go doc strings.SplitSeq || echo "Documentation not found"

# Look for examples of SplitSeq usage in the codebase
rg "SplitSeq" -A 3 -B 3

# Check the Go stdlib strings package
curl -s https://pkg.go.dev/strings | grep -A 10 "SplitSeq"

Length of output: 3169


@dogancanbakir Thanks for providing that function description. Looking at the Go documentation, SplitSeq returns an iter.Seq[string] object, which is an iterator, not a slice like Split returns.

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 SplitSeq:

var parts []string
iter := strings.SplitSeq(string(data), "\n")
for iter.Next() {
    item := iter.Value()
    if item != "" {
        parts = append(parts, item)
    }
}

Since SplitSeq returns a single-use iterator, you need to use the Next() method to advance through the items and Value() to retrieve each string, rather than using a range loop like you would with a slice.


✏️ Learnings added
Learnt from: dogancanbakir
PR: projectdiscovery/subfinder#1586
File: v2/pkg/testutils/integration.go:30-34
Timestamp: 2025-05-17T13:17:03.433Z
Learning: In Go, `strings.SplitSeq` returns an iterator that must be used with the `Next()` and `Value()` methods, unlike `strings.Split` which returns a slice. When replacing `Split` with `SplitSeq` for performance optimization, the iteration pattern must be changed accordingly.

Learnt from: dogancanbakir
PR: projectdiscovery/subfinder#1586
File: v2/pkg/testutils/integration.go:30-34
Timestamp: 2025-05-17T13:17:03.433Z
Learning: In Go, when replacing `strings.Split` with `strings.SplitSeq` (added in Go 1.24.0), the implementation must change from using range loops on slices to using the iterator pattern with `Next()` and `Value()` methods, as `SplitSeq` returns an `iter.Seq[string]` iterator rather than a string slice.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@GeorginaReeder
Copy link

Thanks for your contribution @findnature ! :)

As a token of appreciation for your valuable contribution, you can grab some cool PD Stickers from here http://nux.gg/stickers.

We also have a Discord server, which you’re more than welcome to join. It's a great place to connect with fellow contributors and stay updated with the latest developments!

@findnature
Copy link
Author

Thanks for your contribution @findnature ! :)

As a token of appreciation for your valuable contribution, you can grab some cool PD Stickers from here http://nux.gg/stickers.

We also have a Discord server, which you’re more than welcome to join. It's a great place to connect with fellow contributors and stay updated with the latest developments!

Thanks!

@findnature
Copy link
Author

Anything need I to do?

@ehsandeep ehsandeep requested a review from dogancanbakir May 17, 2025 10:52
Copy link
Member

@dogancanbakir dogancanbakir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Left a comment for you.

@@ -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")
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants