Skip to content

Add smartcase to the regex engine. #5208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 doc/pages/regex.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ them:
* `(?I)` starts case-sensitive matching (default).
* `(?s)` allows `.` to match newlines (default).
* `(?S)` prevents `.` from matching newlines.
* `(?c)` enable smartcase, starts case-insensitive matching unless an uppercase character is present in the pattern.

== Quoting

Expand Down
10 changes: 10 additions & 0 deletions src/regex_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ struct RegexParser
None = 0,
IgnoreCase = 1 << 0,
DotMatchesNewLine = 1 << 1,
SmartCase = 1 << 2,
};
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }

Expand Down Expand Up @@ -212,6 +213,7 @@ struct RegexParser
case 'I': m_flags &= ~Flags::IgnoreCase; break;
case 's': m_flags |= Flags::DotMatchesNewLine; break;
case 'S': m_flags &= ~Flags::DotMatchesNewLine; break;
case 'c': m_flags |= Flags::SmartCase | Flags::IgnoreCase; break;
case ')':
m_pos = Iterator{it, m_regex};
return true;
Expand Down Expand Up @@ -328,6 +330,14 @@ struct RegexParser
if (contains(StringView{"^$.*+?[]{}"}, cp) or (cp >= 0xF0000 and cp <= 0xFFFFF))
parse_error(format("unexpected '{}'", cp));
++m_pos;
if (is_upper(cp) && (m_flags & Flags::SmartCase)) {
m_flags &= ~Flags::IgnoreCase;
Copy link

Choose a reason for hiding this comment

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

I believe SmartCase flag should be cleared too as it's not necessary to execute this block for every subsequent uppercase codepoint again and again.

for (ParsedRegex::Node &node : m_parsed_regex.nodes)
node.ignore_case = false;
for (CharacterClass &char_class : m_parsed_regex.character_classes)
Copy link

@d3m3t3r d3m3t3r Apr 10, 2025

Choose a reason for hiding this comment

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

This is insufficient as these character classes might have already been built as case-insensitive (see character_class() method using to_lower() on ranges). E.g. "(?c)[A-Z]+foO" wouldn't match "BARfoO" because '[A-Z]' was already created as '[a-z]'.

char_class.ignore_case = false;

}
return add_node(ParsedRegex::Literal, cp);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/normal/search-character-class/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/(?c)[a-z]ooBar<ret>
1 change: 1 addition & 0 deletions test/normal/search-character-class/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%(f)oobar FooBar fooBar
1 change: 1 addition & 0 deletions test/normal/search-character-class/kak_quoted_selections
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'fooBar'
1 change: 1 addition & 0 deletions test/normal/search-smartcase/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/fooBar<ret>
1 change: 1 addition & 0 deletions test/normal/search-smartcase/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%(f)oobar FooBar fooBar
1 change: 1 addition & 0 deletions test/normal/search-smartcase/kak_quoted_selections
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'fooBar'