feat(file): add ReadFileSplit / ReadFileWithReaderSplit (#719)#742
Open
ChrisJr404 wants to merge 1 commit intoprojectdiscovery:mainfrom
Open
feat(file): add ReadFileSplit / ReadFileWithReaderSplit (#719)#742ChrisJr404 wants to merge 1 commit intoprojectdiscovery:mainfrom
ChrisJr404 wants to merge 1 commit intoprojectdiscovery:mainfrom
Conversation
…very#719) When a tool reads a list of values from a file (resolvers, wordlists, etc.) it's common for users to mix one-per-line and comma-separated forms on the same line. The two new helpers stream non-empty values from the file/reader, splitting each scanned line on the supplied runes and trimming whitespace; passing no separators reduces to the existing ReadFile/ReadFileWithReader behaviour with TrimSpace applied. Closes projectdiscovery#719
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #719.
What
Two new helpers in `file/file.go` for streaming values from a list-style file where each line may contain one entry or multiple separator-delimited entries:
```go
// Read each line, then split on every rune in
separators, trim whitespace,// drop empties, stream the result.
ReadFileSplit(filename string, separators ...rune) (chan string, error)
ReadFileWithReaderSplit(r io.Reader, separators ...rune) (chan string, error)
```
When no separators are passed, behaviour reduces to the existing `ReadFile` / `ReadFileWithReader` plus `TrimSpace`. So consumers in httpx / nuclei / similar can call the splitting variant unconditionally and get the strictly-line-based reading for free when they don't pass `','`.
Why
Per @dogancanbakir on httpx#2351:
Yes — having it once in `projectdiscovery/utils` lets every PD tool consume the same parsing without copy-pasting the split-and-trim logic each time.
Tests
8 new unit tests in `file/file_split_test.go` covering:
All new tests pass; existing `file/` package tests (including the `FuzzSafeOpen` fuzzer) still pass:
```
$ go test ./file/...
ok github.com/projectdiscovery/utils/file 20.629s
```
Notes