Fix off-by-one in CodeCopyrightTagger end position (missing newline)#282
Open
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Open
Fix off-by-one in CodeCopyrightTagger end position (missing newline)#282Chessing234 wants to merge 1 commit intoallenai:mainfrom
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Conversation
`CodeCopyrightTagger._extract_copyright_spans` splits the text on `"\n"` and walks the resulting lines to find a leading comment block. To reconstruct the end offset of that block in the original text, it accumulates the per-line length, but the split dropped the `"\n"` separators, so each non-empty comment line was counted as `len(line)` instead of `len(line) + 1`. The empty-line branch already added 1 (for the newline), but the non-empty branch forgot the separator, producing spans that end in the middle of the last comment line. Worked example — `text = "// a\n// b\nother"`: - `lines = ["// a", "// b", "other"]` - Current code: `end = 4 + 4 = 8`, so the span is `[0, 8) = "// a\n// "`, truncating mid-line and missing the final `"b\n"`. - Correct: `end = 5 + 5 = 10`, matching the start of `"other"`. Collapse the branches to `end += len(line) + 1`, which is equivalent to the empty-line case's `+= 1` and correct for non-empty lines.
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.
Bug
CodeCopyrightTagger._extract_copyright_spansinpython/dolma/taggers/code/code_taggers.pyproducescomment_blockspans whoseendis short by one per non-empty comment line. The last comment line of a leading copyright block ends up truncated mid-line.Root cause
```python
lines = text.split("\n")
...
end = 0
for line in lines:
if line.startswith("//") or line.startswith("#") or line.startswith("--") or not line:
skip = skip + 1
if not line:
end += 1
else:
end += len(line)
else:
break
```
str.split(\"\\n\")drops the newline separators, so each matched line occupiedlen(line) + 1bytes in the original text (its content plus the trailing\"\\n\"). The empty-line branch correctly adds1(empty content + the newline), but the non-empty branch adds onlylen(line), forgetting the newline.Worked example
`text = "// a\n// b\nother"`:
lines = [\"// a\", \"// b\", \"other\"]end = 4 + 4 = 8. The emitted span is[0, 8) = \"// a\\n// \", cutting off\"b\\n\".end = 5 + 5 = 10, which is exactly where\"other\"begins.This downstream feeds
Span(start=0, end=end, type=\"comment_block\", ...)and_score, so filters and downstream taggers see a slightly-too-small copyright region.Fix
Collapse both branches to
end += len(line) + 1. For empty lines this is0 + 1 = 1, matching the prior behavior. For non-empty comment lines it now correctly includes the dropped newline. Two lines added, four removed; theif not line/elsesplit is no longer needed.```diff
```