omni/compiletime/CompileTime.h

File members: omni/compiletime/CompileTime.h

// Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once

#include "../../carb/Defines.h"

#include <cstdint>

namespace omni
{
namespace compiletime
{

constexpr size_t strlen(const char* str)
{
    return *str ? 1 + strlen(str + 1) : 0;
}

constexpr int strcmp(const char* a, const char* b)
{
    return ((*a == *b) ? (*a ? strcmp(a + 1, b + 1) : 0) : (*a - *b));
}

// compile time unit tests...
static_assert(0 == strlen(""), "compiletime::strlen logic error");
static_assert(1 == strlen("a"), "compiletime::strlen logic error");
static_assert(2 == strlen("ab"), "compiletime::strlen logic error");
static_assert(strcmp("b", "c") < 0, "compiletime::strcmp logic error");
static_assert(strcmp("b", "a") > 0, "compiletime::strcmp logic error");
static_assert(strcmp("b", "b") == 0, "compiletime::strcmp logic error");
static_assert(strcmp("", "") == 0, "compiletime::strcmp logic error");
static_assert(strcmp("", "a") < 0, "compiletime::strcmp logic error");
static_assert(strcmp("a", "") > 0, "compiletime::strcmp logic error");
static_assert(strcmp("carbonite", "carb") > 0, "compiletime::strcmp logic error");

} // namespace compiletime
} // namespace omni