Skip to content

Commit aabb867

Browse files
committed
Refactor inspection/output of numbers
1 parent e94b5f9 commit aabb867

File tree

1 file changed

+19
-60
lines changed

1 file changed

+19
-60
lines changed

src/inspect.cpp

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -519,82 +519,41 @@ namespace Sass {
519519
void Inspect::operator()(Number_Ptr n)
520520
{
521521

522-
std::string res;
523-
524522
// reduce units
525523
n->reduce();
526524

527-
// check if the fractional part of the value equals to zero
528-
// neat trick from http://stackoverflow.com/a/1521682/1550314
529-
// double int_part; bool is_int = modf(value, &int_part) == 0.0;
530-
531-
// this all cannot be done with one run only, since fixed
532-
// output differs from normal output and regular output
533-
// can contain scientific notation which we do not want!
534-
535-
// first sample
536525
std::stringstream ss;
537-
ss.precision(12);
538-
ss << n->value();
539-
540-
// check if we got scientific notation in result
541-
if (ss.str().find_first_of("e") != std::string::npos) {
542-
ss.clear(); ss.str(std::string());
543-
ss.precision(std::max(12, opt.precision));
544-
ss << std::fixed << n->value();
545-
}
546-
547-
std::string tmp = ss.str();
548-
size_t pos_point = tmp.find_first_of(".,");
549-
size_t pos_fract = tmp.find_last_not_of("0");
550-
bool is_int = pos_point == pos_fract ||
551-
pos_point == std::string::npos;
526+
ss.precision(opt.precision);
527+
ss << std::fixed << n->value();
552528

553-
// reset stream for another run
554-
ss.clear(); ss.str(std::string());
529+
std::string res = ss.str();
530+
int s = res.length();
555531

556-
// take a shortcut for integers
557-
if (is_int)
532+
// delete trailing zeros
533+
for(s = s - 1; s > 0; --s)
558534
{
559-
ss.precision(0);
560-
ss << std::fixed << n->value();
561-
res = std::string(ss.str());
562-
}
563-
// process floats
564-
else
565-
{
566-
// do we have have too much precision?
567-
if (pos_fract < opt.precision + pos_point)
568-
{ ss.precision((int)(pos_fract - pos_point)); }
569-
else { ss.precision(opt.precision); }
570-
// round value again
571-
ss << std::fixed << n->value();
572-
res = std::string(ss.str());
573-
// maybe we truncated up to decimal point
574-
size_t pos = res.find_last_not_of("0");
575-
// handle case where we have a "0"
576-
if (pos == std::string::npos) {
577-
res = "0.0";
578-
} else {
579-
bool at_dec_point = res[pos] == '.' ||
580-
res[pos] == ',';
581-
// don't leave a blank point
582-
if (at_dec_point) ++ pos;
583-
res.resize (pos + 1);
584-
}
535+
if(res[s] == '0') {
536+
res.erase(s, 1);
537+
}
538+
else break;
585539
}
586540

541+
// delete trailing decimal separator
542+
if(res[s] == '.') res.erase(s, 1);
543+
587544
// some final cosmetics
588545
if (res == "0.0") res = "0";
589546
else if (res == "") res = "0";
590547
else if (res == "-0") res = "0";
591548
else if (res == "-0.0") res = "0";
592549
else if (opt.output_style == COMPRESSED)
593550
{
594-
// check if handling negative nr
595-
size_t off = res[0] == '-' ? 1 : 0;
596-
// remove leading zero from floating point in compressed mode
597-
if (n->zero() && res[off] == '0' && res[off+1] == '.') res.erase(off, 1);
551+
if (n->zero()) {
552+
// check if handling negative nr
553+
size_t off = res[0] == '-' ? 1 : 0;
554+
// remove leading zero from floating point in compressed mode
555+
if (res[off] == '0' && res[off+1] == '.') res.erase(off, 1);
556+
}
598557
}
599558

600559
// add unit now

0 commit comments

Comments
 (0)