//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// MIT License
//
// Modifications Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef _CUDA_STD_CSTRING
#define _CUDA_STD_CSTRING

#include <cuda/std/detail/__config>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#if !_CCCL_COMPILER(NVRTC) && !defined(_CCCL_COMPILER_HIPRTC)
#  include <cstring>
#endif // !_CCCL_COMPILER(NVRTC)

_CCCL_PUSH_MACROS

_LIBCUDACXX_BEGIN_NAMESPACE_STD

using ::memcpy;
using ::memset;
using ::size_t;

#if _CCCL_HAS_CUDA_COMPILER()
_CCCL_HIDE_FROM_ABI _CCCL_DEVICE const void* __memchr_device(const void* __ptr, int __c, size_t __n) noexcept
{
  auto __p = static_cast<const unsigned char*>(__ptr);

  while (__n--)
  {
    if (*__p == static_cast<unsigned char>(__c))
    {
      return __p;
    }
    ++__p;
  }

  return nullptr;
}
#endif // _CCCL_HAS_CUDA_COMPILER()

_LIBCUDACXX_HIDE_FROM_ABI const void* memchr(const void* __ptr, int __c, size_t __n) noexcept
{
  NV_IF_ELSE_TARGET(
    NV_IS_HOST_LIBHIPCXX, (return ::memchr(__ptr, __c, __n);), (return _CUDA_VSTD::__memchr_device(__ptr, __c, __n);))
}

_LIBCUDACXX_HIDE_FROM_ABI void* memchr(void* __ptr, int __c, size_t __n) noexcept
{
  NV_IF_ELSE_TARGET(NV_IS_HOST_LIBHIPCXX,
                    (return ::memchr(__ptr, __c, __n);),
                    (return const_cast<void*>(_CUDA_VSTD::memchr(const_cast<const void*>(__ptr), __c, __n));))
}

#if _CCCL_HAS_CUDA_COMPILER()
_CCCL_HIDE_FROM_ABI _CCCL_DEVICE void* __memmove_device(void* __dst, const void* __src, size_t __n) noexcept
{
  auto __d = (__dst <= __src) ? static_cast<unsigned char*>(__dst) : (static_cast<unsigned char*>(__dst) + __n - 1);
  auto __s =
    (__dst <= __src) ? static_cast<const unsigned char*>(__src) : (static_cast<const unsigned char*>(__src) + __n - 1);
  const auto __inc = (__dst <= __src) ? 1 : -1;

  while (__n--)
  {
    *__d = *__s;
    __d += __inc;
    __s += __inc;
  }

  return __dst;
}
#endif // _CCCL_HAS_CUDA_COMPILER()

_LIBCUDACXX_HIDE_FROM_ABI void* memmove(void* __dst, const void* __src, size_t __n) noexcept
{
#if defined(_CCCL_BUILTIN_MEMMOVE)
  return _CCCL_BUILTIN_MEMMOVE(__dst, __src, __n);
#else // ^^^ _CCCL_BUILTIN_MEMMOVE ^^^ / vvv !_CCCL_BUILTIN_MEMMOVE vvv
  NV_IF_ELSE_TARGET(
    NV_IS_HOST_LIBHIPCXX, (return ::memmove(__dst, __src, __n);), (return _CUDA_VSTD::__memmove_device(__dst, __src, __n);))
#endif // ^^^ !_CCCL_BUILTIN_MEMMOVE ^^^
}

#if _CCCL_HAS_CUDA_COMPILER()
_CCCL_HIDE_FROM_ABI _CCCL_DEVICE int __memcmp_device(const void* __lhs, const void* __rhs, size_t __n) noexcept
{
  auto __l = static_cast<const unsigned char*>(__lhs);
  auto __r = static_cast<const unsigned char*>(__rhs);

  while (__n--)
  {
    if (*__l != *__r)
    {
      return *__l < *__r ? -1 : 1;
    }
    ++__l;
    ++__r;
  }
  return 0;
}
#endif // _CCCL_HAS_CUDA_COMPILER()

_LIBCUDACXX_HIDE_FROM_ABI int memcmp(const void* __lhs, const void* __rhs, size_t __n) noexcept
{
#if defined(_CCCL_BUILTIN_MEMCMP)
  return _CCCL_BUILTIN_MEMCMP(__lhs, __rhs, __n);
#else // ^^^ _CCCL_BUILTIN_MEMCMP ^^^ / vvv !_CCCL_BUILTIN_MEMCMP vvv
  NV_IF_ELSE_TARGET(
    NV_IS_HOST_LIBHIPCXX, (return ::memcmp(__lhs, __rhs, __n);), (return _CUDA_VSTD::__memcmp_device(__lhs, __rhs, __n);))
#endif // ^^^ !_CCCL_BUILTIN_MEMCMP ^^^
}

_LIBCUDACXX_END_NAMESPACE_STD

_CCCL_POP_MACROS

#endif // _CUDA_STD_CSTRING
