Commit c44e4dab authored by Kevin Modzelewski's avatar Kevin Modzelewski

Get rid of boxString overloads

and have the compiler pick the best way to convert to StringRef

I was running into some cases where we had StringRefs but would call
boxString which takes an std::string.  So hopefully this change
makes things cleaner and (slightly) faster.
parent a2fe70c2
...@@ -254,7 +254,7 @@ extern "C" Box* chr(Box* arg) { ...@@ -254,7 +254,7 @@ extern "C" Box* chr(Box* arg) {
} }
char c = (char)n; char c = (char)n;
return boxStringRef(llvm::StringRef(&c, 1)); return boxString(llvm::StringRef(&c, 1));
} }
extern "C" Box* unichr(Box* arg) { extern "C" Box* unichr(Box* arg) {
......
...@@ -199,14 +199,14 @@ void addToSysArgv(const char* str) { ...@@ -199,14 +199,14 @@ void addToSysArgv(const char* str) {
void appendToSysPath(llvm::StringRef path) { void appendToSysPath(llvm::StringRef path) {
BoxedList* sys_path = getSysPath(); BoxedList* sys_path = getSysPath();
listAppendInternal(sys_path, boxStringRef(path)); listAppendInternal(sys_path, boxString(path));
} }
void prependToSysPath(llvm::StringRef path) { void prependToSysPath(llvm::StringRef path) {
BoxedList* sys_path = getSysPath(); BoxedList* sys_path = getSysPath();
static std::string attr = "insert"; static std::string attr = "insert";
callattr(sys_path, &attr, CallattrFlags({.cls_only = false, .null_on_nonexistent = false }), ArgPassSpec(2), callattr(sys_path, &attr, CallattrFlags({.cls_only = false, .null_on_nonexistent = false }), ArgPassSpec(2),
boxInt(0), boxStringRef(path), NULL, NULL, NULL); boxInt(0), boxString(path), NULL, NULL, NULL);
} }
static BoxedClass* sys_flags_cls; static BoxedClass* sys_flags_cls;
......
...@@ -50,17 +50,10 @@ extern "C" Box* boxStringPtr(const std::string* s) { ...@@ -50,17 +50,10 @@ extern "C" Box* boxStringPtr(const std::string* s) {
return new (s->size()) BoxedString(s->c_str(), s->size()); return new (s->size()) BoxedString(s->c_str(), s->size());
} }
Box* boxStringRef(llvm::StringRef s) { Box* boxString(llvm::StringRef s) {
return new (s.size()) BoxedString(s); return new (s.size()) BoxedString(s);
} }
Box* boxString(const std::string& s) {
return new (s.size()) BoxedString(s.c_str(), s.size());
}
Box* boxString(std::string&& s) {
return new (s.size()) BoxedString(s.c_str(), s.size());
}
Box* boxStringTwine(const llvm::Twine& t) { Box* boxStringTwine(const llvm::Twine& t) {
llvm::SmallString<256> Vec; llvm::SmallString<256> Vec;
return boxString(t.toStringRef(Vec)); return boxString(t.toStringRef(Vec));
......
...@@ -923,7 +923,7 @@ extern "C" Box* intHex(BoxedInt* self) { ...@@ -923,7 +923,7 @@ extern "C" Box* intHex(BoxedInt* self) {
len = snprintf(buf, sizeof(buf), "-0x%lx", std::abs(self->n)); len = snprintf(buf, sizeof(buf), "-0x%lx", std::abs(self->n));
else else
len = snprintf(buf, sizeof(buf), "0x%lx", self->n); len = snprintf(buf, sizeof(buf), "0x%lx", self->n);
return boxStringRef(llvm::StringRef(buf, len)); return boxString(llvm::StringRef(buf, len));
} }
extern "C" Box* intOct(BoxedInt* self) { extern "C" Box* intOct(BoxedInt* self) {
......
...@@ -1117,7 +1117,7 @@ Box* descriptorClsSpecialCases(GetattrRewriteArgs* rewrite_args, BoxedClass* cls ...@@ -1117,7 +1117,7 @@ Box* descriptorClsSpecialCases(GetattrRewriteArgs* rewrite_args, BoxedClass* cls
Box* boxChar(char c) { Box* boxChar(char c) {
char d[1]; char d[1];
d[0] = c; d[0] = c;
return boxStringRef(llvm::StringRef(d, 1)); return boxString(llvm::StringRef(d, 1));
} }
static Box* noneIfNull(Box* b) { static Box* noneIfNull(Box* b) {
...@@ -1265,7 +1265,7 @@ Box* dataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, llvm:: ...@@ -1265,7 +1265,7 @@ Box* dataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, llvm::
rewrite_args = NULL; rewrite_args = NULL;
REWRITE_ABORTED(""); REWRITE_ABORTED("");
char* rtn = reinterpret_cast<char*>((char*)obj + member_desc->offset); char* rtn = reinterpret_cast<char*>((char*)obj + member_desc->offset);
return boxStringRef(llvm::StringRef(rtn)); return boxString(llvm::StringRef(rtn));
} }
default: default:
......
...@@ -1823,9 +1823,9 @@ Box* strStrip(BoxedString* self, Box* chars) { ...@@ -1823,9 +1823,9 @@ Box* strStrip(BoxedString* self, Box* chars) {
if (isSubclass(chars->cls, str_cls)) { if (isSubclass(chars->cls, str_cls)) {
auto chars_str = static_cast<BoxedString*>(chars)->s(); auto chars_str = static_cast<BoxedString*>(chars)->s();
return boxStringRef(str.trim(chars_str)); return boxString(str.trim(chars_str));
} else if (chars->cls == none_cls) { } else if (chars->cls == none_cls) {
return boxStringRef(str.trim(" \t\n\r\f\v")); return boxString(str.trim(" \t\n\r\f\v"));
} else if (isSubclass(chars->cls, unicode_cls)) { } else if (isSubclass(chars->cls, unicode_cls)) {
PyObject* uniself = PyUnicode_FromObject((PyObject*)self); PyObject* uniself = PyUnicode_FromObject((PyObject*)self);
PyObject* res; PyObject* res;
...@@ -1847,9 +1847,9 @@ Box* strLStrip(BoxedString* self, Box* chars) { ...@@ -1847,9 +1847,9 @@ Box* strLStrip(BoxedString* self, Box* chars) {
if (isSubclass(chars->cls, str_cls)) { if (isSubclass(chars->cls, str_cls)) {
auto chars_str = static_cast<BoxedString*>(chars)->s(); auto chars_str = static_cast<BoxedString*>(chars)->s();
return boxStringRef(str.ltrim(chars_str)); return boxString(str.ltrim(chars_str));
} else if (chars->cls == none_cls) { } else if (chars->cls == none_cls) {
return boxStringRef(str.ltrim(" \t\n\r\f\v")); return boxString(str.ltrim(" \t\n\r\f\v"));
} else if (isSubclass(chars->cls, unicode_cls)) { } else if (isSubclass(chars->cls, unicode_cls)) {
PyObject* uniself = PyUnicode_FromObject((PyObject*)self); PyObject* uniself = PyUnicode_FromObject((PyObject*)self);
PyObject* res; PyObject* res;
...@@ -1871,9 +1871,9 @@ Box* strRStrip(BoxedString* self, Box* chars) { ...@@ -1871,9 +1871,9 @@ Box* strRStrip(BoxedString* self, Box* chars) {
if (isSubclass(chars->cls, str_cls)) { if (isSubclass(chars->cls, str_cls)) {
auto chars_str = static_cast<BoxedString*>(chars)->s(); auto chars_str = static_cast<BoxedString*>(chars)->s();
return boxStringRef(str.rtrim(chars_str)); return boxString(str.rtrim(chars_str));
} else if (chars->cls == none_cls) { } else if (chars->cls == none_cls) {
return boxStringRef(str.rtrim(" \t\n\r\f\v")); return boxString(str.rtrim(" \t\n\r\f\v"));
} else if (isSubclass(chars->cls, unicode_cls)) { } else if (isSubclass(chars->cls, unicode_cls)) {
PyObject* uniself = PyUnicode_FromObject((PyObject*)self); PyObject* uniself = PyUnicode_FromObject((PyObject*)self);
PyObject* res; PyObject* res;
...@@ -2220,7 +2220,7 @@ extern "C" Box* strGetitem(BoxedString* self, Box* slice) { ...@@ -2220,7 +2220,7 @@ extern "C" Box* strGetitem(BoxedString* self, Box* slice) {
} }
char c = self->s()[n]; char c = self->s()[n];
return boxStringRef(llvm::StringRef(&c, 1)); return boxString(llvm::StringRef(&c, 1));
} else if (slice->cls == slice_cls) { } else if (slice->cls == slice_cls) {
BoxedSlice* sslice = static_cast<BoxedSlice*>(slice); BoxedSlice* sslice = static_cast<BoxedSlice*>(slice);
...@@ -2269,7 +2269,7 @@ public: ...@@ -2269,7 +2269,7 @@ public:
char c = *self->it; char c = *self->it;
++self->it; ++self->it;
return boxStringRef(llvm::StringRef(&c, 1)); return boxString(llvm::StringRef(&c, 1));
} }
}; };
......
...@@ -418,7 +418,7 @@ Box* BoxedModule::getStringConstant(llvm::StringRef ast_str) { ...@@ -418,7 +418,7 @@ Box* BoxedModule::getStringConstant(llvm::StringRef ast_str) {
if (idx_iter != str_const_index.end()) if (idx_iter != str_const_index.end())
return str_constants[idx_iter->second]; return str_constants[idx_iter->second];
Box* box = boxStringRef(ast_str); Box* box = boxString(ast_str);
str_const_index[ast_str] = str_constants.size(); str_const_index[ast_str] = str_constants.size();
str_constants.push_back(box); str_constants.push_back(box);
return box; return box;
...@@ -1725,7 +1725,7 @@ Box* attrwrapperKeys(Box* b) { ...@@ -1725,7 +1725,7 @@ Box* attrwrapperKeys(Box* b) {
} }
void attrwrapperDel(Box* b, llvm::StringRef attr) { void attrwrapperDel(Box* b, llvm::StringRef attr) {
AttrWrapper::delitem(b, boxStringRef(attr)); AttrWrapper::delitem(b, boxString(attr));
} }
Box* objectNewNoArgs(BoxedClass* cls) { Box* objectNewNoArgs(BoxedClass* cls) {
......
...@@ -111,9 +111,7 @@ extern "C" Box* boxInstanceMethod(Box* obj, Box* func, Box* type); ...@@ -111,9 +111,7 @@ extern "C" Box* boxInstanceMethod(Box* obj, Box* func, Box* type);
extern "C" Box* boxUnboundInstanceMethod(Box* func, Box* type); extern "C" Box* boxUnboundInstanceMethod(Box* func, Box* type);
extern "C" Box* boxStringPtr(const std::string* s); extern "C" Box* boxStringPtr(const std::string* s);
Box* boxString(const std::string& s); Box* boxString(llvm::StringRef s);
Box* boxString(std::string&& s);
Box* boxStringRef(llvm::StringRef s);
Box* boxStringTwine(const llvm::Twine& s); Box* boxStringTwine(const llvm::Twine& s);
extern "C" BoxedString* boxStrConstant(const char* chars); extern "C" BoxedString* boxStrConstant(const char* chars);
......
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