Skip to content

Commit

Permalink
Merge pull request sass#2496 from mgreter/bugfix/issue-2465
Browse files Browse the repository at this point in the history
Implement exponents for numbers
  • Loading branch information
mgreter authored Nov 11, 2017
2 parents e772193 + b7175d0 commit c46396a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ namespace Sass {
return unsigned(chr - '0') <= '9' - '0';
}

bool is_number(const char& chr)
{
// adapted the technique from is_alpha
return is_digit(chr) || chr == '-' || chr == '+';
}

bool is_xdigit(const char& chr)
{
// adapted the technique from is_alpha
Expand Down
1 change: 1 addition & 0 deletions src/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Sass {
bool is_alpha(const char& src);
bool is_punct(const char& src);
bool is_digit(const char& src);
bool is_number(const char& src);
bool is_alnum(const char& src);
bool is_xdigit(const char& src);
bool is_unicode(const char& src);
Expand Down
9 changes: 6 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ namespace Sass {
{
Number_Ptr nr = SASS_MEMORY_NEW(Number,
pstate,
sass_atof(parsed.c_str()),
sass_strtod(parsed.c_str()),
"",
number_has_zero(parsed));
nr->is_interpolant(false);
Expand All @@ -1503,7 +1503,7 @@ namespace Sass {
{
Number_Ptr nr = SASS_MEMORY_NEW(Number,
pstate,
sass_atof(parsed.c_str()),
sass_strtod(parsed.c_str()),
"%",
true);
nr->is_interpolant(false);
Expand All @@ -1517,11 +1517,14 @@ namespace Sass {
size_t num_pos = parsed.find_first_not_of(" \n\r\t");
if (num_pos == std::string::npos) num_pos = L;
size_t unit_pos = parsed.find_first_not_of("-+0123456789.", num_pos);
if (parsed[unit_pos] == 'e' && is_number(parsed[unit_pos+1]) ) {
unit_pos = parsed.find_first_not_of("-+0123456789.", ++ unit_pos);
}
if (unit_pos == std::string::npos) unit_pos = L;
const std::string& num = parsed.substr(num_pos, unit_pos - num_pos);
Number_Ptr nr = SASS_MEMORY_NEW(Number,
pstate,
sass_atof(num.c_str()),
sass_strtod(num.c_str()),
Token(number(parsed.c_str())),
number_has_zero(parsed));
nr->is_interpolant(false);
Expand Down
12 changes: 11 additions & 1 deletion src/prelexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,17 @@ namespace Sass {
digits>(src);
}
const char* number(const char* src) {
return sequence< optional<sign>, unsigned_number>(src);
return sequence<
optional<sign>,
unsigned_number,
optional<
sequence<
exactly<'e'>,
optional<sign>,
unsigned_number
>
>
>(src);
}
const char* coefficient(const char* src) {
return alternatives< sequence< optional<sign>, digits >,
Expand Down
6 changes: 3 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Sass {
}

/* Locale unspecific atof function. */
double sass_atof(const char *str)
double sass_strtod(const char *str)
{
char separator = *(localeconv()->decimal_point);
if(separator != '.'){
Expand All @@ -48,13 +48,13 @@ namespace Sass {
// of the string. This is slower but it is thread safe.
char *copy = sass_copy_c_string(str);
*(copy + (found - str)) = separator;
double res = atof(copy);
double res = strtod(copy, NULL);
free(copy);
return res;
}
}

return atof(str);
return strtod(str, NULL);
}

// helper for safe access to c_ctx
Expand Down
2 changes: 1 addition & 1 deletion src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Sass {
} while (0)

double round(double val, size_t precision = 0);
double sass_atof(const char* str);
double sass_strtod(const char* str);
const char* safe_str(const char *, const char* = "");
void free_string_array(char **);
char **copy_strings(const std::vector<std::string>&, char ***, int = 0);
Expand Down

0 comments on commit c46396a

Please sign in to comment.