Commit a2fe70c2 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Switch some more runtime functions to taking StringRefs

The motivating one was classLookup(), since this happened
extremely frequently (once for every old-style instance
lookup), but I decided to go through and get some others.
parent c510bb9e
......@@ -650,8 +650,8 @@ BoxedModule* createModule(const std::string& name, const char* fn = NULL, const
Box* moduleInit(BoxedModule* self, Box* name, Box* doc = NULL);
// TODO where to put this
void appendToSysPath(const std::string& path);
void prependToSysPath(const std::string& path);
void appendToSysPath(llvm::StringRef path);
void prependToSysPath(llvm::StringRef path);
void addToSysArgv(const char* str);
// Raise a SyntaxError that occurs at a specific location.
......
......@@ -197,16 +197,16 @@ void addToSysArgv(const char* str) {
listAppendInternal(sys_argv, boxStrConstant(str));
}
void appendToSysPath(const std::string& path) {
void appendToSysPath(llvm::StringRef path) {
BoxedList* sys_path = getSysPath();
listAppendInternal(sys_path, boxStringPtr(&path));
listAppendInternal(sys_path, boxStringRef(path));
}
void prependToSysPath(const std::string& path) {
void prependToSysPath(llvm::StringRef path) {
BoxedList* sys_path = getSysPath();
static std::string attr = "insert";
callattr(sys_path, &attr, CallattrFlags({.cls_only = false, .null_on_nonexistent = false }), ArgPassSpec(2),
boxInt(0), boxString(path), NULL, NULL, NULL);
boxInt(0), boxStringRef(path), NULL, NULL, NULL);
}
static BoxedClass* sys_flags_cls;
......
......@@ -28,7 +28,7 @@ extern "C" {
BoxedClass* classobj_cls, *instance_cls;
}
static Box* classLookup(BoxedClassobj* cls, const std::string& attr) {
static Box* classLookup(BoxedClassobj* cls, llvm::StringRef attr) {
Box* r = cls->getattr(attr);
if (r)
return r;
......
......@@ -632,7 +632,7 @@ BoxedFloat* _floatNew(Box* a) {
} else if (isSubclass(a->cls, int_cls)) {
return new BoxedFloat(static_cast<BoxedInt*>(a)->n);
} else if (a->cls == str_cls) {
const std::string& s = static_cast<BoxedString*>(a)->s();
llvm::StringRef s = static_cast<BoxedString*>(a)->s();
if (s == "nan")
return new BoxedFloat(NAN);
if (s == "-nan")
......@@ -644,10 +644,11 @@ BoxedFloat* _floatNew(Box* a) {
// TODO this should just use CPython's implementation:
char* endptr;
const char* startptr = s.c_str();
assert(s.data()[s.size()] == '\0');
const char* startptr = s.data();
double r = strtod(startptr, &endptr);
if (endptr != startptr + s.size())
raiseExcHelper(ValueError, "could not convert string to float: %s", s.c_str());
raiseExcHelper(ValueError, "could not convert string to float: %s", s.data());
return new BoxedFloat(r);
} else {
static const std::string float_str("__float__");
......
......@@ -625,8 +625,9 @@ BoxedLong* _longNew(Box* val, Box* _base) {
} else if (isSubclass(val->cls, int_cls)) {
mpz_init_set_si(rtn->n, static_cast<BoxedInt*>(val)->n);
} else if (val->cls == str_cls) {
const std::string& s = static_cast<BoxedString*>(val)->s();
int r = mpz_init_set_str(rtn->n, s.c_str(), 10);
llvm::StringRef s = static_cast<BoxedString*>(val)->s();
assert(s.data()[s.size()] == '\0');
int r = mpz_init_set_str(rtn->n, s.data(), 10);
RELEASE_ASSERT(r == 0, "");
} else if (val->cls == float_cls) {
mpz_init_set_si(rtn->n, static_cast<BoxedFloat*>(val)->d);
......
......@@ -516,7 +516,7 @@ void setupSet() {
v_fu.push_back(FROZENSET);
v_fu.push_back(UNKNOWN);
auto add = [&](const std::string& name, void* func) {
auto add = [&](llvm::StringRef name, void* func) {
CLFunction* func_obj = createRTFunction(2, 0, false, false);
addRTFunction(func_obj, (void*)func, SET, v_ss);
addRTFunction(func_obj, (void*)func, SET, v_sf);
......
......@@ -413,12 +413,12 @@ std::string BoxedModule::name() {
}
}
Box* BoxedModule::getStringConstant(const std::string& ast_str) {
Box* BoxedModule::getStringConstant(llvm::StringRef ast_str) {
auto idx_iter = str_const_index.find(ast_str);
if (idx_iter != str_const_index.end())
return str_constants[idx_iter->second];
Box* box = boxString(ast_str);
Box* box = boxStringRef(ast_str);
str_const_index[ast_str] = str_constants.size();
str_constants.push_back(box);
return box;
......@@ -1724,8 +1724,8 @@ Box* attrwrapperKeys(Box* b) {
return AttrWrapper::keys(b);
}
void attrwrapperDel(Box* b, const std::string& attr) {
AttrWrapper::delitem(b, boxString(attr));
void attrwrapperDel(Box* b, llvm::StringRef attr) {
AttrWrapper::delitem(b, boxStringRef(attr));
}
Box* objectNewNoArgs(BoxedClass* cls) {
......
......@@ -698,7 +698,7 @@ public:
BoxedModule() {} // noop constructor to disable zero-initialization of cls
std::string name();
Box* getStringConstant(const std::string& ast_str);
Box* getStringConstant(llvm::StringRef ast_str);
llvm::StringMap<int> str_const_index;
std::vector<Box*> str_constants;
......@@ -854,7 +854,7 @@ Box* objectSetattr(Box* obj, Box* attr, Box* value);
Box* unwrapAttrWrapper(Box* b);
Box* attrwrapperKeys(Box* b);
void attrwrapperDel(Box* b, const std::string& attr);
void attrwrapperDel(Box* b, llvm::StringRef attr);
Box* boxAst(AST* ast);
AST* unboxAst(Box* b);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment