Skip to content

Commit 4182c98

Browse files
committed
Ensure last encoding flag wins
You can't encoding a string with more than one encoding so ensure that the last one wins.
1 parent fbd101d commit 4182c98

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/prism.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,21 @@ parse_decimal_number(pm_parser_t *parser, const uint8_t *start, const uint8_t *e
779779
static inline pm_node_flags_t
780780
pm_regular_expression_flags_create(const pm_token_t *closing) {
781781
pm_node_flags_t flags = 0;
782+
pm_node_flags_t mask = (uint16_t) 0xFF0F;
782783

783784
if (closing->type == PM_TOKEN_REGEXP_END) {
784785
for (const uint8_t *flag = closing->start + 1; flag < closing->end; flag++) {
785786
switch (*flag) {
786787
case 'i': flags |= PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE; break;
787788
case 'm': flags |= PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE; break;
788789
case 'x': flags |= PM_REGULAR_EXPRESSION_FLAGS_EXTENDED; break;
789-
case 'e': flags |= PM_REGULAR_EXPRESSION_FLAGS_EUC_JP; break;
790-
case 'n': flags |= PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT; break;
791-
case 's': flags |= PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J; break;
792-
case 'u': flags |= PM_REGULAR_EXPRESSION_FLAGS_UTF_8; break;
793790
case 'o': flags |= PM_REGULAR_EXPRESSION_FLAGS_ONCE; break;
791+
792+
case 'e': flags &= mask; flags |= PM_REGULAR_EXPRESSION_FLAGS_EUC_JP; break;
793+
case 'n': flags &= mask; flags |= PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT; break;
794+
case 's': flags &= mask; flags |= PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J; break;
795+
case 'u': flags &= mask; flags |= PM_REGULAR_EXPRESSION_FLAGS_UTF_8; break;
796+
794797
default: assert(false && "unreachable");
795798
}
796799
}

test/prism/regexp_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ def test_flag_combined
227227
assert_equal(value, options("mix"))
228228
end
229229

230+
def test_last_encoding_option_wins
231+
regex = "/foo/nu"
232+
option = Prism.parse(regex).value.statements.body.first.options
233+
234+
assert_equal Regexp::FIXEDENCODING, option
235+
236+
regex = "/foo/un"
237+
option = Prism.parse(regex).value.statements.body.first.options
238+
239+
assert_equal Regexp::NOENCODING, option
240+
end
241+
230242
private
231243

232244
def named_captures(source)

0 commit comments

Comments
 (0)