ladybird/Userland/Libraries/LibSQL/AST/Token.cpp
sin-ack 3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00

53 lines
1.2 KiB
C++

/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Token.h"
#include <AK/Assertions.h>
#include <AK/String.h>
#include <stdlib.h>
namespace SQL::AST {
StringView Token::name(TokenType type)
{
switch (type) {
#define __ENUMERATE_SQL_TOKEN(value, type, category) \
case TokenType::type: \
return #type##sv;
ENUMERATE_SQL_TOKENS
#undef __ENUMERATE_SQL_TOKEN
default:
VERIFY_NOT_REACHED();
}
}
TokenCategory Token::category(TokenType type)
{
switch (type) {
#define __ENUMERATE_SQL_TOKEN(value, type, category) \
case TokenType::type: \
return TokenCategory::category;
ENUMERATE_SQL_TOKENS
#undef __ENUMERATE_SQL_TOKEN
default:
VERIFY_NOT_REACHED();
}
}
double Token::double_value() const
{
VERIFY(type() == TokenType::NumericLiteral);
String value(m_value);
if (value[0] == '0' && value.length() >= 2) {
if (value[1] == 'x' || value[1] == 'X')
return static_cast<double>(strtoul(value.characters() + 2, nullptr, 16));
}
return strtod(value.characters(), nullptr);
}
}