diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index bb3de83d..b29dc96d 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -60,11 +60,12 @@ struct Str{ friend std::ostream& operator<<(std::ostream& os, const Str& str); + const char* c_str() const { return data; } + std::string_view sv() const { return std::string_view(data, size); } + std::string str() const { return std::string(data, size); } + Str substr(int start, int len) const; Str substr(int start) const; - const char* c_str() const; - std::string_view sv() const; - std::string str() const; Str strip(bool left, bool right, const Str& chars) const; Str strip(bool left=true, bool right=true) const; Str lstrip() const { return strip(true, false); } @@ -90,15 +91,17 @@ struct Str{ struct StrName { uint16_t index; - StrName(); - explicit StrName(uint16_t index); - StrName(const char* s); - StrName(const Str& s); - std::string_view sv() const; - const char* c_str() const; - bool empty() const { return index == 0; } - Str escape() const; + StrName(): index(0) {} + explicit StrName(uint16_t index): index(index) {} + StrName(const char* s): index(get(s).index) {} + StrName(const Str& s): index(get(s.sv()).index) {} + + std::string_view sv() const { return _r_interned()[index];} + const char* c_str() const { return _r_interned()[index].c_str(); } + + bool empty() const { return index == 0; } + Str escape() const { return Str(sv()).escape(); } bool operator==(const StrName& other) const noexcept { return this->index == other.index; diff --git a/src/str.cpp b/src/str.cpp index f4d379f9..b0308ffb 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -182,18 +182,6 @@ int utf8len(unsigned char c, bool suppress){ return substr(start, size - start); } - const char* Str::c_str() const{ - return data; - } - - std::string_view Str::sv() const { - return std::string_view(data, size); - } - - std::string Str::str() const { - return std::string(data, size); - } - Str Str::strip(bool left, bool right, const Str& chars) const { int L = 0; int R = u8_length(); @@ -414,31 +402,10 @@ int utf8len(unsigned char c, bool suppress){ return StrName(index); } - Str StrName::escape() const { - return Str(sv()).escape(); - } - bool StrName::is_valid(int index) { return _r_interned().find(index) != _r_interned().end(); } - StrName::StrName(): index(0) {} - StrName::StrName(uint16_t index): index(index) {} - StrName::StrName(const char* s): index(get(s).index) {} - StrName::StrName(const Str& s){ - index = get(s.sv()).index; - } - - std::string_view StrName::sv() const { - const std::string& str = _r_interned()[index]; - return std::string_view(str); - } - - const char* StrName::c_str() const{ - const std::string& str = _r_interned()[index]; - return str.c_str(); - } - Str SStream::str(){ // after this call, the buffer is no longer valid buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0'