Skip to content

Preserve whitespace when using --anonymize-passwords/--sensitive-words - #225

Merged
dhalperi merged 1 commit into
masterfrom
spr/master/08cb5061
Feb 5, 2026
Merged

Preserve whitespace when using --anonymize-passwords/--sensitive-words#225
dhalperi merged 1 commit into
masterfrom
spr/master/08cb5061

Conversation

@dhalperi

@dhalperi dhalperi commented Feb 5, 2026

Copy link
Copy Markdown
Member

Fix #213: The old code used str.split() which collapsed all internal
whitespace to single spaces. This change uses re.split(r"(\s+)", line)
to preserve whitespace as separate list elements, then restores original
whitespace after processing.

Key changes:

  • New _split_line_preserve_whitespace() replaces old _split_line()
  • SensitiveWordAnonymizer.anonymize() modifies words in-place
  • replace_matching_item() returns original line if no regex matches,
    otherwise restores whitespace when possible
  • Falls back to collapsed whitespace only when enclosing text (quotes)
    was extracted, which changes word boundaries

Prompt:

Read https://github.com/intentionet/netconan/issues/213 and
https://github.com/intentionet/netconan/pull/214 and the code review
therein. Can you implement a more idiomatic solution to this bug that
meets the requirements of the issue and code review?

This change is Reviewable

Fix #213: The old code used str.split() which collapsed all internal
whitespace to single spaces. This change uses re.split(r"(\s+)", line)
to preserve whitespace as separate list elements, then restores original
whitespace after processing.

Key changes:
- New _split_line_preserve_whitespace() replaces old _split_line()
- SensitiveWordAnonymizer.anonymize() modifies words in-place
- replace_matching_item() returns original line if no regex matches,
  otherwise restores whitespace when possible
- Falls back to collapsed whitespace only when enclosing text (quotes)
  was extracted, which changes word boundaries

----

Prompt:
```
Read #213 and
#214 and the code review
therein. Can you implement a more idiomatic solution to this bug that
meets the requirements of the issue and code review?
```

commit-id:08cb5061

@dhalperi dhalperi left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dhalperi reviewed 2 files and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved.

@dhalperi
dhalperi merged commit 22cf6ff into master Feb 5, 2026
10 checks passed
@dhalperi
dhalperi deleted the spr/master/08cb5061 branch February 5, 2026 18:27
dhalperi added a commit that referenced this pull request Feb 5, 2026
Regression from #225: the _split_line_preserve_whitespace function
incorrectly detected leading/trailing whitespace. When re.split is
called with a capturing group on a string with leading/trailing
whitespace, it produces empty strings at the boundaries:

    re.split(r"(\s+)", "  foo\n") -> ['', '  ', 'foo', '\n', '']

The code was checking parts[0] and parts[-1] for whitespace, but those
are empty strings. The actual whitespace is at parts[1] and parts[-2].

Fix: drop empty strings at boundaries before processing, simplifying
the leading/trailing whitespace detection.

----

Prompt:
```
Hey I ran netconan on ~/networks/2026-02-04-.../ and got this diff. Did we mess
up the newlines somewhere? We're now commenting out the closing brace.
```

commit-id:c8455062
dhalperi added a commit that referenced this pull request Feb 5, 2026
Regression from #225: the _split_line_preserve_whitespace function
incorrectly detected leading/trailing whitespace. When re.split is
called with a capturing group on a string with leading/trailing
whitespace, it produces empty strings at the boundaries:

    re.split(r"(\s+)", "  foo\n") -> ['', '  ', 'foo', '\n', '']

The code was checking parts[0] and parts[-1] for whitespace, but those
are empty strings. The actual whitespace is at parts[1] and parts[-2].

Fix: drop empty strings at boundaries before processing, simplifying
the leading/trailing whitespace detection.

----

Prompt:
```
Hey I ran netconan on ~/networks/2026-02-04-.../ and got this diff. Did we mess
up the newlines somewhere? We're now commenting out the closing brace.
```

commit-id:c8455062
dhalperi added a commit that referenced this pull request Feb 5, 2026
Regression from #225: the _split_line_preserve_whitespace function
incorrectly detected leading/trailing whitespace. When re.split is
called with a capturing group on a string with leading/trailing
whitespace, it produces empty strings at the boundaries:

    re.split(r"(\s+)", "  foo\n") -> ['', '  ', 'foo', '\n', '']

The code was checking parts[0] and parts[-1] for whitespace, but those
are empty strings. The actual whitespace is at parts[1] and parts[-2].

Fix: drop empty strings at boundaries before processing, simplifying
the leading/trailing whitespace detection.

----

Prompt:
```
Hey I ran netconan on ~/networks/2026-02-04-.../ and got this diff. Did we mess
up the newlines somewhere? We're now commenting out the closing brace.
```

commit-id:c8455062
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.

Whitespace is unnecessarily and erroneously changed when using --anonymize-passwords and/or --sensitive-words

1 participant