Use std::string instead of manual character buffers where applicable.
This commit is contained in:
@@ -98,14 +98,12 @@ std::string PycLong::repr() const
|
||||
void PycFloat::load(PycData* stream, PycModule*)
|
||||
{
|
||||
int len = stream->getByte();
|
||||
delete[] m_value;
|
||||
if (len > 0) {
|
||||
m_value = new char[len+1];
|
||||
stream->getBuffer(len, m_value);
|
||||
m_value[len] = 0;
|
||||
} else {
|
||||
m_value = 0;
|
||||
}
|
||||
if (len < 0)
|
||||
throw std::bad_alloc();
|
||||
|
||||
m_value.resize(len);
|
||||
if (len > 0)
|
||||
stream->getBuffer(len, &m_value.front());
|
||||
}
|
||||
|
||||
bool PycFloat::isEqual(PycRef<PycObject> obj) const
|
||||
@@ -114,9 +112,7 @@ bool PycFloat::isEqual(PycRef<PycObject> obj) const
|
||||
return false;
|
||||
|
||||
PycRef<PycFloat> floatObj = obj.cast<PycFloat>();
|
||||
if (m_value == floatObj->m_value)
|
||||
return true;
|
||||
return (strcmp(m_value, floatObj->m_value) == 0);
|
||||
return m_value == floatObj->m_value;
|
||||
}
|
||||
|
||||
|
||||
@@ -126,14 +122,12 @@ void PycComplex::load(PycData* stream, PycModule* mod)
|
||||
PycFloat::load(stream, mod);
|
||||
|
||||
int len = stream->getByte();
|
||||
delete[] m_imag;
|
||||
if (len > 0) {
|
||||
m_imag = new char[len+1];
|
||||
stream->getBuffer(len, m_imag);
|
||||
m_imag[len] = 0;
|
||||
} else {
|
||||
m_imag = 0;
|
||||
}
|
||||
if (len < 0)
|
||||
throw std::bad_alloc();
|
||||
|
||||
m_imag.resize(len);
|
||||
if (len > 0)
|
||||
stream->getBuffer(len, &m_imag.front());
|
||||
}
|
||||
|
||||
bool PycComplex::isEqual(PycRef<PycObject> obj) const
|
||||
@@ -142,9 +136,7 @@ bool PycComplex::isEqual(PycRef<PycObject> obj) const
|
||||
return false;
|
||||
|
||||
PycRef<PycComplex> floatObj = obj.cast<PycComplex>();
|
||||
if (m_imag == floatObj->m_imag)
|
||||
return true;
|
||||
return (strcmp(m_imag, floatObj->m_imag) == 0);
|
||||
return m_imag == floatObj->m_imag;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -47,35 +47,31 @@ private:
|
||||
class PycFloat : public PycObject {
|
||||
public:
|
||||
PycFloat(int type = TYPE_FLOAT)
|
||||
: PycObject(type), m_value(0) { }
|
||||
|
||||
~PycFloat() { delete[] m_value; }
|
||||
: PycObject(type) { }
|
||||
|
||||
bool isEqual(PycRef<PycObject> obj) const 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:
|
||||
char* m_value; // Floats are stored as strings
|
||||
std::string m_value; // Floats are stored as strings
|
||||
};
|
||||
|
||||
class PycComplex : public PycFloat {
|
||||
public:
|
||||
PycComplex(int type = TYPE_COMPLEX)
|
||||
: PycFloat(type), m_imag(0) { }
|
||||
|
||||
~PycComplex() { delete[] m_imag; }
|
||||
: PycFloat(type) { }
|
||||
|
||||
bool isEqual(PycRef<PycObject> obj) const 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:
|
||||
char* m_imag;
|
||||
std::string m_imag;
|
||||
};
|
||||
|
||||
class PycCFloat : public PycObject {
|
||||
|
@@ -123,7 +123,9 @@ public:
|
||||
int type() const { return m_type; }
|
||||
|
||||
virtual bool isEqual(PycRef<PycObject> obj) const
|
||||
{ return obj.isIdent(this); }
|
||||
{
|
||||
return obj.isIdent(this);
|
||||
}
|
||||
|
||||
virtual void load(PycData*, PycModule*) { }
|
||||
|
||||
|
@@ -4,10 +4,10 @@
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
static void ascii_to_utf8(char** data)
|
||||
static void ascii_to_utf8(std::string* data)
|
||||
{
|
||||
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) {
|
||||
if (*cp & 0x80)
|
||||
utf8len += 2;
|
||||
@@ -26,9 +26,10 @@ static void ascii_to_utf8(char** data)
|
||||
return;
|
||||
}
|
||||
|
||||
char* utf8_buffer = new char[utf8len + 1];
|
||||
unsigned char* up = reinterpret_cast<unsigned char*>(utf8_buffer);
|
||||
cp = reinterpret_cast<unsigned char*>(*data);
|
||||
std::string utf8_buffer;
|
||||
utf8_buffer.resize(utf8len);
|
||||
auto up = reinterpret_cast<unsigned char*>(&utf8_buffer.front());
|
||||
cp = reinterpret_cast<const unsigned char*>(data->c_str());
|
||||
while (*cp) {
|
||||
if (*cp & 0x80) {
|
||||
*up++ = 0xC0 | ((*cp >> 6) & 0x1F);
|
||||
@@ -39,45 +40,32 @@ static void ascii_to_utf8(char** data)
|
||||
++cp;
|
||||
}
|
||||
|
||||
utf8_buffer[utf8len] = 0;
|
||||
delete[] *data;
|
||||
*data = utf8_buffer;
|
||||
*data = std::move(utf8_buffer);
|
||||
}
|
||||
|
||||
/* PycString */
|
||||
void PycString::load(PycData* stream, PycModule* mod)
|
||||
{
|
||||
delete[] m_value;
|
||||
|
||||
if (type() == TYPE_STRINGREF) {
|
||||
PycRef<PycString> str = mod->getIntern(stream->get32());
|
||||
m_length = str->length();
|
||||
if (m_length) {
|
||||
m_value = new char[m_length+1];
|
||||
memcpy(m_value, str->value(), m_length);
|
||||
m_value[m_length] = 0;
|
||||
} else {
|
||||
m_value = 0;
|
||||
}
|
||||
m_value.resize(str->length());
|
||||
std::char_traits<char>::copy(&m_value.front(), str->value(), str->length());
|
||||
} else {
|
||||
int length;
|
||||
if (type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
|
||||
m_length = stream->getByte();
|
||||
length = stream->getByte();
|
||||
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();
|
||||
|
||||
if (m_length) {
|
||||
m_value = new char[m_length+1];
|
||||
stream->getBuffer(m_length, m_value);
|
||||
m_value[m_length] = 0;
|
||||
|
||||
m_value.resize(length);
|
||||
if (length) {
|
||||
stream->getBuffer(length, &m_value.front());
|
||||
if (type() == TYPE_ASCII || type() == TYPE_ASCII_INTERNED ||
|
||||
type() == TYPE_SHORT_ASCII || type() == TYPE_SHORT_ASCII_INTERNED)
|
||||
ascii_to_utf8(&m_value);
|
||||
} else {
|
||||
m_value = 0;
|
||||
}
|
||||
|
||||
if (type() == TYPE_INTERNED || type() == TYPE_ASCII_INTERNED ||
|
||||
@@ -95,15 +83,6 @@ bool PycString::isEqual(PycRef<PycObject> obj) const
|
||||
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)
|
||||
{
|
||||
if (prefix != 0)
|
||||
|
14
pyc_string.h
14
pyc_string.h
@@ -4,25 +4,23 @@
|
||||
#include "pyc_object.h"
|
||||
#include "data.h"
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
class PycString : public PycObject {
|
||||
public:
|
||||
PycString(int type = TYPE_STRING)
|
||||
: PycObject(type), m_value(0), m_length(0) { }
|
||||
|
||||
~PycString() { delete[] m_value; }
|
||||
: PycObject(type) { }
|
||||
|
||||
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;
|
||||
|
||||
int length() const { return m_length; }
|
||||
const char* value() const { return m_value; }
|
||||
int length() const { return (int)m_value.size(); }
|
||||
const char* value() const { return m_value.c_str(); }
|
||||
|
||||
private:
|
||||
char* m_value;
|
||||
int m_length;
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
void OutputString(PycRef<PycString> str, char prefix = 0,
|
||||
|
Reference in New Issue
Block a user