Last active
October 28, 2021 14:40
-
-
Save nirvdrum/06bf5fc194290f9771ff190db81c56a0 to your computer and use it in GitHub Desktop.
Ruby Encoding Compatibility Check
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
@Specialization(guards = { | |
"firstEncoding != secondEncoding", | |
"!isBroken(first, codeRangeNode)", | |
"!isBroken(second, codeRangeNode)" | |
}) | |
protected RubyEncoding commonCases( | |
Rope first, RubyEncoding firstEncoding, Rope second, RubyEncoding secondEncoding) { | |
final CodeRange firstCodeRange = getCodeRange(first); | |
final CodeRange secondCodeRange = getCodeRange(second); | |
assert firstCodeRange == CodeRange.CR_7BIT || firstCodeRange == CodeRange.CR_VALID; | |
assert secondCodeRange == CodeRange.CR_7BIT || secondCodeRange == CodeRange.CR_VALID; | |
if (firstEncoding == Encodings.UTF_8) { | |
if (secondEncoding == Encodings.US_ASCII) { | |
return Encodings.UTF_8; | |
} | |
if (secondEncoding == Encodings.BINARY) { | |
if (secondCodeRange == CodeRange.CR_7BIT) { | |
return Encodings.UTF_8; | |
} | |
if (secondCodeRange == CodeRange.CR_VALID) { | |
if (firstCodeRange == CodeRange.CR_VALID && !second.isEmpty()) { | |
return null; | |
} | |
return Encodings.BINARY; | |
} | |
} | |
} else if (firstEncoding == Encodings.US_ASCII) { | |
if (secondEncoding == Encodings.UTF_8) { | |
if (secondCodeRange == CodeRange.CR_VALID) { | |
return secondEncoding; | |
} else { | |
return Encodings.US_ASCII; | |
} | |
} | |
if (secondEncoding == Encodings.BINARY) { | |
if (secondCodeRange == CodeRange.CR_VALID) { | |
return Encodings.BINARY; | |
} else { | |
return Encodings.US_ASCII; | |
} | |
} | |
} else if (firstEncoding == Encodings.BINARY) { | |
if (secondEncoding == Encodings.UTF_8) { | |
if (secondCodeRange == CodeRange.CR_7BIT || second.isEmpty()) { | |
return Encodings.BINARY; | |
} | |
if (firstCodeRange == CodeRange.CR_VALID && secondCodeRange == CodeRange.CR_VALID && | |
!first.isEmpty()) { | |
return null; | |
} else { | |
return Encodings.UTF_8; | |
} | |
} | |
if (secondEncoding == Encodings.US_ASCII) { | |
return Encodings.BINARY; | |
} | |
} | |
return compatibleEncodingForRopes(first, firstEncoding, second, secondEncoding); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment