Skip to content

Commit 95d6ee6

Browse files
committed
Use memchr.
1 parent 31e7881 commit 95d6ee6

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

flang-rt/lib/runtime/character.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "flang/Common/uint128.h"
1515
#include "flang/Runtime/character.h"
1616
#include "flang/Runtime/cpp-type.h"
17+
#include "flang/Runtime/freestanding-tools.h"
1718
#include <algorithm>
1819
#include <cstring>
1920

@@ -296,10 +297,18 @@ inline RT_API_ATTRS std::size_t Index(const CHAR *x, std::size_t xLen,
296297
if (wantLen == 1) {
297298
// Trivial case for single character lookup.
298299
// We can use simple forward search.
299-
CHAR ch = want[0];
300-
for (std::size_t at = 0; at < xLen; ++at) {
301-
if (x[at] == ch) {
302-
return at + 1;
300+
CHAR ch{want[0]};
301+
if constexpr (std::is_same_v<CHAR, char>) {
302+
auto pos{reinterpret_cast<const CHAR *>(
303+
Fortran::runtime::memchr(x, ch, xLen))};
304+
if (pos) {
305+
return pos - x + 1;
306+
}
307+
} else {
308+
for (std::size_t at{0}; at < xLen; ++at) {
309+
if (x[at] == ch) {
310+
return at + 1;
311+
}
303312
}
304313
}
305314
return 0;

0 commit comments

Comments
 (0)