[K/N] Move memmem from Porting.h to KString.cpp ^KT-59008
This commit is contained in:
committed by
Space Cloud
parent
d9071aed78
commit
27c85a059f
@@ -111,6 +111,16 @@ int threeWayCompare(T a, T b) {
|
|||||||
return (a == b) ? 0 : (a < b ? -1 : 1);
|
return (a == b) ? 0 : (a < b ? -1 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if KONAN_WINDOWS
|
||||||
|
void* memmem(const void *big, size_t bigLen, const void *little, size_t littleLen) {
|
||||||
|
for (size_t i = 0; i + littleLen <= bigLen; ++i) {
|
||||||
|
void* pos = ((char*)big) + i;
|
||||||
|
if (memcmp(little, pos, littleLen) == 0) return pos;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -388,7 +398,9 @@ KInt Kotlin_String_lastIndexOfChar(KString thiz, KChar ch, KInt fromIndex) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: or code up Knuth-Moris-Pratt.
|
// TODO: or code up Knuth-Moris-Pratt,
|
||||||
|
// or use std::search with std::boyer_moore_searcher:
|
||||||
|
// https://en.cppreference.com/w/cpp/algorithm/search
|
||||||
KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
|
KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
|
||||||
if (fromIndex < 0) {
|
if (fromIndex < 0) {
|
||||||
fromIndex = 0;
|
fromIndex = 0;
|
||||||
@@ -407,7 +419,7 @@ KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
|
|||||||
const KChar* otherRaw = CharArrayAddressOfElementAt(other, 0);
|
const KChar* otherRaw = CharArrayAddressOfElementAt(other, 0);
|
||||||
const auto otherSize = other->count_ * sizeof(KChar);
|
const auto otherSize = other->count_ * sizeof(KChar);
|
||||||
while (true) {
|
while (true) {
|
||||||
void* result = konan::memmem(thizRaw + fromIndex, (thiz->count_ - fromIndex) * sizeof(KChar),
|
void* result = memmem(thizRaw + fromIndex, (thiz->count_ - fromIndex) * sizeof(KChar),
|
||||||
otherRaw, otherSize);
|
otherRaw, otherSize);
|
||||||
if (result == nullptr) return -1;
|
if (result == nullptr) return -1;
|
||||||
auto byteIndex = reinterpret_cast<intptr_t>(result) - reinterpret_cast<intptr_t>(thizRaw);
|
auto byteIndex = reinterpret_cast<intptr_t>(result) - reinterpret_cast<intptr_t>(thizRaw);
|
||||||
|
|||||||
@@ -251,22 +251,6 @@ NO_EXTERNAL_CALLS_CHECK int currentThreadId() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// String/byte operations.
|
|
||||||
// memcpy/memmove are not here intentionally, as frequently implemented/optimized
|
|
||||||
// by C compiler.
|
|
||||||
void* memmem(const void *big, size_t bigLen, const void *little, size_t littleLen) {
|
|
||||||
#if KONAN_NO_MEMMEM
|
|
||||||
for (size_t i = 0; i + littleLen <= bigLen; ++i) {
|
|
||||||
void* pos = ((char*)big) + i;
|
|
||||||
if (::memcmp(little, pos, littleLen) == 0) return pos;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
#else
|
|
||||||
return ::memmem(big, bigLen, little, littleLen);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Time operations.
|
// Time operations.
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
|
|||||||
@@ -40,11 +40,6 @@ void onThreadExit(void (*destructor)(void*), void* destructorParameter);
|
|||||||
bool isOnThreadExitNotSetOrAlreadyStarted();
|
bool isOnThreadExitNotSetOrAlreadyStarted();
|
||||||
int currentThreadId();
|
int currentThreadId();
|
||||||
|
|
||||||
// String/byte operations.
|
|
||||||
// memcpy/memmove/memcmp are not here intentionally, as frequently implemented/optimized
|
|
||||||
// by C compiler.
|
|
||||||
void* memmem(const void *big, size_t bigLen, const void *little, size_t littleLen);
|
|
||||||
|
|
||||||
// Time operations.
|
// Time operations.
|
||||||
uint64_t getTimeMillis();
|
uint64_t getTimeMillis();
|
||||||
uint64_t getTimeMicros();
|
uint64_t getTimeMicros();
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ sealed class ClangArgs(
|
|||||||
"WINDOWS".takeIf { target.family == Family.MINGW },
|
"WINDOWS".takeIf { target.family == Family.MINGW },
|
||||||
"MACOSX".takeIf { target.family == Family.OSX },
|
"MACOSX".takeIf { target.family == Family.OSX },
|
||||||
|
|
||||||
"NO_MEMMEM".takeUnless { target.suportsMemMem() },
|
|
||||||
"NO_64BIT_ATOMIC".takeUnless { target.supports64BitAtomics() },
|
"NO_64BIT_ATOMIC".takeUnless { target.supports64BitAtomics() },
|
||||||
"NO_UNALIGNED_ACCESS".takeUnless { target.supportsUnalignedAccess() },
|
"NO_UNALIGNED_ACCESS".takeUnless { target.supportsUnalignedAccess() },
|
||||||
"FORBID_BUILTIN_MUL_OVERFLOW".takeUnless { target.supports64BitMulOverflow() },
|
"FORBID_BUILTIN_MUL_OVERFLOW".takeUnless { target.supports64BitMulOverflow() },
|
||||||
|
|||||||
@@ -99,14 +99,6 @@ fun KonanTarget.supportsExceptions(): Boolean = when(this) {
|
|||||||
else -> true
|
else -> true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KonanTarget.suportsMemMem(): Boolean = when (this) {
|
|
||||||
is KonanTarget.WASM32 -> false
|
|
||||||
is KonanTarget.MINGW_X86 -> false
|
|
||||||
is KonanTarget.MINGW_X64 -> false
|
|
||||||
is KonanTarget.ZEPHYR -> false
|
|
||||||
else -> true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun KonanTarget.supportsObjcInterop(): Boolean = family.isAppleFamily
|
fun KonanTarget.supportsObjcInterop(): Boolean = family.isAppleFamily
|
||||||
fun KonanTarget.hasFoundationFramework(): Boolean = family.isAppleFamily
|
fun KonanTarget.hasFoundationFramework(): Boolean = family.isAppleFamily
|
||||||
fun KonanTarget.hasUIKitFramework(): Boolean = family == Family.IOS || family == Family.TVOS
|
fun KonanTarget.hasUIKitFramework(): Boolean = family == Family.IOS || family == Family.TVOS
|
||||||
|
|||||||
Reference in New Issue
Block a user