8000 Rewrite convert_to_string using std::string by QuLogic · Pull Request #13332 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Rewrite convert_to_string using std::string #13332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 44 additions & 79 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>

#include "agg_conv_contour.h"
#include "agg_conv_curve.h"
Expand Down Expand Up @@ -1044,40 +1045,11 @@ void quad2cubic(double x0, double y0,
outy[2] = y2;
}

char *__append_to_string(char *p, char **buffer, size_t *buffersize,
const char *content)
{
for (const char *i = content; *i; ++i) {
if (p < *buffer) {
/* This is just an internal error */
return NULL;
}
if ((size_t)(p - *buffer) >= *buffersize) {
char *newbuffer;
ptrdiff_t diff = p - *buffer;
*buffersize *= 2;
newbuffer = (char *)realloc(*buffer, *buffersize);
if (newbuffer == NULL) {
return NULL;
}
*buffer = newbuffer;
p = *buffer + diff;
}

*p++ = *i;
}

return p;
}


char *__add_number(double val, const char *format, int precision,
char **buffer, char *p, size_t *buffersize)
void __add_number(double val, char format_code, int precision,
std::string& buffer)
{
char *result;

char *str;
str = PyOS_double_to_string(val, format[0], precision, 0, NULL);
char *str = PyOS_double_to_string(val, format_code, precision, 0, NULL);

// Delete trailing zeros and decimal point
char *q = str;
Expand All @@ -1090,7 +1062,7 @@ char *__add_number(double val, const char *format, int precision,
// Rewind through all the zeros
}

// If the end is a decimal qoint, delete that too
// If the end is a decimal point, delete that too
if (q >= str && *q == '.') {
--q;
}
Expand All @@ -1099,27 +1071,25 @@ char *__add_number(double val, const char *format, int precision,
++q;
*q = 0;

if ((result = __append_to_string(p, buffer, buffersize, str)) == NULL) {
try {
buffer += str;
} catch (std::bad_alloc& e) {
PyMem_Free(str);
return NULL;
throw e;
}
PyMem_Free(str);

return result;
}


template <class PathIterator>
int __convert_to_string(PathIterator &path,
int precision,
char **codes,
bool postfix,
char **buffer,
size_t *buffersize)
bool __convert_to_string(PathIterator &path,
int precision,
char **codes,
bool postfix,
std::string& buffer)
{
const char *format = "f";
const char format_code = 'f';

char *p = *buffer;
double x[3];
double y[3];
double last_x = 0.0;
Expand All @@ -1131,14 +1101,14 @@ int __convert_to_string(PathIterator &path,

while ((code = path.vertex(&x[0], &y[0])) != agg::path_cmd_stop) {
if (code == 0x4f) {
if ((p = __append_to_string(p, buffer, buffersize, codes[4])) == NULL) return 1;
buffer += codes[4];
} else if (code < 5) {
size = sizes[code - 1];

for (int i = 1; i < size; ++i) {
unsigned subcode = path.vertex(&x[i], &y[i]);
if (subcode != code) {
return 2;
return false;
}
}

Expand All @@ -1151,48 +1121,46 @@ int __convert_to_string(PathIterator &path,
}

if (!postfix) {
if ((p = __append_to_string(p, buffer, buffersize, codes[code - 1])) == NULL) return 1;
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
buffer += codes[code - 1];
buffer += ' ';
}

for (int i = 0; i < size; ++i) {
if ((p = __add_number(x[i], format, precision, buffer, p, buffersize)) == NULL) return 1;
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
if ((p = __add_number(y[i], format, precision, buffer, p, buffersize)) == NULL) return 1;
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
__add_number(x[i], format_code, precision, buffer);
buffer += ' ';
__add_number(y[i], format_code, precision, buffer);
buffer += ' ';
}

if (postfix) {
if ((p = __append_to_string(p, buffer, buffersize, codes[code - 1])) == NULL) return 1;
buffer += codes[code - 1];
}

last_x = x[size - 1];
last_y = y[size - 1];
} else {
// Unknown code value
return 2;
return false;
}

if ((p = __append_to_string(p, buffer, buffersize, "\n")) == NULL) return 1;
buffer += '\n';
}

*buffersize = p - *buffer;

return 0;
return true;
}

template <class PathIterator>
int convert_to_string(PathIterator &path,
agg::trans_affine &trans,
agg::rect_d &clip_rect,
bool simplify,
SketchParams sketch_params,
int precision,
char **codes,
bool postfix,
char **buffer,
size_t *buffersize)
bool convert_to_string(PathIterator &path,
agg::trans_affine &trans,
agg::rect_d &clip_rect,
bool simplify,
SketchParams sketch_params,
int precision,
char **codes,
bool postfix,
std::string& buffer)
{
size_t buffersize;
typedef agg::conv_transform<py::PathIterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> nan_removal_t;
typedef PathClipper<nan_removal_t> clipped_t;
Expand All @@ -1207,26 +1175,23 @@ int convert_to_string(PathIterator &path,
clipped_t clipped(nan_removed, do_clip && !path.has_curves(), clip_rect);
simplify_t simplified(clipped, simplify, path.simplify_threshold());

*buffersize = path.total_vertices() * (precision + 5) * 4;
if (*buffersize == 0) {
return 0;
buffersize = path.total_vertices() * (precision + 5) * 4;
if (buffersize == 0) {
return true;
}

if (sketch_params.scale != 0.0) {
*buffersize *= 10;
buffersize *= 10;
}

*buffer = (char *)malloc(*buffersize);
if (*buffer == NULL) {
return 1;
}
buffer.reserve(buffersize);

if (sketch_params.scale == 0.0) {
return __convert_to_string(simplified, precision, codes, postfix, buffer, buffersize);
return __convert_to_string(simplified, precision, codes, postfix, buffer);
} else {
curve_t curve(simplified);
sketch_t sketch(curve, sketch_params.scale, sketch_params.length, sketch_params.randomness);
return __convert_to_string(sketch, precision, codes, postfix, buffer, buffersize);
return __convert_to_string(sketch, precision, codes, postfix, buffer);
}

}
Expand Down
30 changes: 7 additions & 23 deletions src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,8 @@ static PyObject *Py_convert_to_string(PyObject *self, PyObject *args, PyObject *
PyObject *codesobj;
char *codes[5];
bool postfix;
char *buffer = NULL;
size_t buffersize;
PyObject *result;
int status;
std::string buffer;
bool status;

if (!PyArg_ParseTuple(args,
"O&O&O&OO&iOO&:convert_to_string",
Expand Down Expand Up @@ -735,28 +733,14 @@ static PyObject *Py_convert_to_string(PyObject *self, PyObject *args, PyObject *
CALL_CPP("convert_to_string",
(status = convert_to_string(
path, trans, cliprect, simplify, sketch,
precision, codes, postfix, &buffer,
&buffersize)));

if (status) {
free(buffer);
if (status == 1) {
PyErr_SetString(PyExc_MemoryError, "Memory error");
} else if (status == 2) {
PyErr_SetString(PyExc_ValueError, "Malformed path codes");
}
return NULL;
}
precision, codes, postfix, buffer)));

if (buffersize == 0) {
result = PyBytes_FromString("");
} else {
result = PyBytes_FromStringAndSize(buffer, buffersize);
if (!status) {
PyErr_SetString(PyExc_ValueError, "Malformed path codes");
return NULL;
}

free(buffer);

return result;
return PyBytes_FromStringAndSize(buffer.c_str(), buffer.size());
}


Expand Down
2 0