-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add mvp for ignoring code comments * Add multiline regex * Update README.md with new command line options
- Loading branch information
Showing
7 changed files
with
80 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ go.work | |
*.zip | ||
text.txt | ||
text.json | ||
git2gpt |
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
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// RemoveCodeComments removes single-line and multiline comments from the provided code string. | ||
func RemoveCodeComments(code string) string { | ||
// Regex for single-line comments. | ||
singleLineCommentRegex := regexp.MustCompile(`^\s*(//|#|--|<!--|%|;|REM\s).*$`) | ||
|
||
// Regex for multiline comments in C, JavaScript, Go, and HTML. | ||
multiLineCommentRegex := regexp.MustCompile(`(?s)/\*.*?\*/|<!--.*?-->`) | ||
|
||
// Use a scanner to process each line of the input string. | ||
var result strings.Builder | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
// First remove multiline comments as they may span across multiple lines. | ||
line = multiLineCommentRegex.ReplaceAllString(line, "") | ||
// Then remove any single-line comment parts that remain. | ||
cleanLine := singleLineCommentRegex.ReplaceAllString(line, "") | ||
if cleanLine != "" { | ||
// Write the cleaned line to the result, preserving original line breaks. | ||
result.WriteString(cleanLine + "\n") | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Fprintln(&result, "Error reading input:", err) | ||
} | ||
|
||
// Additional cleanup in case of multiline comments spanning across multiple scanned lines. | ||
finalCleanedCode := multiLineCommentRegex.ReplaceAllString(result.String(), "") | ||
|
||
return strings.TrimRight(finalCleanedCode, "\n") | ||
} |