1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-10 19:32:10 +00:00
luanti/src/util/enum_string.cpp

32 lines
608 B
C++
Raw Normal View History

// Luanti
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2025 cx384
#include "util/enum_string.h"
#include <cassert>
bool string_to_enum(const EnumString *spec, int &result, std::string_view str)
{
const EnumString *esp = spec;
while (esp->str) {
if (str == esp->str) {
assert(esp->num >= 0);
result = esp->num;
return true;
}
esp++;
}
return false;
}
const char *enum_to_string(const EnumString *spec, int num)
{
if (num < 0)
return nullptr;
// assume array order matches enum order
auto *p = &spec[num];
assert(p->num == num);
assert(p->str);
return p->str;
}