Visitar URL original
XmlLayout - Optimize XML escape to reduce string enumeration by snakefoot · Pull Request #6095 · NLog/NLog · GitHub
Skip to content

XmlLayout - Optimize XML escape to reduce string enumeration - #6095

Merged
snakefoot merged 1 commit into
NLog:devfrom
snakefoot:xmlhelper_escape
Feb 19, 2026
Merged

snakefoot merged 1 commit into
NLog:devfrom
snakefoot:xmlhelper_escape

Conversation

@snakefoot

@snakefoot snakefoot commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Followup to #6094

@coderabbitai

coderabbitai Bot commented Feb 19, 2026 •

Copy link
Copy Markdown

Walkthrough

Modified the XML escaping logic in NLog's XmlHelper to remove pre-escape character sanitization, adjusted the EscapeXmlString method signature by removing the startIndex parameter, and restructured control flow to handle surrogate pairs and defer invalid character removal to fallback scenarios.

Changes

Cohort / File(s) Summary
XML Escaping Logic Refactoring
src/NLog/Internal/XmlHelper.cs
Removed upfront character cleansing before escaping, modified EscapeXmlString signature to eliminate startIndex parameter, added surrogate pair preservation during character processing, and deferred RemoveInvalidXmlChars to fallback path instead of preprocessing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Escaping XML with care so fine,
Surrogate pairs in perfect line,
No pre-wash needed, just escape with grace,
The characters dance in their rightful place! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided, making it impossible to assess whether a description relates to the changeset. Add a pull request description explaining the optimization rationale, performance benefits, and any behavioral changes from removing pre-escape cleansing.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main optimization: performing a single enumeration pass for XML escaping instead of multiple passes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/NLog/Internal/XmlHelper.cs (1)

154-167: startIndex variable name is now misleading — consider renaming

After removing the startIndex parameter from EscapeXmlString, the local variable startIndex computed at line 156 is only tested as >= 0; its numeric value is never consumed. The name implies a positional offset that is no longer used.

♻️ Suggested rename for clarity
-            var startIndex = text.IndexOfAny(xmlEncodeNewlines ? XmlEscapeNewlineChars : XmlEscapeChars);
-            if (startIndex >= 0)
+            var needsEscape = text.IndexOfAny(xmlEncodeNewlines ? XmlEscapeNewlineChars : XmlEscapeChars) >= 0;
+            if (needsEscape)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/NLog/Internal/XmlHelper.cs` around lines 154 - 167, The local variable
startIndex in EscapeXmlWhenNeeded is misleading because its numeric value is
unused after checking >= 0; rename it to a boolean-like name (e.g., needsEscape
or hasEscapeChar) and update the conditional to check that boolean to make
intent clear, and keep calls to EscapeXmlString(text, xmlEncodeNewlines, sb) and
RemoveInvalidXmlChars(text, builder) unchanged; reference: EscapeXmlWhenNeeded,
EscapeXmlString, RemoveInvalidXmlChars.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/NLog/Internal/XmlHelper.cs`:
- Around line 154-167: The local variable startIndex in EscapeXmlWhenNeeded is
misleading because its numeric value is unused after checking >= 0; rename it to
a boolean-like name (e.g., needsEscape or hasEscapeChar) and update the
conditional to check that boolean to make intent clear, and keep calls to
EscapeXmlString(text, xmlEncodeNewlines, sb) and RemoveInvalidXmlChars(text,
builder) unchanged; reference: EscapeXmlWhenNeeded, EscapeXmlString,
RemoveInvalidXmlChars.

@sonarqubecloud

Copy link
Copy Markdown

@snakefoot
snakefoot merged commit 9770daf into NLog:dev Feb 19, 2026
6 of 7 checks passed
@snakefoot snakefoot changed the title XmlLayout - Optimize XML escape to perform single enumeration XmlLayout - Optimize XML escape to reduce string enumeration Feb 19, 2026
@snakefoot snakefoot added this to the 6.1.1 milestone Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant