//===----------------------------------------------------------------------===//
//
// 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-2026 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

#include <cuda/std/__cstring/memcpy.h>
#include <cuda/std/__cstring/memset.h>
#include <cuda/std/__string/constexpr_c_functions.h>

// NOTE(HIP/AMD): HIPRTC does not have <cstring> available; exclude it like NVRTC.
#if _CCCL_HOSTED()
#  include <cstring>
#endif // _CCCL_HOSTED()

#include <cuda/std/__cccl/prologue.h>

_CCCL_BEGIN_NAMESPACE_CUDA_STD

using ::size_t;

_CCCL_API constexpr char* strcpy(char* _CCCL_RESTRICT __dst, const char* _CCCL_RESTRICT __src)
{
  return ::cuda::std::__cccl_strcpy(__dst, __src);
}

_CCCL_API constexpr char* strncpy(char* _CCCL_RESTRICT __dst, const char* _CCCL_RESTRICT __src, size_t __n)
{
  return ::cuda::std::__cccl_strncpy(__dst, __src, __n);
}

[[nodiscard]] _CCCL_API constexpr size_t strlen(const char* __ptr)
{
  return ::cuda::std::__cccl_strlen(__ptr);
}

[[nodiscard]] _CCCL_API constexpr int strcmp(const char* __lhs, const char* __rhs)
{
  return ::cuda::std::__cccl_strcmp(__lhs, __rhs);
}

[[nodiscard]] _CCCL_API constexpr int strncmp(const char* __lhs, const char* __rhs, size_t __n)
{
  return ::cuda::std::__cccl_strncmp(__lhs, __rhs, __n);
}

[[nodiscard]] _CCCL_API constexpr const char* strchr(const char* __ptr, int __c)
{
  return ::cuda::std::__cccl_strchr<const char>(__ptr, static_cast<char>(__c));
}

[[nodiscard]] _CCCL_API constexpr char* strchr(char* __ptr, int __c)
{
  return ::cuda::std::__cccl_strchr(__ptr, static_cast<char>(__c));
}

[[nodiscard]] _CCCL_API constexpr const char* strrchr(const char* __ptr, int __c)
{
  return ::cuda::std::__cccl_strrchr<const char>(__ptr, static_cast<char>(__c));
}

[[nodiscard]] _CCCL_API constexpr char* strrchr(char* __ptr, int __c)
{
  return ::cuda::std::__cccl_strrchr(__ptr, static_cast<char>(__c));
}

_CCCL_API inline const void* memchr(const void* __ptr, int __c, size_t __n) noexcept
{
  return ::cuda::std::__cccl_memchr<const unsigned char>(
    reinterpret_cast<const unsigned char*>(__ptr), static_cast<unsigned char>(__c), __n);
}

_CCCL_API inline void* memchr(void* __ptr, int __c, size_t __n) noexcept
{
  return ::cuda::std::__cccl_memchr(reinterpret_cast<unsigned char*>(__ptr), static_cast<unsigned char>(__c), __n);
}

_CCCL_API inline void* memmove(void* __dst, const void* __src, size_t __n) noexcept
{
  return ::cuda::std::__cccl_memmove(
    reinterpret_cast<unsigned char*>(__dst), reinterpret_cast<const unsigned char*>(__src), __n);
}

[[nodiscard]] _CCCL_API inline int memcmp(const void* __lhs, const void* __rhs, size_t __n) noexcept
{
  return ::cuda::std::__cccl_memcmp(
    reinterpret_cast<const unsigned char*>(__lhs), reinterpret_cast<const unsigned char*>(__rhs), __n);
}

_CCCL_END_NAMESPACE_CUDA_STD

#include <cuda/std/__cccl/epilogue.h>

#endif // _CUDA_STD_CSTRING
