Fix condition for ISO-8859-1 encoding selection in GetTargetEncoding method#681
Fix condition for ISO-8859-1 encoding selection in GetTargetEncoding method#681
Conversation
📝 WalkthroughWalkthroughAdjusts the encoding selection logic in ByteDataSegment: the fast path to ISO-8859-1 is now taken only when ECI mode is Default, not forced UTF-8, and the text is ISO-valid. Non-default ECI modes bypass this path and proceed through the switch-based handling. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller
participant ByteDataSegment
Caller->>ByteDataSegment: Create byte data segment (plainText, eciMode, forceUtf8)
alt eciMode == Default AND !forceUtf8 AND IsValidISO(plainText)
Note right of ByteDataSegment: Use ISO-8859-1 path (unchanged)
ByteDataSegment-->>Caller: Encoded bytes (ISO-8859-1)
else Non-default ECI or forced UTF-8 or invalid ISO
Note right of ByteDataSegment: Proceed to ECI-aware switch-case
ByteDataSegment->>ByteDataSegment: switch (eciMode) { Utf8, Utf8BOM, ... }
ByteDataSegment-->>Caller: Encoded bytes per selected ECI handling
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
QRCoder/QRCodeGenerator/ByteDataSegment.cs (3)
124-125: Update comment to reflect ECI mode check.The comment should mention that the ECI mode must also be
Defaultfor the ISO-8859-1 fast path.Apply this diff:
- // Check if the text is valid ISO-8859-1 and UTF-8 is not forced, then encode using ISO-8859-1. + // Check if ECI mode is Default, the text is valid ISO-8859-1, and UTF-8 is not forced, then encode using ISO-8859-1. if (eciMode == EciMode.Default && !forceUtf8 && IsValidISO(plainText))
186-190: Update remarks to reflect ECI mode handling.The remarks are outdated. They should clarify that encoding selection also depends on the ECI mode. When an explicit non-default ECI mode is specified (e.g.,
EciMode.Utf8), that encoding is used regardless of ISO-8859-1 validity.Apply this diff:
/// <remarks> - /// The returned text is always encoded as ISO-8859-1 unless either the text contains a non-ISO-8859-1 character or - /// UTF-8 encoding is forced. This does not meet the QR Code standard, which requires the use of ECI to specify the encoding - /// when not ISO-8859-1. + /// The returned text is encoded as ISO-8859-1 when the ECI mode is Default, UTF-8 is not forced, and the text + /// contains only valid ISO-8859-1 characters. Otherwise, the encoding is determined by the ECI mode or defaults to UTF-8. + /// This does not meet the QR Code standard, which requires the use of ECI to specify the encoding when not ISO-8859-1. /// </remarks>
209-213: Update remarks to reflect ECI mode handling.Same issue as lines 186-190. The remarks should clarify that encoding selection depends on the ECI mode.
Apply this diff:
/// <remarks> - /// The returned text is always encoded as ISO-8859-1 unless either the text contains a non-ISO-8859-1 character or - /// UTF-8 encoding is forced. This does not meet the QR Code standard, which requires the use of ECI to specify the encoding - /// when not ISO-8859-1. + /// The returned text is encoded as ISO-8859-1 when the ECI mode is Default, UTF-8 is not forced, and the text + /// contains only valid ISO-8859-1 characters. Otherwise, the encoding is determined by the ECI mode or defaults to UTF-8. + /// This does not meet the QR Code standard, which requires the use of ECI to specify the encoding when not ISO-8859-1. /// </remarks>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
QRCoder/QRCodeGenerator/ByteDataSegment.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
QRCoder/QRCodeGenerator/ByteDataSegment.cs (1)
QRCoder/QRCodeGenerator.cs (1)
IsValidISO(973-985)
🔇 Additional comments (1)
QRCoder/QRCodeGenerator/ByteDataSegment.cs (1)
125-125: LGTM! Correctly restricts ISO-8859-1 fast path to Default ECI mode.This fix ensures that explicit ECI mode requests (e.g.,
EciMode.Utf8) are honored even when the text is valid ISO-8859-1. Previously, the method would incorrectly select ISO-8859-1 encoding when a user explicitly requested UTF-8 viaEciMode.Utf8but didn't setforceUtf8.The condition ordering is also optimal: cheap enum and boolean checks before the more expensive
IsValidISOstring iteration.
Summary by CodeRabbit