Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Src/IronRuby.Tests/RubyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public Tests(Driver/*!*/ driver) {
Regex_SharedTranslation1,
Regex_SharedTranslation2,
Regex_PropertyEncoding1,
Regex_BinaryCtype1,
Regex_InspectResultEncoding1,
Regex_EscapedByteEncoding1,
Regex_EscapedByteEncoding2,
Expand Down
57 changes: 57 additions & 0 deletions Src/IronRuby.Tests/Runtime/RegexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,63 @@ def t(l)
union string: [#<Encoding:ISO-8859-1>, true]
retry: ""a""
translated: [RegexpError, ""unmatched close parenthesis: /\\xC3\\xA9)/""]
");
}

/// <summary>
/// A BINARY subject is matched with Onigmo's ASCII-8BIT ctype table (MRI's
/// rb_reg_prepare_enc compiles a regexp that is not fixed-encoding for the subject's
/// encoding): no letters, punctuation, word characters or case pairs above 0x7F, where .NET
/// sees U+0080-U+00FF. ISO-8859-1 and UTF-8 subjects keep their own letters, and a BINARY
/// regexp's \p{Punct} is [[:punct:]] even on ASCII text.
/// </summary>
public void Regex_BinaryCtype1() {
TestOutput(@"
require 'ruby4'
def t(l)
v = begin; yield; rescue => e; [e.class, e.message]; end
puts ""#{l}: #{v.inspect}""
end
s = ""a\xE9b\xFF c\xC9"".b
t('property /n') { ""\xff"".b =~ /\p{Alpha}/n }
t('posix') { ""\xff"".b =~ Regexp.new('[[:alpha:]]'.b) }
t('word') { ""\xff"".b =~ Regexp.new('\p{Word}'.b) }
t('lower') { ""\xff"".b =~ Regexp.new('\p{Lower}'.b) }
t('negated') { [""\xff"".b =~ /[[:^alpha:]]/, ""\xff"".b =~ /\P{Alpha}/n] }
t('\w') { [""\xff"".b =~ /\w/n, ""\xff"".b =~ /(?u)\w/] }
t('punct') { [""$"".b =~ Regexp.new('\p{Punct}'.b), ""$\xff"".b =~ /[[:punct:]]+/, ""\xab"".b =~ /[[:punct:]]/] }
t('\b') { [s.scan(/\b./), s.split(/\b/)] }
t('\R') { [""\x85"".b =~ /\R/, ""\r\n\x85"".b[/\R+/]] }
t('scan') { s.scan(/[[:alpha:]]+/) }
t('gsub') { s.gsub(/\p{^Alpha}/n, '.') }
t('/i literal') { [""\xC9"".b =~ /\xE9/in, ""\xC9"".b =~ Regexp.new(""\xE9"".b, Regexp::IGNORECASE), ""\xE9"".b =~ /\xE9/in] }
t('/i class') { [""\xC9"".b =~ /[\xE0-\xEF]/in, ""\xC9"".b =~ /[^\xE9]/in, ""A\xE9"".b =~ /[^a]/in, ""A\xE9"".b =~ /a/in] }
t('/i posix') { s.scan(/[[:upper:]]+/i) }
t('(?i)') { ""\xC9a"".b =~ /(?i:\xE9)|(?i)A/n }
t('one regexp') { re = /[[:alpha:]]/; [re.match?(""\u00e9""), re.match?(""\xE9"".b), re.match?(""\u00e9""), re.match?(""\xE9"".dup.force_encoding(""ISO-8859-1""))] }
t('iso-8859-1') { [""\xE9"".dup.force_encoding(""ISO-8859-1"") =~ /[[:alpha:]]/, ""\xC9"".dup.force_encoding(""ISO-8859-1"") =~ Regexp.new(""\xE9"".dup.force_encoding(""ISO-8859-1""), Regexp::IGNORECASE)] }
t('utf-8') { [""\u00e9"" =~ /\p{Alpha}/, ""\u00e9"" =~ /\b/] }
t('absent /i') { a = ""\xE9a"".b; [a.match(/(?~\xE9a)/in, 0)[0], a.match(/(?~\xE9a)/in, 1)[0], ""\xC9a"".b.match(/(?~\xE9a)/in)[0], a.match(/(?~\xC9a)/in)[0]] }
", @"
property /n: nil
posix: nil
word: nil
lower: nil
negated: [0, 0]
\w: [nil, nil]
punct: [0, 0, nil]
\b: [[""a"", ""\xE9"", ""b"", ""\xFF"", ""c"", ""\xC9""], [""a"", ""\xE9"", ""b"", ""\xFF "", ""c"", ""\xC9""]]
\R: [nil, ""\r\n""]
scan: [""a"", ""b"", ""c""]
gsub: ""a.b..c.""
/i literal: [nil, nil, 0]
/i class: [nil, 0, 1, 0]
/i posix: [""a"", ""b"", ""c""]
(?i): 1
one regexp: [true, false, true, true]
iso-8859-1: [0, 0]
utf-8: [0, 0]
absent /i: [""\xE9"", ""a"", ""\xC9a"", ""\xE9a""]
");
}
}
Expand Down
166 changes: 158 additions & 8 deletions Src/Ruby/Builtins/RegexpTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ private sealed class SharedState {
public int Emitted;
// see Transform(..., astralSafe)
public bool AstralSafe;
// see Transform(..., asciiCtype)
public bool AsciiCtype;
// Whether /i or an inline (?i) may be in effect anywhere in the pattern (AsciiCtype only).
public bool MayIgnoreCase;
// Whether AsciiCtype made the translation differ from the plain one.
public bool AsciiCtypeApplied;
}

private sealed class ExpansionTooLargeException : Exception {
Expand Down Expand Up @@ -223,17 +229,37 @@ internal static string Transform(string/*!*/ rubyPattern, RubyRegexOptions optio
/// with no pairs needs and is what keeps the common case as fast as it was.
/// </summary>
internal static string Transform(string/*!*/ rubyPattern, RubyRegexOptions options, out bool hasGAnchor, bool astralSafe) {
bool asciiCtypeApplied;
return Transform(rubyPattern, options, out hasGAnchor, astralSafe, false, out asciiCtypeApplied);
}

/// <summary>
/// With <paramref name="asciiCtype"/> the translation is for a match MRI makes with
/// Onigmo's ASCII-8BIT or US-ASCII ctype table (see RubyRegex.UsesAsciiCtype) - a BINARY
/// subject that holds bytes above 0x7F, above all. Those tables have nothing above 0x7F:
/// no letters, digits, spaces or punctuation, and no case pairs. A BINARY subject's bytes
/// reach .NET as U+0080-U+00FF, most of which .NET calls letters, so here the properties
/// (as the POSIX classes they are in those tables), the POSIX brackets, (?u)\w \d \s,
/// \b and \R are restricted to ASCII, and under /i a Latin-1 letter matches only itself.
/// <paramref name="asciiCtypeApplied"/> says whether any of that made a difference, i.e.
/// whether the pattern has a construct the restriction applies to.
/// </summary>
internal static string Transform(string/*!*/ rubyPattern, RubyRegexOptions options, out bool hasGAnchor, bool astralSafe,
bool asciiCtype, out bool asciiCtypeApplied) {
// TODO: surrogates (REXML uses this pattern)
if (rubyPattern == "^[\t\n\r -\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF]*$") {
hasGAnchor = false;
asciiCtypeApplied = false;
return "^(?:[\t\n\r -\uD7FF\uE000-\uFFFD]|[\uD800-\uDBFF][\uDC00-\uDFFF])*$";
}

RegexpTransformer transformer = new RegexpTransformer(rubyPattern);
transformer._clrOptions = RubyRegex.ToClrOptions(options);
transformer._state.AstralSafe = astralSafe;
transformer._state.AsciiCtype = asciiCtype;
var result = transformer.Transform();
hasGAnchor = transformer._hasGAnchor;
asciiCtypeApplied = transformer._state.AsciiCtypeApplied;
return result;
}

Expand Down Expand Up @@ -539,9 +565,41 @@ private void Append(char c) {
}

private void AppendEscaped(int c) {
if (IsUnfoldedLatin1Letter(c)) {
AppendUnfoldedLatin1Letter(c);
return;
}
AppendEscaped(_sb, c);
}

private const string CaseSensitiveLatin1Prefix = "(?-i:";

/// <summary>
/// (?-i:c) for a letter IsUnfoldedLatin1Letter accepts; GetLiteralLength counts it as the
/// one character it is, so that (?~...) keeps its fixed-length prefix.
/// </summary>
private void AppendUnfoldedLatin1Letter(int c) {
_sb.Append(CaseSensitiveLatin1Prefix).Append((char)c).Append(')');
}

private static bool IsUnfoldableLatin1Letter(int c) {
return c >= 0xc0 && c <= 0xfe && c != 0xd7 && c != 0xf7;
}

/// <summary>
/// A Latin-1 letter that has its other case in U+00C0-U+00FE, while the translation is for
/// a BINARY subject (SharedState.AsciiCtype) under a pattern that may ignore case: Onigmo's
/// ASCII-8BIT folds only A-Z, so it has to be matched case-sensitively, where .NET's
/// IgnoreCase would take "\xC9" for "\xE9".
/// </summary>
private bool IsUnfoldedLatin1Letter(int c) {
if (_state.MayIgnoreCase && IsUnfoldableLatin1Letter(c)) {
_state.AsciiCtypeApplied = true;
return true;
}
return false;
}

private static StringBuilder/*!*/ AppendEscaped(StringBuilder/*!*/ builder, string/*!*/ str) {
for (int i = 0; i < str.Length; i++) {
AppendEscaped(builder, str[i]);
Expand Down Expand Up @@ -604,6 +662,25 @@ private static bool IsMetaCharacter(int c) {
((options & RegexOptions.IgnorePatternWhitespace) != 0 ? "x" : ""));
}

/// <summary>
/// Whether some (?i) or (?i:...) - the i possibly among other options, before any '-' -
/// may turn /i on. Errs on the side of yes (an escaped \(?i) counts), which costs no
/// more than a (?-i:...) around a Latin-1 letter that did not need one.
/// </summary>
private static bool HasInlineIgnoreCase(string/*!*/ pattern) {
int i = 0;
while ((i = pattern.IndexOf("(?", i, StringComparison.Ordinal)) >= 0) {
i += 2;
while (i < pattern.Length && "imxadu".IndexOf(pattern[i]) >= 0) {
if (pattern[i] == 'i') {
return true;
}
i++;
}
}
return false;
}

private string/*!*/ Transform() {
if (_copyGroups) {
return TransformBody();
Expand All @@ -616,8 +693,12 @@ private static bool IsMetaCharacter(int c) {

for (int depth = DefaultMaxCallDepth; ; depth /= 2) {
bool astralSafe = _state.AstralSafe;
bool asciiCtype = _state.AsciiCtype;
_state = new SharedState();
_state.AstralSafe = astralSafe;
_state.AsciiCtype = asciiCtype;
_state.MayIgnoreCase = asciiCtype &&
((_clrOptions & RegexOptions.IgnoreCase) != 0 || HasInlineIgnoreCase(_rubyPattern));
_state.MaxCallDepth = depth;
_state.ClrOptions = _clrOptions;
_state.RootPattern = _rubyPattern;
Expand Down Expand Up @@ -865,6 +946,10 @@ private void Parse(bool isSubexpression) {
}
break;
}
if (IsUnfoldedLatin1Letter(c)) {
AppendUnfoldedLatin1Letter(c);
break;
}
Append((char)c);
break;
}
Expand Down Expand Up @@ -1474,7 +1559,11 @@ private static int GetLiteralLength(string/*!*/ pattern) {
int length = 0;
for (int i = 0; i < pattern.Length; i++) {
char c = pattern[i];
if (c == '\\') {
if (c == '(' && i + 6 < pattern.Length && String.CompareOrdinal(pattern, i, CaseSensitiveLatin1Prefix, 0, 5) == 0 &&
pattern[i + 6] == ')' && IsUnfoldableLatin1Letter(pattern[i + 5])) {
// one Latin-1 letter matched case-sensitively for a BINARY subject (AppendUnfoldedLatin1Letter)
i += 6;
} else if (c == '\\') {
// a meta character or a whitespace character, as Escape writes them
if (i + 1 == pattern.Length || !(IsMetaCharacter(pattern[i + 1]) || Char.IsWhiteSpace(pattern[i + 1]))) {
return -1;
Expand Down Expand Up @@ -1538,7 +1627,13 @@ private void ParseEscape(int escape) {
switch (escape) {
case 'b': // word boundary
case 'B': // not a word boundary
if (_state.AstralSafe) {
if (_state.AsciiCtype) {
// a BINARY subject's word characters are ASCII ones
_state.AsciiCtypeApplied = true;
_sb.Append(escape == 'b'
? "(?:(?<=[0-9A-Za-z_])(?![0-9A-Za-z_])|(?<![0-9A-Za-z_])(?=[0-9A-Za-z_]))"
: "(?:(?<=[0-9A-Za-z_])(?=[0-9A-Za-z_])|(?<![0-9A-Za-z_])(?![0-9A-Za-z_]))");
} else if (_state.AstralSafe) {
AppendAstralWordBoundary(escape == 'b');
} else {
Append('\\');
Expand Down Expand Up @@ -1584,7 +1679,9 @@ private void ParseEscape(int escape) {
case 'R':
// A generic line break: CRLF as a unit, or any single line terminator.
// Atomic so that CRLF never backtracks into matching just the CR.
_sb.Append("(?>\\r\\n|[\\n\\v\\f\\r\\u0085\\u2028\\u2029])");
// In a BINARY subject (SharedState.AsciiCtype) 0x85 is no line terminator.
_state.AsciiCtypeApplied |= _state.AsciiCtype;
_sb.Append(_state.AsciiCtype ? "(?>\\r\\n|[\\n\\v\\f\\r])" : "(?>\\r\\n|[\\n\\v\\f\\r\\u0085\\u2028\\u2029])");
break;

case 'K':
Expand Down Expand Up @@ -1630,7 +1727,7 @@ private void ParseEscape(int escape) {
}

var set = ParseCharacterEscape(escape);
if ((escape == 'w' || escape == 'W') && _characterClassMode == CharacterClassMode.Unicode) {
if ((escape == 'w' || escape == 'W') && _characterClassMode == CharacterClassMode.Unicode && !_state.AsciiCtype) {
// Outside a class Onigmo tests a (?u)\w below U+0100 against its ISO-8859-1
// ctype table, which also counts the superscripts and vulgar fractions
// (U+00B2 U+00B3 U+00B9 U+00BC-U+00BE) as word characters.
Expand Down Expand Up @@ -2170,6 +2267,31 @@ private void AppendAnyCharacter() {
/// a * or + follows is emitted as one class (CharacterSet.AppendLoopBodyTo), for speed.
/// </summary>
private void AppendCharacterSet(CharacterSet/*!*/ set, bool parenthesize) {
if (_state.MayIgnoreCase) {
if (set.IsSingleCharacter && IsUnfoldedLatin1Letter(set.SingleCodepoint)) {
// \xE9: the letter itself, spelled as a literal one is
AppendUnfoldedLatin1Letter(set.SingleCodepoint);
return;
}
int[] latin1 = set.GetLatin1Members();
if (!CodePointRanges.IsLatin1CaseClosed(latin1)) {
_state.AsciiCtypeApplied = true;
// .NET's IgnoreCase would fold the Latin-1 letters, Onigmo's ASCII-8BIT does
// not: the ASCII members as the set itself (so that [^a] still turns "A"
// away under /i), the others case-sensitively, as listed.
_sb.Append("(?:(?![\\x80-\\xff])");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
set.AppendTo(_sb, true);
int[] high = CodePointRanges.Intersect(latin1, CodePointRanges.Single(0x80, 0xff));
if (high.Length != 0) {
_sb.Append("|(?-i:");
CharacterSet.MakeSet(high, null).AppendTo(_sb, true);
_sb.Append(')');
}
_sb.Append(')');
return;
}
}

int next = Peek();
if ((next == '*' || next == '+') && set.HasAllAstral) {
set.AppendLoopBodyTo(_sb);
Expand All @@ -2191,7 +2313,10 @@ private void AppendCharacterSet(CharacterSet/*!*/ set, bool parenthesize) {
/// </summary>
private CharacterSet/*!*/ MakeAsciiAware(string/*!*/ property, int[]/*!*/ ascii, string/*!*/ asciiText, bool positive) {
if (_characterClassMode == CharacterClassMode.Unicode) {
return CharacterSet.MakeProperty(UnicodeProperties.Find(property), positive);
if (!_state.AsciiCtype) {
return CharacterSet.MakeProperty(UnicodeProperties.Find(property), positive);
}
_state.AsciiCtypeApplied = true;
}
var result = CharacterSet.MakeSet(ascii, asciiText);
return positive ? result : result.Complement();
Expand Down Expand Up @@ -2324,6 +2449,7 @@ private sealed class CharacterSet {
public static readonly CharacterSet Empty = new CharacterSet(CodePointRanges.Empty, false, "", -1, -1);

private static readonly int[] BmpRange = CodePointRanges.Single(0, 0xffff);
private static readonly int[] Latin1Range = CodePointRanges.Single(0, 0xff);
private static readonly int[] AstralRange = CodePointRanges.Single(0x10000, CodePointRanges.MaxCodePoint);
private static readonly int[] SurrogateRange = CodePointRanges.Single(0xd800, 0xdfff);

Expand Down Expand Up @@ -2381,6 +2507,12 @@ private CharacterSet(int[]/*!*/ ranges, bool negated)
return new CharacterSet(UnicodeProperties.GetRanges(table), !positive, null, -1, table);
}

/// <summary>The members in U+0000-U+00FF, the characters a BINARY subject is made of.</summary>
internal int[]/*!*/ GetLatin1Members() {
int[] members = CodePointRanges.Intersect(_ranges, Latin1Range);
return _negated ? CodePointRanges.Subtract(Latin1Range, members) : members;
}

internal bool IsSingleCharacter {
get { return _singleCodepoint >= 0; }
}
Expand Down Expand Up @@ -2973,6 +3105,14 @@ private CharacterSet ParseCharacter(ref IEnumerator<int> codepoints, out bool ma
if (table < 0) {
throw MakeError("invalid character property name {" + name + "}");
}
if (_state.AsciiCtype) {
_state.AsciiCtypeApplied = true;
// A BINARY subject: the ASCII-8BIT ctype table, whose properties are the POSIX
// classes (\p{Punct} takes in $+<=>^`|~ there) over ASCII. A non-POSIX name never
// gets here: it makes the regexp fixed-encoding and not BINARY.
int posix = UnicodeProperties.FindPosix(normalized);
return RestrictToAscii(CharacterSet.MakeProperty(posix >= 0 ? posix : table, true), positive);
}
return CharacterSet.MakeProperty(table, positive);
}

Expand Down Expand Up @@ -3015,17 +3155,27 @@ private CharacterSet ParsePosixCharacterClass() {

private static readonly int[] _asciiRange = new int[] { 0, 0x7f };

/// <summary>[[:name:]]: Onigmo's property of that name, all of Unicode unless (?a) is in effect.</summary>
/// <summary>
/// [[:name:]]: Onigmo's property of that name, all of Unicode unless (?a) is in effect or
/// the subject is BINARY (SharedState.AsciiCtype).
/// </summary>
private CharacterSet/*!*/ MakePosixCharacterClass(int table, bool positive) {
var result = CharacterSet.MakeProperty(table, true);
if (_characterClassMode == CharacterClassMode.Ascii) {
if (_characterClassMode == CharacterClassMode.Ascii || _state.AsciiCtype) {
// (?a) restricts the POSIX classes to ASCII. A negated class still matches
// non-ASCII: (?a)[[:^alpha:]] accepts a Hiragana character.
result = result.Intersect(CharacterSet.MakeSet(_asciiRange, null));
_state.AsciiCtypeApplied |= _state.AsciiCtype;
return RestrictToAscii(result, positive);
}
return positive ? result : result.Complement();
}

/// <summary>The ASCII members of <paramref name="set"/>, or every character but those.</summary>
private static CharacterSet/*!*/ RestrictToAscii(CharacterSet/*!*/ set, bool positive) {
var result = set.Intersect(CharacterSet.MakeSet(_asciiRange, null));
return positive ? result : result.Complement();
}

#endregion
}
}
Loading
Loading