Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions flang-rt/lib/runtime/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ inline RT_API_ATTRS std::size_t Index(const CHAR *x, std::size_t xLen,
}
return 0;
}
if (wantLen == 1) {
// Trivial case for single character lookup.
// We can use simple forward search.
CHAR ch = want[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

This could just become a call to std::memchr() for KIND==1.

For other kinds, please add braced initialization to the two declarations here.

for (std::size_t at = 0; at < xLen; ++at) {
if (x[at] == ch) {
return at + 1;
}
}
return 0;
}
// Non-trivial forward substring search: use a simplified form of
// Boyer-Moore substring searching.
for (std::size_t at{1}; at + wantLen - 1 <= xLen;) {
Expand Down
1 change: 1 addition & 0 deletions flang-rt/unittests/Runtime/CharacterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ TYPED_TEST(SearchTests, IndexTests) {
{"", "a", true, 0},
{"aa", "a", false, 1},
{"aa", "a", true, 2},
{"aAA", "A", false, 2},
{"Fortran that I ran", "that I ran", false, 9},
{"Fortran that I ran", "that I ran", true, 9},
{"Fortran that you ran", "that I ran", false, 0},
Expand Down
Loading