Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 07e9ead

Browse files
author
Aaron Leung
committed
making the precision configurable
1 parent 5660bfe commit 07e9ead

File tree

8 files changed

+31
-22
lines changed

8 files changed

+31
-22
lines changed

context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace Sass {
5656
source_map_file (initializers.source_map_file()),
5757
names_to_colors (map<string, Color*>()),
5858
colors_to_names (map<int, string>()),
59+
precision (initializers.precision()),
5960
extensions(multimap<Compound_Selector, Complex_Selector*>()),
6061
subset_map(Subset_Map<string, pair<Complex_Selector*, Compound_Selector*> >())
6162
{

context.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ namespace Sass {
5454
map<string, Color*> names_to_colors;
5555
map<int, string> colors_to_names;
5656

57+
size_t precision; // precision for outputting fractional numbers
58+
5759
KWD_ARG_SET(Data) {
5860
KWD_ARG(Data, const char*, source_c_str);
5961
KWD_ARG(Data, string, entry_point);
@@ -65,7 +67,8 @@ namespace Sass {
6567
KWD_ARG(Data, bool, source_comments);
6668
KWD_ARG(Data, bool, source_maps);
6769
KWD_ARG(Data, Output_Style, output_style);
68-
KWD_ARG(Data, string, source_map_file)
70+
KWD_ARG(Data, string, source_map_file);
71+
KWD_ARG(Data, size_t, precision);
6972
};
7073

7174
Context(Data);

eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ namespace Sass {
477477
Expression* Eval::operator()(String_Schema* s)
478478
{
479479
string acc;
480-
To_String to_string;
480+
To_String to_string(&ctx);
481481
for (size_t i = 0, L = s->length(); i < L; ++i) {
482482
string chunk((*s)[i]->perform(this)->perform(&to_string));
483483
if (((s->quote_mark() && is_quoted(chunk)) || !s->quote_mark()) && (*s)[i]->is_interpolant()) { // some redundancy in that test

inspect.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -287,30 +287,30 @@ namespace Sass {
287287
}
288288

289289
// helper functions for serializing numbers
290-
string frac_to_string(double f, size_t p) {
291-
stringstream ss;
292-
ss.setf(ios::fixed, ios::floatfield);
293-
ss.precision(p);
294-
ss << f;
295-
string result(ss.str().substr(f < 0 ? 2 : 1));
296-
size_t i = result.size() - 1;
297-
while (result[i] == '0') --i;
298-
result = result.substr(0, i+1);
299-
return result;
300-
}
301-
string double_to_string(double d, size_t p) {
302-
stringstream ss;
303-
double ipart;
304-
double fpart = std::modf(d, &ipart);
305-
ss << ipart;
306-
if (fpart != 0) ss << frac_to_string(fpart, 5);
307-
return ss.str();
308-
}
290+
// string frac_to_string(double f, size_t p) {
291+
// stringstream ss;
292+
// ss.setf(ios::fixed, ios::floatfield);
293+
// ss.precision(p);
294+
// ss << f;
295+
// string result(ss.str().substr(f < 0 ? 2 : 1));
296+
// size_t i = result.size() - 1;
297+
// while (result[i] == '0') --i;
298+
// result = result.substr(0, i+1);
299+
// return result;
300+
// }
301+
// string double_to_string(double d, size_t p) {
302+
// stringstream ss;
303+
// double ipart;
304+
// double fpart = std::modf(d, &ipart);
305+
// ss << ipart;
306+
// if (fpart != 0) ss << frac_to_string(fpart, 5);
307+
// return ss.str();
308+
// }
309309

310310
void Inspect::operator()(Number* n)
311311
{
312312
stringstream ss;
313-
ss.precision(5);
313+
ss.precision(ctx ? ctx->precision : 5);
314314
ss << fixed << n->value();
315315
string d(ss.str());
316316
for (size_t i = d.length()-1; d[i] == '0'; --i) {

sass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern "C" {
5454
.include_paths_c_str (c_ctx->include_paths_string)
5555
.include_paths_array (/*c_ctx->include_paths_array*/0)
5656
.include_paths (vector<string>())
57+
.precision (c_ctx->precision ? c_ctx->precision : 5)
5758
);
5859
if (src_option == FILE_SOURCE) cpp_ctx.compile_file();
5960
else cpp_ctx.compile_string();

sass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct Sass_Context {
2626
const char* image_path;
2727
const char* include_paths_string;
2828
const char** include_paths_array;
29+
int precision;
2930
};
3031

3132
struct Sass_Context* make_sass_context ();

sass_interface.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extern "C" {
9696
.include_paths_c_str(c_ctx->options.include_paths)
9797
.include_paths_array(0)
9898
.include_paths(vector<string>())
99+
.precision(c_ctx->options.precision ? c_ctx->options.precision : 5)
99100
);
100101
c_ctx->output_string = cpp_ctx.compile_string();
101102
c_ctx->error_message = 0;
@@ -143,6 +144,7 @@ extern "C" {
143144
.include_paths_c_str(c_ctx->options.include_paths)
144145
.include_paths_array(0)
145146
.include_paths(vector<string>())
147+
.precision(c_ctx->options.precision ? c_ctx->options.precision : 5)
146148
);
147149
c_ctx->output_string = cpp_ctx.compile_file();
148150
c_ctx->source_map_string = cpp_ctx.generate_source_map();

sass_interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct sass_options {
2020
int source_comments; // really want a bool, but C doesn't have them
2121
const char* include_paths;
2222
const char* image_path;
23+
int precision;
2324
};
2425

2526
struct sass_context {

0 commit comments

Comments
 (0)