Use std::string instead of manual character buffers where applicable.

This commit is contained in:
Michael Hansen
2019-10-02 16:01:54 -07:00
parent 8d3752b4f0
commit f88869fb17
5 changed files with 45 additions and 78 deletions

View File

@@ -98,14 +98,12 @@ std::string PycLong::repr() const
void PycFloat::load(PycData* stream, PycModule*) void PycFloat::load(PycData* stream, PycModule*)
{ {
int len = stream->getByte(); int len = stream->getByte();
delete[] m_value; if (len < 0)
if (len > 0) { throw std::bad_alloc();
m_value = new char[len+1];
stream->getBuffer(len, m_value); m_value.resize(len);
m_value[len] = 0; if (len > 0)
} else { stream->getBuffer(len, &m_value.front());
m_value = 0;
}
} }
bool PycFloat::isEqual(PycRef<PycObject> obj) const bool PycFloat::isEqual(PycRef<PycObject> obj) const
@@ -114,9 +112,7 @@ bool PycFloat::isEqual(PycRef<PycObject> obj) const
return false; return false;
PycRef<PycFloat> floatObj = obj.cast<PycFloat>(); PycRef<PycFloat> floatObj = obj.cast<PycFloat>();
if (m_value == floatObj->m_value) return m_value == floatObj->m_value;
return true;
return (strcmp(m_value, floatObj->m_value) == 0);
} }
@@ -126,14 +122,12 @@ void PycComplex::load(PycData* stream, PycModule* mod)
PycFloat::load(stream, mod); PycFloat::load(stream, mod);
int len = stream->getByte(); int len = stream->getByte();
delete[] m_imag; if (len < 0)
if (len > 0) { throw std::bad_alloc();
m_imag = new char[len+1];
stream->getBuffer(len, m_imag); m_imag.resize(len);
m_imag[len] = 0; if (len > 0)
} else { stream->getBuffer(len, &m_imag.front());
m_imag = 0;
}
} }
bool PycComplex::isEqual(PycRef<PycObject> obj) const bool PycComplex::isEqual(PycRef<PycObject> obj) const
@@ -142,9 +136,7 @@ bool PycComplex::isEqual(PycRef<PycObject> obj) const
return false; return false;
PycRef<PycComplex> floatObj = obj.cast<PycComplex>(); PycRef<PycComplex> floatObj = obj.cast<PycComplex>();
if (m_imag == floatObj->m_imag) return m_imag == floatObj->m_imag;
return true;
return (strcmp(m_imag, floatObj->m_imag) == 0);
} }

View File

@@ -47,35 +47,31 @@ private:
class PycFloat : public PycObject { class PycFloat : public PycObject {
public: public:
PycFloat(int type = TYPE_FLOAT) PycFloat(int type = TYPE_FLOAT)
: PycObject(type), m_value(0) { } : PycObject(type) { }
~PycFloat() { delete[] m_value; }
bool isEqual(PycRef<PycObject> obj) const override; bool isEqual(PycRef<PycObject> obj) const override;
void load(class PycData* stream, class PycModule* mod) override; void load(class PycData* stream, class PycModule* mod) override;
const char* value() const { return m_value; } const char* value() const { return m_value.c_str(); }
private: private:
char* m_value; // Floats are stored as strings std::string m_value; // Floats are stored as strings
}; };
class PycComplex : public PycFloat { class PycComplex : public PycFloat {
public: public:
PycComplex(int type = TYPE_COMPLEX) PycComplex(int type = TYPE_COMPLEX)
: PycFloat(type), m_imag(0) { } : PycFloat(type) { }
~PycComplex() { delete[] m_imag; }
bool isEqual(PycRef<PycObject> obj) const override; bool isEqual(PycRef<PycObject> obj) const override;
void load(class PycData* stream, class PycModule* mod) override; void load(class PycData* stream, class PycModule* mod) override;
const char* imag() const { return m_imag; } const char* imag() const { return m_imag.c_str(); }
private: private:
char* m_imag; std::string m_imag;
}; };
class PycCFloat : public PycObject { class PycCFloat : public PycObject {

View File

@@ -123,7 +123,9 @@ public:
int type() const { return m_type; } int type() const { return m_type; }
virtual bool isEqual(PycRef<PycObject> obj) const virtual bool isEqual(PycRef<PycObject> obj) const
{ return obj.isIdent(this); } {
return obj.isIdent(this);
}
virtual void load(PycData*, PycModule*) { } virtual void load(PycData*, PycModule*) { }

View File

@@ -4,10 +4,10 @@
#include <cstring> #include <cstring>
#include <limits> #include <limits>
static void ascii_to_utf8(char** data) static void ascii_to_utf8(std::string* data)
{ {
size_t utf8len = 0, asciilen = 0; size_t utf8len = 0, asciilen = 0;
unsigned char* cp = reinterpret_cast<unsigned char*>(*data); auto cp = reinterpret_cast<const unsigned char*>(data->c_str());
while (*cp) { while (*cp) {
if (*cp & 0x80) if (*cp & 0x80)
utf8len += 2; utf8len += 2;
@@ -26,9 +26,10 @@ static void ascii_to_utf8(char** data)
return; return;
} }
char* utf8_buffer = new char[utf8len + 1]; std::string utf8_buffer;
unsigned char* up = reinterpret_cast<unsigned char*>(utf8_buffer); utf8_buffer.resize(utf8len);
cp = reinterpret_cast<unsigned char*>(*data); auto up = reinterpret_cast<unsigned char*>(&utf8_buffer.front());
cp = reinterpret_cast<const unsigned char*>(data->c_str());
while (*cp) { while (*cp) {
if (*cp & 0x80) { if (*cp & 0x80) {
*up++ = 0xC0 | ((*cp >> 6) & 0x1F); *up++ = 0xC0 | ((*cp >> 6) & 0x1F);
@@ -39,45 +40,32 @@ static void ascii_to_utf8(char** data)
++cp; ++cp;
} }
utf8_buffer[utf8len] = 0; *data = std::move(utf8_buffer);
delete[] *data;
*data = utf8_buffer;
} }
/* PycString */ /* PycString */
void PycString::load(PycData* stream, PycModule* mod) void PycString::load(PycData* stream, PycModule* mod)
{ {
delete[] m_value;
if (type() == TYPE_STRINGREF) { if (type() == TYPE_STRINGREF) {
PycRef<PycString> str = mod->getIntern(stream->get32()); PycRef<PycString> str = mod->getIntern(stream->get32());
m_length = str->length(); m_value.resize(str->length());
if (m_length) { std::char_traits<char>::copy(&m_value.front(), str->value(), str->length());
m_value = new char[m_length+1];
memcpy(m_value, str->value(), m_length);
m_value[m_length] = 0;
} else {
m_value = 0;
}
} else { } else {
int length;
if (type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED) if (type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
m_length = stream->getByte(); length = stream->getByte();
else else
m_length = stream->get32(); length = stream->get32();
if (m_length < 0 || (m_length > std::numeric_limits<int>::max() - 1)) if (length < 0)
throw std::bad_alloc(); throw std::bad_alloc();
if (m_length) { m_value.resize(length);
m_value = new char[m_length+1]; if (length) {
stream->getBuffer(m_length, m_value); stream->getBuffer(length, &m_value.front());
m_value[m_length] = 0;
if (type() == TYPE_ASCII || type() == TYPE_ASCII_INTERNED || if (type() == TYPE_ASCII || type() == TYPE_ASCII_INTERNED ||
type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED) type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
ascii_to_utf8(&m_value); ascii_to_utf8(&m_value);
} else {
m_value = 0;
} }
if (type() == TYPE_INTERNED || type() == TYPE_ASCII_INTERNED || if (type() == TYPE_INTERNED || type() == TYPE_ASCII_INTERNED ||
@@ -95,15 +83,6 @@ bool PycString::isEqual(PycRef<PycObject> obj) const
return isEqual(strObj->m_value); return isEqual(strObj->m_value);
} }
bool PycString::isEqual(const char* str) const
{
if (m_value == str)
return true;
if (!m_value)
return false;
return (strcmp(m_value, str) == 0);
}
void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F) void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F)
{ {
if (prefix != 0) if (prefix != 0)

View File

@@ -4,25 +4,23 @@
#include "pyc_object.h" #include "pyc_object.h"
#include "data.h" #include "data.h"
#include <cstdio> #include <cstdio>
#include <string>
class PycString : public PycObject { class PycString : public PycObject {
public: public:
PycString(int type = TYPE_STRING) PycString(int type = TYPE_STRING)
: PycObject(type), m_value(0), m_length(0) { } : PycObject(type) { }
~PycString() { delete[] m_value; }
bool isEqual(PycRef<PycObject> obj) const override; bool isEqual(PycRef<PycObject> obj) const override;
bool isEqual(const char* str) const; bool isEqual(const std::string& str) const { return m_value == str; }
void load(class PycData* stream, class PycModule* mod) override; void load(class PycData* stream, class PycModule* mod) override;
int length() const { return m_length; } int length() const { return (int)m_value.size(); }
const char* value() const { return m_value; } const char* value() const { return m_value.c_str(); }
private: private:
char* m_value; std::string m_value;
int m_length;
}; };
void OutputString(PycRef<PycString> str, char prefix = 0, void OutputString(PycRef<PycString> str, char prefix = 0,