Skip to content

Commit 384df9e

Browse files
committed
Implement exponents for numbers
1 parent 331fe0c commit 384df9e

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

src/lexer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ namespace Sass {
4949
return unsigned(chr - '0') <= '9' - '0';
5050
}
5151

52+
bool is_number(const char& chr)
53+
{
54+
// adapted the technique from is_alpha
55+
return is_digit(chr) || chr == '-' || chr == '+';
56+
}
57+
5258
bool is_xdigit(const char& chr)
5359
{
5460
// adapted the technique from is_alpha

src/lexer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Sass {
2929
bool is_alpha(const char& src);
3030
bool is_punct(const char& src);
3131
bool is_digit(const char& src);
32+
bool is_number(const char& src);
3233
bool is_alnum(const char& src);
3334
bool is_xdigit(const char& src);
3435
bool is_unicode(const char& src);

src/parser.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ namespace Sass {
14721472
{
14731473
Number_Ptr nr = SASS_MEMORY_NEW(Number,
14741474
pstate,
1475-
sass_atof(parsed.c_str()),
1475+
sass_strtod(parsed.c_str()),
14761476
"",
14771477
number_has_zero(parsed));
14781478
nr->is_interpolant(false);
@@ -1484,7 +1484,7 @@ namespace Sass {
14841484
{
14851485
Number_Ptr nr = SASS_MEMORY_NEW(Number,
14861486
pstate,
1487-
sass_atof(parsed.c_str()),
1487+
sass_strtod(parsed.c_str()),
14881488
"%",
14891489
true);
14901490
nr->is_interpolant(false);
@@ -1498,11 +1498,14 @@ namespace Sass {
14981498
size_t num_pos = parsed.find_first_not_of(" \n\r\t");
14991499
if (num_pos == std::string::npos) num_pos = L;
15001500
size_t unit_pos = parsed.find_first_not_of("-+0123456789.", num_pos);
1501+
if (parsed[unit_pos] == 'e' && is_number(parsed[unit_pos+1]) ) {
1502+
unit_pos = parsed.find_first_not_of("-+0123456789.", ++ unit_pos);
1503+
}
15011504
if (unit_pos == std::string::npos) unit_pos = L;
15021505
const std::string& num = parsed.substr(num_pos, unit_pos - num_pos);
15031506
Number_Ptr nr = SASS_MEMORY_NEW(Number,
15041507
pstate,
1505-
sass_atof(num.c_str()),
1508+
sass_strtod(num.c_str()),
15061509
Token(number(parsed.c_str())),
15071510
number_has_zero(parsed));
15081511
nr->is_interpolant(false);

src/prelexer.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,17 @@ namespace Sass {
998998
digits>(src);
999999
}
10001000
const char* number(const char* src) {
1001-
return sequence< optional<sign>, unsigned_number>(src);
1001+
return sequence<
1002+
optional<sign>,
1003+
unsigned_number,
1004+
optional<
1005+
sequence<
1006+
exactly<'e'>,
1007+
optional<sign>,
1008+
unsigned_number
1009+
>
1010+
>
1011+
>(src);
10021012
}
10031013
const char* coefficient(const char* src) {
10041014
return alternatives< sequence< optional<sign>, digits >,

src/util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Sass {
3535
}
3636

3737
/* Locale unspecific atof function. */
38-
double sass_atof(const char *str)
38+
double sass_strtod(const char *str)
3939
{
4040
char separator = *(localeconv()->decimal_point);
4141
if(separator != '.'){
@@ -48,13 +48,13 @@ namespace Sass {
4848
// of the string. This is slower but it is thread safe.
4949
char *copy = sass_copy_c_string(str);
5050
*(copy + (found - str)) = separator;
51-
double res = atof(copy);
51+
double res = strtod(copy, NULL);
5252
free(copy);
5353
return res;
5454
}
5555
}
5656

57-
return atof(str);
57+
return strtod(str, NULL);
5858
}
5959

6060
// helper for safe access to c_ctx

src/util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Sass {
1818
} while (0)
1919

2020
double round(double val, size_t precision = 0);
21-
double sass_atof(const char* str);
21+
double sass_strtod(const char* str);
2222
const char* safe_str(const char *, const char* = "");
2323
void free_string_array(char **);
2424
char **copy_strings(const std::vector<std::string>&, char ***, int = 0);

0 commit comments

Comments
 (0)