mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibJS: Add numeric literal parsing for different bases and exponents
This commit is contained in:
parent
b82a2239c6
commit
500f6d9e3a
Notes:
sideshowbarker
2024-07-19 07:53:33 +09:00
Author: https://github.com/sunverwerth
Commit: 500f6d9e3a
Pull-request: https://github.com/SerenityOS/serenity/pull/1646
Reviewed-by: https://github.com/linusg
4 changed files with 107 additions and 5 deletions
|
@ -27,6 +27,7 @@
|
|||
#include "Token.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <ctype.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -52,7 +53,23 @@ const char* Token::name() const
|
|||
double Token::double_value() const
|
||||
{
|
||||
ASSERT(type() == TokenType::NumericLiteral);
|
||||
return strtod(String(m_value).characters(), nullptr);
|
||||
String value_string(m_value);
|
||||
if (value_string[0] == '0' && value_string.length() >= 2) {
|
||||
if (value_string[1] == 'x' || value_string[1] == 'X') {
|
||||
// hexadecimal
|
||||
return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 16));
|
||||
} else if (value_string[1] == 'o' || value_string[1] == 'O') {
|
||||
// octal
|
||||
return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 8));
|
||||
} else if (value_string[1] == 'b' || value_string[1] == 'B') {
|
||||
// binary
|
||||
return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 2));
|
||||
} else if (isdigit(value_string[1])) {
|
||||
// also octal, but syntax error in strict mode
|
||||
return static_cast<double>(strtoul(value_string.characters() + 1, nullptr, 8));
|
||||
}
|
||||
}
|
||||
return strtod(value_string.characters(), nullptr);
|
||||
}
|
||||
|
||||
String Token::string_value() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue